gpt4 book ai didi

android retrofit 无法解析数据为空

转载 作者:行者123 更新时间:2023-11-29 15:38:57 24 4
gpt4 key购买 nike

我有 json 数据,我正在使用 gson 转换器解析这些数据并响应 pojo 类并显示它。我越来越空了。我已经看到很多关于此的问题,但我没有得到太多解决方案。看看我做错了什么。我是改造新手。

{
"loginj": [
{
"JUser_Id": "20",
"JFullName": "aaa",
"JEmail": "abc@gmail.com"
}
]
}

查看我的改造构建器类

 public static APIInterface getInterfaceService() {

Gson gson = new GsonBuilder()
.setLenient()
.create();

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();

final APIInterface mInterfaceService = retrofit.create(APIInterface.class);
return mInterfaceService;
}

模型类

public class Loginj {
private String JUser_Id;
private String JFullName;
private String JEmail;
}

回调类APIInterface

Call<Loginj> login(@Body User user);

得到回应

Call<Loginj> loginResponseCall = apiInterface.login(user);
loginResponseCall.enqueue(new Callback<Loginj> () {
@Override
public void onResponse(Call<Loginj> call, Response<Loginj> response) {
if (response.isSuccessful()) {
Loginj bodywValue = response.body();
Toast.makeText(getApplicationContext(),""+bodywValue.getJFullName(),
Toast.LENGTH_SHORT).show();
}
}

@Override
public void onFailure(Call<Loginj> call, Throwable t){}

最佳答案

选项 1:

这里你的响应在数组中,你需要像这样改变

{
"JUser_Id":"20",
"JFullName":"aaa",
"JEmail":"abc@gmail.com"
}

选项 2:

如果您无法更改您的 API 响应,请为与您的响应 JSON 匹配的响应创建 pojo。

public class RestResponse {
public List<Loginj> loginj;
}

那么你的代码应该是这样的

Call<RestResponse> loginResponseCall = apiInterface.login(user);
loginResponseCall.enqueue(new Callback<RestResponse> () {
@Override
public void onResponse(Call<RestResponse> call, Response<RestResponse> response) {
if (response.isSuccessful()) {
RestResponse body = response.body();
Loginj bodywValue = body.loginj.get(0);
Toast.makeText(getApplicationContext(),""+bodywValue.getJFullName(),Toast.LENGTH_SHORT).show();
}}

@Override
public void onFailure
(Call<RestResponse> call, Throwable t){

}

关于android retrofit 无法解析数据为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44667706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com