面对raspberry-pi-android的改型错误,无法找出发生了什么异常,我正在使用raspberry-pi(android 8.0)改型,试图找出在移动设备中什么样的问题和相同的代码起作用。我在oncreate()中调用getlogin()函数,但不返回响应。
private fun getLogin() {
val userid = username.text.toString()
val pwd = password.text.toString()
Global.pswd = pwd
try{
val gson = GsonBuilder().registerTypeAdapter(LoginResponse::class.java ,
LoginResponse.DataStateDeserializer()).create()
val retrofit : Retrofit = Retrofit.Builder()
.baseUrl(Global.Main_Url)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
val apiInterface = retrofit.create(ApiInterface::class.java)
val call: retrofit2.Call<LoginResponse> = apiInterface.getLogin(
Responses.LoginResponse(Global.HmiSrNo , "1" ,"1" , "IDS")
)
call.enqueue(object : Callback<LoginResponse> {
override fun onFailure(call: retrofit2.Call<LoginResponse>?, t: Throwable?) {
//-- snackbar to dispaly error --//
val snackbar =
Snackbar.make(item, t!!.message!!, Snackbar.LENGTH_LONG)
.setAction("Retry") {
getLogin()
}
snackbar.setActionTextColor(Color.YELLOW)
snackbar.show()
}
override fun onResponse(
call: retrofit2.Call<LoginResponse>?,
response: retrofit2.Response<LoginResponse>?
) {
if (response!!.isSuccessful) {
val list = response.body()
val status = list!!.status
if (status == "success") {
Global.userid = list.UserID
Global.userName = list.UserName
val intent = Intent(applicationContext, BalanceCalibration::class.java)
startActivity(intent)
progressDialog.dismiss()
} else {
val snackbar =
Snackbar.make(item, list.result.toString(),
Snackbar.LENGTH_INDEFINITE)
snackbar.setAction("OK"){
snackbar.dismiss()
}
snackbar.setActionTextColor(Color.YELLOW)
snackbar.show()
}
}
}
})
}catch (e:Exception){
e.printStackTrace()
}
}
我的模特课:
class LoginResponse {
@SerializedName("status")
lateinit var status:String
@SerializedName("message")
lateinit var message : String
@SerializedName("result")
lateinit var result:Any
lateinit var UserID: String
lateinit var UserName:String
lateinit var realPassword: String
class DataStateDeserializer : JsonDeserializer<LoginResponse>
{
override fun deserialize(
json: JsonElement?,
typeOfT: Type?,
context: JsonDeserializationContext?
): LoginResponse {
val loginResponse : LoginResponse = Gson().fromJson(json, LoginResponse::class.java)
val jsonObject = json!!.asJsonObject
if(jsonObject.has("result"))
{
val element = jsonObject.get("result")
if(element != null && !element.isJsonNull)
{
if(element.isJsonPrimitive){
loginResponse.result = element.asString
}
else{
loginResponse.UserID = element.asJsonObject.get("UserID").asString
loginResponse.UserName = element.asJsonObject.get("UserName").asString
}
}
}
return loginResponse
}
}
}
我的界面:
interface ApiInterface {
@POST("/API/Login/loginMain")
fun getLogin(@Body body: Responses.LoginResponse): Call<LoginResponse>
}
暂无答案!
目前还没有任何答案,快来回答吧!