简体   繁体   English

改装响应没有得到

[英]Retrofit response not getting

Hi, I'm using retrofit for JSON parsing.I got 200 response code, but I didn't get any proper response to my ArrayList.嗨,我正在使用改造进行 JSON 解析。我得到了 200 个响应代码,但是我没有得到对我的 ArrayList 的任何正确响应。 I only get the response to "status" field.Other Fields are getting null.我只得到对“状态”字段的响应。其他字段为空。 Please Help me.JSON data are given below Thank you :)请帮帮我。下面给出了JSON数据谢谢:)

//json parsing method //json解析方法

     private void load() {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
              .addConverterFactory(GsonConverterFactory.create()).client(client)


        Apiinterface request = retrofit.create(Apiinterface.class);
        Call<MyPojo> call = request.newsget(new Newslist_Post("en",0));
        call.enqueue(new Callback<MyPojo>() {
            @Override
            public void onResponse(Call<MyPojo> call, Response<MyPojo> response{
                try {
                    if (response.code() == 200) {
                        Log.d("act", "onResponse - Status : " +response.code());
                        Gson gson = new Gson();
                       TypeAdapter<MyPojo>adapter=gson.getAdapter(MyPojo.class);
                        try {
                            if (response.errorBody() != null) {
                       Toast.makeText(MainActivity.this, "Request Success", Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    MyPojo news_model = response.body();

                   // data = new ArrayList<>(Arrays.asList(news_model.getNews()));
                    status=news_model.isStatus();
                    count=news_model.getCount();
                    data=news_model.getNews();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<MyPojo> call, Throwable t) {
                Toast.makeText(MainActivity.this, "Please Check your network",Toast.LENGTH_SHORT).show();
              //Log.d("Error",t.getMessage());
            }
       });}

ModelClass模型类

These model classes are used to parse the JSON, in this model class list get second model class contents这些模型类用于解析 JSON,在此模型类列表中获取第二个模型类内容

 public class MyPojo{
 @SerializedName("status")
 private boolean status;
 @SerializedName("count")
 private int count;
 @SerializedName("news")
 private List<News>news;
 public int getCount() {
    return count;
 }
 public List<News> getNews() {
    return news;
 }
 public boolean isStatus() {
    return status;
 }

Second ModelClass第二个模型类

In this model class contains news data, this news data get in the first model class list.在这个模型类中包含新闻数据,这个新闻数据在第一个模型类列表中得到。

 public class News

 {

 private String news_id;

 private String newss_description;

 private String newss_title;

 private String news_image;

  private String lang;

 public String getNews_id ()
 {
    return news_id;
 }

 public String getNewss_description ()
  {
    return newss_description;
  }

public String getNewss_title ()
{
    return newss_title;
}
public String getNews_image ()
{
    return news_image;
}
public String getLang ()
{
    return lang;
}
}



 //JSON


       {
"status": true,
"count": 2,
"news": [
    {
        "news_id": "2",
        "news_image": "5fc2eaf170e6fc8ba6aa3974ce0c2e11.jpg",
        "newss_title": "new 1",
        "newss_description": "test news",
        "lang": "en"
    },
    {
        "news_id": "1",
        "news_image": "31e3650272d006d24ac6c5fd580cace0.jpg",
        "newss_title": "cooking class with tanya in the name of healthy",
        "newss_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
        "lang": "en"
    }
]
}

SerializedName and Expose is missing for news model item新闻模型项缺少 SerializedName 和 Expose

@SerializedName("code")
@Expose

Change your News.class like this.像这样改变你的 News.class。

public class News {
    @SerializedName("news_id")
    @Expose
    private String news_id;
    @SerializedName("newss_description")
    @Expose
    private String newss_description;

    @SerializedName("newss_title")
    @Expose
    private String newss_title;

    @SerializedName("news_image")
    @Expose
    private String news_image;

    @SerializedName("lang")
    @Expose
    private String lang;

    public String getNews_id() {
        return news_id;
    }

    public String getNewss_description() {
        return newss_description;
    }

    public String getNewss_title() {
        return newss_title;
    }

    public String getNews_image() {
        return news_image;
    }

    public String getLang() {
        return lang;
    }
}

Change your MyPojo and News class like below更改您的 MyPojo 和 News 类,如下所示

 import java.util.List; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; public class Example { @SerializedName("status") @Expose private Boolean status; @SerializedName("count") @Expose private Integer count; @SerializedName("news") @Expose private List<News> news = null; public Boolean getStatus() { return status; } public void setStatus(Boolean status) { this.status = status; } public Integer getCount() { return count; } public void setCount(Integer count) { this.count = count; } public List<News> getNews() { return news; } public void setNews(List<News> news) { this.news = news; } } // News class import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; public class News { @SerializedName("news_id") @Expose private String newsId; @SerializedName("news_image") @Expose private String newsImage; @SerializedName("newss_title") @Expose private String newssTitle; @SerializedName("newss_description") @Expose private String newssDescription; @SerializedName("lang") @Expose private String lang; public String getNewsId() { return newsId; } public void setNewsId(String newsId) { this.newsId = newsId; } public String getNewsImage() { return newsImage; } public void setNewsImage(String newsImage) { this.newsImage = newsImage; } public String getNewssTitle() { return newssTitle; } public void setNewssTitle(String newssTitle) { this.newssTitle = newssTitle; } public String getNewssDescription() { return newssDescription; } public void setNewssDescription(String newssDescription) { this.newssDescription = newssDescription; } public String getLang() { return lang; } public void setLang(String lang) { this.lang = lang; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM