如何在android中处理改装响应Kotlin?
你们所有人。 我正在使用改型从api调用中执行示例Android Kotlin项目。我调用了api并显示响应logcat。但它并没有处理从服务器发送的用户id和数据。所以,如果你知道男人分享你最好的经验如何在android中处理改装响应Kotlin?,android,api,kotlin,retrofit,Android,Api,Kotlin,Retrofit,你们所有人。 我正在使用改型从api调用中执行示例Android Kotlin项目。我调用了api并显示响应logcat。但它并没有处理从服务器发送的用户id和数据。所以,如果你知道男人分享你最好的经验 val params = HashMap<String, String>() params["api_key"] = "api_key_value" params["username"] = "abcd" params["password"] = "123
val params = HashMap<String, String>()
params["api_key"] = "api_key_value"
params["username"] = "abcd"
params["password"] = "1234"
doApiLogin.getLogin(params).enqueue(object : Callback<GetLoginAndRegisterResp> {
override fun onResponse(call: Call<GetLoginAndRegisterResp>?, response: Response<GetLoginAndRegisterResp>?) {
//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<GetLoginAndRegisterResp>?, t: Throwable?) {
//To change body of created functions use File | Settings | File Templates.
NajibApplication.instance.setLog("onFailure>>>>")
}
})
}
这是Api调用Reformation 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.)
请添加服务器响应示例和相应的logcat输出,好吗?“数据”是什么意思?我看不到字段“数据”。我理解的是
状态
对象已正确反序列化,而用户信息
对象未正确反序列化。这是正确的吗?另外,您是否使用Gson将原始响应转换为对象或其他库?响应键的格式是x-total-count
?
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.)
import com.google.gson.GsonBuilder
import java.util.concurrent.TimeUnit
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
object APIClient {
private var retrofit: Retrofit? = null
val client: Retrofit
get() {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder()
.addInterceptor(interceptor)
.connectTimeout(1, TimeUnit.MINUTES)
.retryOnConnectionFailure(true)
.build()
if (retrofit == null) {
val gson = GsonBuilder()
.setLenient()
.create()
retrofit = Retrofit.Builder()
.baseUrl("")
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build()
}
return this!!.retrofit!!
}
}
interface APIInterface {
@POST("login")
fun callLoginWebservice(@Query("device_token") token: String,
@Query("email") userName: String,
@Query("password") password: String
): Call<UserLoginDetailModel>}
init {
if (apiInterface == null) {
apiInterface = APIClient.client.create(APIInterface::class.java)
}
}
apiInterface?.callLoginWebservice(token, userName, password).enqueue(object : Callback<UserLoginDetailModel> {
override fun onResponse(call: Call<UserLoginDetailModel>, response: Response<UserLoginDetailModel>) {
loginResponseInterface.onSuccess(loginDetail?.status.toString(),loginDetail ?. errorcode.toString(), loginDetail)
Log.d("WebServices", "" + loginDetail)
}
override fun onFailure(call: Call<UserLoginDetailModel>, t: Throwable) {
// loginResponseInterface.onSuccess("","", loginDetail);
loginResponseInterface.onFailure(t)
}
})
class UserLoginDetailModel {
@SerializedName("_id")
@Expose
var id: String? = null
@SerializedName("email")
@Expose
var email: String? = ""
@SerializedName("name")
@Expose
var name: String? = null
@SerializedName("height_value")
@Expose
var heightValue: String? = "0'0''"}