Skip to content

Commit

Permalink
fixed a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
CHN-STUDENT committed Jun 4, 2019
1 parent ec23fbf commit 25b4fd1
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 39 deletions.
2 changes: 1 addition & 1 deletion EHAUT/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions EHAUT/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ android {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.key
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions EHAUT/app/src/main/java/cn/ehaut/srun3000/LogoutActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,9 @@ class LogoutActivity : AppCompatActivity() {
Log.d(TAG, "Logout")
Network.logout(OnlineInfo.onlineUsername)
}

override fun onBackPressed() {
moveTaskToBack(true)
}
}

248 changes: 210 additions & 38 deletions EHAUT/app/src/main/java/cn/ehaut/srun3000/Network.kt
Original file line number Diff line number Diff line change
@@ -1,47 +1,219 @@
package cn.ehaut.srun3000
import android.os.Handler

// 与ios版保持一致
// see here:https://github.com/ehaut/E-HAUT/blob/master/E-HAUT/Model/AppPreferences.swift
// Created by chn-student on 2019/5/26.
// Copyright © 2019 ehaut. All rights reserved.
//
// Thanks to qianchengyu

public object OnlineInfo{
var networkIsConnect:Boolean = false
var isOnline:Boolean = false
var onlineIp:String = ""
var onlineUsername:String = ""
var usedData:String = ""
var usedTime:Int = 0
}
import java.net.HttpURLConnection
import java.net.URL
import java.nio.charset.Charset
import android.os.Message
import android.util.Log
import java.net.URLEncoder

public object ServerInfo {
//服务器地址示范http://172.16.154.130,请不要忘了http://头以及没有最后/符号
var authServerAddr:String = "http://172.16.154.130"
var authServerPort:String = "69"
var serviceServerAddr:String = "http://172.16.154.130"
var serviceServerPort:String = "8800"
var macAddr:String = ""
var acid:String = "1"
var type:String = "3"
var drop:String = "0"
var pop:String = "1"
var key:String = "1234567890"
}
val networkTag = "Network"
var result:String = ""

public object PostResult {
var networkIsConnect:Boolean = false
var isLoginOK:Boolean = false
var isLogoutOK:Boolean = false
var result:String = ""
}
class Network {
companion object {
fun getUserInfo() {
result = ""
Thread(object : Runnable {
override fun run() {
try {
val serverAddress = ServerInfo.authServerAddr //+ ":" + ServerInfo.authServerPort
val url = serverAddress + "/cgi-bin/rad_user_info"
HttpGet(url)
Log.d("Network", result)
if (result.contains("not_online") || result.contains("not_online_error")) {
OnlineInfo.isOnline = false
OnlineInfo.networkIsConnect = true
} else {
val infoArray = result.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
OnlineInfo.onlineUsername = infoArray[0]
var data = java.lang.Long.valueOf(infoArray[6])
data /= (1024 * 1024).toLong()
OnlineInfo.usedData = data.toString() + "M"
OnlineInfo.onlineIp = infoArray[8]
OnlineInfo.usedTime = infoArray[7].toInt()
OnlineInfo.usedTime += infoArray[2].toInt() - infoArray[1].toInt()
OnlineInfo.isOnline = true
OnlineInfo.networkIsConnect = true
}
} catch (e: java.net.SocketTimeoutException) {
OnlineInfo.networkIsConnect = false
} catch (e: Exception) {
Log.d(networkTag, e.toString())
}
val msg: Message = Message.obtain()
msg.what = 1
msg.obj = "HTTP GET FINSHED"
handler?.sendMessage(msg)
}
}).start()
}
@Throws(Exception::class)
fun Login(usr: String, pwd: CharArray) {
result = ""
Thread(object : Runnable {
override fun run() {
try {
val url = ServerInfo.authServerAddr + ":" + ServerInfo.authServerPort + "/cgi-bin/srun_portal"
val urlencode_usr = URLEncoder.encode(Crypto.usrEncode(usr), "utf-8")
val urlencode_pwd = URLEncoder.encode(Crypto.pwdEncode(String(pwd)), "utf-8")
val urlencode_acid = URLEncoder.encode(ServerInfo.acid, "utf-8")
val urlencode_mac = URLEncoder.encode(ServerInfo.macAddr, "utf-8")
val data = ("action=login&username="
+ urlencode_usr
+ "&password="
+ urlencode_pwd
+ "&drop="
+ ServerInfo.drop
+"&pop="
+ ServerInfo.pop
+"&type="
+ ServerInfo.type
+ "&n=117&mbytes=0&minutes=0&ac_id="
+ urlencode_acid
+ "&mac="
+ urlencode_mac)
HttpPost(url, data)
Log.d("Network", result)
if (result.contains("login_ok")) {
OnlineInfo.isOnline = true
PostResult.result = "login_ok"
PostResult.isLoginOK = true
PostResult.networkIsConnect = true
} else {
OnlineInfo.isOnline = false
PostResult.isLoginOK = false
PostResult.result = result
PostResult.networkIsConnect = true
}
} catch (e: java.net.SocketTimeoutException) {
PostResult.networkIsConnect = false
} catch (e: Exception) {
Log.d(networkTag, e.toString())
}
val msg:Message = Message.obtain()
msg.what = 2
msg.obj = "HTTP POST FINSHED"
handler?.sendMessage(msg)
}
}).start()
}

@Throws(Exception::class)
fun logout(usr: String){
result = ""
Thread(object : Runnable {
override fun run() {
try {
val url = ServerInfo.authServerAddr + ":" + ServerInfo.authServerPort + "/cgi-bin/srun_portal"
val urlencode_usr = URLEncoder.encode(Crypto.usrEncode(usr), "utf-8")
val urlencode_acid = URLEncoder.encode(ServerInfo.acid, "utf-8")
val urlencode_mac = URLEncoder.encode(ServerInfo.macAddr, "utf-8")
HttpPost(url,
"action=logout&ac_id=$urlencode_acid&username=$urlencode_usr&mac=$urlencode_mac&type=2")
Log.d("Network", result)
if (result.contains("logout_ok")) {
OnlineInfo.isOnline = false
PostResult.result = "logout_ok"
PostResult.isLogoutOK = true
PostResult.networkIsConnect = true
} else {
PostResult.isLogoutOK = false
PostResult.result = result
PostResult.networkIsConnect = true
}
} catch (e: java.net.SocketTimeoutException) {
PostResult.networkIsConnect = false
} catch (e: Exception) {
Log.d(networkTag, e.toString())
}
val msg:Message = Message.obtain()
msg.what = 2
msg.obj = "HTTP POST FINSHED"
handler?.sendMessage(msg)
}
}).start()
}

var handler:Handler? = null
@Throws(Exception::class)
fun HttpPost(url: String, data: String) {
val urlObj = URL(null, url)
val urlConnection = urlObj.openConnection()
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
val httpURLConnection = urlConnection as HttpURLConnection
httpURLConnection.setRequestMethod("POST")
httpURLConnection.setDoOutput(true)
val contentBytes = data.toByteArray(charset("UTF-8"))
httpURLConnection.setFixedLengthStreamingMode(contentBytes.size)
val out = httpURLConnection.getOutputStream()
try {
out.write(contentBytes)
} finally {
out.flush()
}
val inputStream = urlConnection.getInputStream()
var contentLength = urlConnection.getContentLength()
contentLength = if (contentLength == -1) 4096 else contentLength
var buffer = ByteArray(contentLength)
var offset = 0
while (true) {
var remain = buffer.size - offset
if (remain <= 0) {
val newSize = buffer.size * 2
val newBuffer = ByteArray(newSize)
System.arraycopy(buffer, 0, newBuffer, 0, offset)
buffer = newBuffer
remain = buffer.size - offset
}
val numRead = inputStream.read(buffer, offset, remain)
if (numRead == -1) {
break
}
offset += numRead
}
if (offset < buffer.size) {
val newBuffer = ByteArray(offset)
System.arraycopy(buffer, 0, newBuffer, 0, offset)
buffer = newBuffer
}
result = String(buffer, Charset.forName("UTF-8"))
}


public object UserInfo {
var username:String = ""
var password:String = ""
@Throws(Exception::class)
fun HttpGet(url: String) {
val urlObj = URL(null, url)
val urlConnection = urlObj.openConnection()
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
val httpURLConnection = urlConnection as HttpURLConnection
httpURLConnection.requestMethod = "GET"
val inputStream = urlConnection.getInputStream()
var contentLength = urlConnection.getContentLength()
contentLength = if (contentLength == -1) 4096 else contentLength
var buffer = ByteArray(contentLength)
var offset = 0
while (true) {
var remain = buffer.size - offset
if (remain <= 0) {
val newSize = buffer.size * 2
val newBuffer = ByteArray(newSize)
System.arraycopy(buffer, 0, newBuffer, 0, offset)
buffer = newBuffer
remain = buffer.size - offset
}
val numRead = inputStream.read(buffer, offset, remain)
if (numRead == -1) {
break
}
offset += numRead
}
if (offset < buffer.size) {
val newBuffer = ByteArray(offset)
System.arraycopy(buffer, 0, newBuffer, 0, offset)
buffer = newBuffer
}
result = String(buffer, Charset.forName("UTF-8"))
}
}
}

0 comments on commit 25b4fd1

Please sign in to comment.