Skip to content
This repository was archived by the owner on Oct 7, 2023. It is now read-only.

Commit cc533ca

Browse files
committed
DormitoryApi: Use GB2312 charset to decode string
Signed-off-by: Fung <[email protected]>
1 parent eb67084 commit cc533ca

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

app/src/main/kotlin/moe/feng/scut/autowifi/MainActivity.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,16 @@ class MainActivity : Activity(), AnkoLogger {
4141

4242
private fun doConnect() {
4343
doAsync {
44-
val isConnected = DormitoryApi
44+
val result = DormitoryApi
4545
.setCurrentIp(WifiUtils.getCurrentIP(this@MainActivity))
46-
.connect("", "")
46+
.connect(username = "", password = "")
47+
val errCode = DormitoryApi.checkError()
4748
uiThread {
48-
info("isConnected: $isConnected")
49+
val dialog = AlertDialogBuilder(this@MainActivity)
50+
dialog.title(if (result is String && result.contains("登陆成功", false)) "Success" else "Fail")
51+
dialog.message("$result\n\n$errCode")
52+
dialog.okButton { }
53+
dialog.show()
4954
}
5055
}
5156
}

app/src/main/kotlin/moe/feng/scut/autowifi/api/DormitoryApi.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package moe.feng.scut.autowifi.api
22

33
import moe.feng.scut.autowifi.support.HttpUtils
4+
import java.nio.charset.Charset
45

56
class DormitoryApi {
67

@@ -16,7 +17,7 @@ class DormitoryApi {
1617
return this
1718
}
1819

19-
fun connect(username : String, password : String) : Boolean {
20+
fun connect(username : String, password : String) : String? {
2021
val operation = "Login"
2122

2223
val result = HttpUtils.postForm(
@@ -34,7 +35,13 @@ class DormitoryApi {
3435
"upass" to password
3536
)
3637
)
37-
return result is String && result.contains("已经成功登录")
38+
// return result is String && result.contains("已经成功登录")
39+
return if (result is ByteArray) String(bytes = result, charset = Charset.forName("GB2312")) else null
40+
}
41+
42+
fun checkError() : String? {
43+
val result = HttpUtils.get("https://s.scut.edu.cn/errcode")
44+
return if (result is ByteArray) String(bytes = result, charset = Charset.forName("GB2312")) else null
3845
}
3946

4047
fun getWlanCenterIp(ip : String) : String {

app/src/main/kotlin/moe/feng/scut/autowifi/support/HttpUtils.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,22 @@ class HttpUtils {
2222
okHttpClient = OkHttpClient.Builder().cookieJar(cookieJar).build()
2323
}
2424

25-
fun get(url : String) : String? {
25+
fun get(url : String) : ByteArray? {
2626
Log.d(TAG, "Request url: " + url)
2727
val request = Request.Builder().url(url).build()
2828
try {
2929
val response = okHttpClient.newCall(request).execute()
3030
Log.d(TAG, "Response code:" + response.code())
31-
val result = response.body().string()
31+
val result = response.body().bytes()
32+
Log.d(TAG, "Response data: " + String(result))
3233
return result
3334
} catch (e : Exception) {
3435
e.printStackTrace()
3536
return null
3637
}
3738
}
3839

39-
fun postForm(url: String, params: Map<String, Any>?): String? {
40+
fun postForm(url: String, params: Map<String, Any>?): ByteArray? {
4041
Log.d(TAG, "Request url: " + url)
4142
val builder = FormBody.Builder()
4243
if (params != null) for ((key, value) in params) {
@@ -48,7 +49,8 @@ class HttpUtils {
4849
try {
4950
val response = okHttpClient.newCall(request).execute()
5051
Log.d(TAG, "Response code: " + response.code())
51-
val result = response.body().string()
52+
val result = response.body().bytes()
53+
Log.d(TAG, "Response data: " + String(result))
5254
return result
5355
} catch (e: Exception) {
5456
e.printStackTrace()

0 commit comments

Comments
 (0)