【问题标题】:android retrofit unable to parse data geting nullandroid改造无法解析数据为空
【发布时间】:2017-06-21 05:45:04
【问题描述】:

我有 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){}

【问题讨论】:

  • 这里的Loginj是什么?
  • loginj 模型类,所有字段都存在
  • 你得到的 Loginj null 对吗?
  • 看我的回答,我会工作

标签: android json retrofit2


【解决方案1】:

选项 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){

                    }

【讨论】:

    【解决方案2】:

    如果您的 json 示例是正确的,那么 loginj 是一个对象数组,但您说要进行改造,即您想要一个对象。

    试试这样的:

    public class Loginj {
    
        private List<LoginjData> loginj;
    
        private class LoginjData {
            private String JUser_Id;
            private String JFullName;
            private String JEmail;
        }
    }
    

    【讨论】:

    • 感谢它的工作,我可以接受这个,但其他一些答案更清楚,我投票给你的答案
    【解决方案3】:

    再制作一个模型类。

    public class ANY_NAME{
      private List<Loginj> loginj;
     }
    

    并替换此代码:

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

    有了这个:

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

    您的代码将起作用

    【讨论】:

      猜你喜欢
      • 2018-11-07
      • 2015-09-04
      • 2017-07-07
      • 2020-06-17
      • 2015-08-18
      • 1970-01-01
      • 2013-12-13
      • 2015-09-07
      • 1970-01-01
      相关资源
      最近更新 更多