如何处理Android的改造反应Kotlin?

你们全部 我正在使用改进的API调用示例Android Kotlin项目。 我调用API并显示响应logcat。 但它不处理来自服务器的用户标识和数据。 所以,如果你知道的人分享你最好的经验。

val params = HashMap() params["api_key"] = "api_key_value" params["username"] = "abcd" params["password"] = "1234" doApiLogin.getLogin(params).enqueue(object : Callback { override fun onResponse(call: Call?, response: Response?) { //To change body of created functions use File | Settings | File Templates. if (response != null && response.isSuccessful) { val getLoginAndRegisterResp = response.body() if (getLoginAndRegisterResp != null) { // Here. server response } else { val statusCode = response.code() NajibApplication.instance.setLog("statusCode:" + statusCode) } } else { NajibApplication.instance.setLog("onFailure>>>>") } } override fun onFailure(call: Call?, t: Throwable?) { //To change body of created functions use File | Settings | File Templates. NajibApplication.instance.setLog("onFailure>>>>") } }) 

这里是模型类

 class GetLoginAndRegisterResp { data class LoginResp( val user_info: UserInfo = UserInfo(), val status: Status = Status() ) { override fun toString(): String { return "LoginResp(user_info=$user_info, status=$status)" } } data class UserInfo( @SerializedName("user_id") val user_id: String = "", // @SerializedName("username") val username: String = "", //abcd @SerializedName("login_hash") val login_hash: String = "", // @SerializedName("facebook_id") val facebook_id: String = "", @SerializedName("twitter_id") val twitter_id: String = "", @SerializedName("full_name") val full_name: String = "", // @SerializedName("thumb_url") val thumb_url: String = "", @SerializedName("photo_url") val photo_url: String = "", @SerializedName("mobile") val mobile: String = "", // @SerializedName("email") val email: String = "" // ) { override fun toString(): String { return "UserInfo(user_id='$user_id', username='$username', login_hash='$login_hash', facebook_id='$facebook_id', twitter_id='$twitter_id', full_name='$full_name', thumb_url='$thumb_url', photo_url='$photo_url', mobile='$mobile', email='$email')" } } data class Status( @SerializedName("status_code") val status_code: String = "", //-1 @SerializedName("status_text") val status_text: String = "" //Success. ) { override fun toString(): String { return "Status(status_code='$status_code', status_text='$status_text')" } } 

}

这里是API调用改造Kotlin的代码。

大量的搜索,find了我自己的解决方案。 作为模型类的解决方案并不完美,这是正确的模型类作为我们的要求。

  1. 仅创建Kotlin文件。
  2. 将Json转换成Kotlin类。

并明确项目和运行! 🙂

它应该是工作正常!

 data class LoginResultResp( val user_info: UserInfo = UserInfo(), val status: Status = Status()) data class UserInfo( val user_id: String = "", //1010 val username: String = "", val login_hash: String = "", val facebook_id: String = "", val twitter_id: String = "", val full_name: String = "", // val thumb_url: String = "", val photo_url: String = "", val mobile: String = "", // val email: String = "") data class Status( val status_code: String = "", //-1 val status_text: String = "" //Success.)