parent
a467f95730
commit
0e2e960c31
@ -0,0 +1,25 @@ |
||||
package com.gyf.csams |
||||
|
||||
|
||||
interface UrlPath{ |
||||
fun build():String |
||||
} |
||||
|
||||
enum class RegisterApi(val path: String):UrlPath{ |
||||
register("/register"), |
||||
checkId("/register/checkId"); |
||||
|
||||
override fun build(): String { |
||||
return "/api/account${this.path}" |
||||
} |
||||
} |
||||
|
||||
|
||||
class Api { |
||||
companion object{ |
||||
fun buildUrl(urlPath: UrlPath):String{ |
||||
return "${BuildConfig.SERVER_ADDRESS}${urlPath.build()}" |
||||
} |
||||
} |
||||
} |
||||
|
@ -0,0 +1,122 @@ |
||||
package com.gyf.csams.util |
||||
|
||||
import com.google.gson.Gson |
||||
import com.orhanobut.logger.Logger |
||||
import okhttp3.* |
||||
import okhttp3.MediaType.Companion.toMediaType |
||||
import okhttp3.RequestBody.Companion.toRequestBody |
||||
import java.io.IOException |
||||
import java.lang.reflect.Type |
||||
|
||||
object HttpClient{ |
||||
private val httpClient:OkHttpClient=OkHttpClient() |
||||
|
||||
private val JSON_CONTENT_TYPE="application/json; charset=UTF-8".toMediaType() |
||||
|
||||
|
||||
private fun buildQueryParams(params:Map<String,String>?):String{ |
||||
return if(params?.isNotEmpty() == true) { |
||||
val urlPath = StringBuilder("?") |
||||
for(i in params){ |
||||
urlPath.append(i.key).append("=").append(i.value) |
||||
} |
||||
|
||||
urlPath.toString() |
||||
}else{ |
||||
"" |
||||
} |
||||
} |
||||
|
||||
private fun buildFormBody(params: Map<String, String>?):FormBody{ |
||||
val builder=FormBody.Builder() |
||||
if(params?.isNotEmpty()==true){ |
||||
for(item in params){ |
||||
builder.add(item.key,item.value) |
||||
} |
||||
} |
||||
return builder.build() |
||||
} |
||||
|
||||
/** |
||||
* HTTP GET |
||||
* |
||||
* @param url |
||||
* @param callback |
||||
* @param params |
||||
*/ |
||||
fun get(url:String, callback: Callback, params: Map<String, String>?=null){ |
||||
val request = Request.Builder() |
||||
.url(url.plus(buildQueryParams(params = params))) |
||||
.build() |
||||
val call=httpClient.newCall(request) |
||||
call.enqueue(callback) |
||||
} |
||||
|
||||
/** |
||||
* HTTP POST |
||||
* 发送表单 |
||||
* |
||||
* @param url |
||||
* @param callback |
||||
* @param params |
||||
*/ |
||||
fun post(url:String, callback: Callback, params: Map<String, String>?=null){ |
||||
val request = Request.Builder() |
||||
.url(url) |
||||
.post(body = buildFormBody(params)) |
||||
.build() |
||||
val call=httpClient.newCall(request) |
||||
call.enqueue(callback) |
||||
} |
||||
|
||||
/** |
||||
* HTTP POST |
||||
* 发送JSON |
||||
* |
||||
* @param url |
||||
* @param callback |
||||
* @param jsonBody |
||||
*/ |
||||
fun post(url:String, callback: Callback, jsonBody:String){ |
||||
val request = Request.Builder() |
||||
.url(url) |
||||
.post(body = jsonBody.toRequestBody(contentType = JSON_CONTENT_TYPE)) |
||||
.build() |
||||
val call=httpClient.newCall(request) |
||||
call.enqueue(callback) |
||||
} |
||||
|
||||
|
||||
} |
||||
|
||||
|
||||
data class ApiResponse<T>(val code:Int,val message:String,val body:T?=null) |
||||
|
||||
class SimpleCallback<T>(private val action:String, |
||||
private val onSuccess:(res:ApiResponse<T>) -> Unit, |
||||
private val onFail:(error:String) -> Unit, |
||||
private val type: Type):Callback{ |
||||
override fun onFailure(call: Call, e: IOException) { |
||||
onFail("${action}失败,请联系管理员") |
||||
Logger.e(e,"${action}请求失败,发生IO异常") |
||||
} |
||||
|
||||
override fun onResponse(call: Call, response: Response) { |
||||
if (response.code == 200) { |
||||
val body=response.body |
||||
if (body!=null&&body.contentType()?.subtype == "json") { |
||||
val jsonRes=body.string() |
||||
val res:ApiResponse<T> = Gson().fromJson(jsonRes, type) |
||||
Logger.i("${action}请求响应成功:") |
||||
Logger.json(jsonRes) |
||||
onSuccess(res) |
||||
} else { |
||||
onFail("${action}失败,请联系管理员") |
||||
Logger.e("无法解析${action}请求响应数据:,响应码:${response.code},${response.body}") |
||||
} |
||||
}else{ |
||||
onFail("${action}失败,请联系管理员") |
||||
Logger.e("${action}失败,请求响应码:${response.code}") |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue