Как получить данные в виде списка из JSON с помощью Retrofit - PullRequest
0 голосов
/ 25 декабря 2018

Одиночные данные извлекаются, но когда я пытаюсь получить их в виде списка, показывается проблема Ожидается BEGIN_ARRAY, но он был BEGIN_OBJECT в строке 1 пути 2 столбца $ вот ссылка JSON: https://newsapi.org/v2/top-headlines?sources=abc-news&apiKey=4969854e2d424ed9972370f709ace9cc

Вот мой метод получения данных JSON в моей основной деятельности

   private void loadJSON_Data() {

    RetofitClient_SingleTone 
      retofitClient_singleTone=RetofitClient_SingleTone.getInstance();


    Api api = retofitClient_singleTone.getApi();

    Call<List<Response_Pojo>> call = api.getAllResponse();

    call.enqueue(new Callback<List<Response_Pojo>>() {
        @Override
        public void onResponse(Call<List<Response_Pojo>> call, 
         Response<List<Response_Pojo>> response) {
            if (response.isSuccessful()) {

                Toast.makeText(context, "Success" + response.body().size(), 
           Toast.LENGTH_SHORT).show();
                Log.e("Home  - --", "onResponse: " + response.body());
                swipeRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onFailure(Call<List<Response_Pojo>> call, Throwable t) {
            Log.e("Home Failed - -", "Failed: " + t.getMessage());
            Toast.makeText(context, "Faield - - " +t.getMessage(), 
             Toast.LENGTH_SHORT).show();
            swipeRefreshLayout.setRefreshing(false);
        }
    });
}

Вот мой api.Class

public interface Api {

@GET("top-headlines?sources=abc-news&apiKey=4969854e2d424ed9972370f709ace9cc")
Call<List<Response_Pojo>> getAllResponse();

}

RetofitClient_SingleTone Class

public class RetofitClient_SingleTone {

  private static final String BASE_URL = "https://newsapi.org/v2/";
  private static RetofitClient_SingleTone mInstance;
  private Retrofit retrofit;

  private RetofitClient_SingleTone() {
    retrofit=new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
              .build();
 }
  public static synchronized  RetofitClient_SingleTone getInstance(){
    if (mInstance==null){
        mInstance = new RetofitClient_SingleTone();
    }
    return mInstance;
 }

 public Api getApi(){
    return retrofit.create(Api.class);
  }
}

Вот мой класс POJO

public class Response_Pojo {

@SerializedName("status")
@Expose
private String status;
@SerializedName("totalResults")
@Expose
private Integer totalResults;
@SerializedName("articles")
@Expose
private List<Article> articles = null;

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public Integer getTotalResults() {
    return totalResults;
}

public void setTotalResults(Integer totalResults) {
    this.totalResults = totalResults;
}

public List<Article> getArticles() {
    return articles;
}

public void setArticles(List<Article> articles) {
    this.articles = articles;
}
     public class Source {

    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("name")
    @Expose
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

   }

   public class Article {

    @SerializedName("source")
    @Expose
    private Source source;
    @SerializedName("author")
    @Expose
    private String author;
    @SerializedName("title")
    @Expose
    private String title;
    @SerializedName("description")
    @Expose
    private String description;
    @SerializedName("url")
    @Expose
    private String url;
    @SerializedName("urlToImage")
    @Expose
    private String urlToImage;
    @SerializedName("publishedAt")
    @Expose
    private String publishedAt;
    @SerializedName("content")
    @Expose
    private String content;

    public Source getSource() {
        return source;
    }

    public void setSource(Source source) {
        this.source = source;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrlToImage() {
        return urlToImage;
    }

    public void setUrlToImage(String urlToImage) {
        this.urlToImage = urlToImage;
    }

    public String getPublishedAt() {
        return publishedAt;
    }

    public void setPublishedAt(String publishedAt) {
        this.publishedAt = publishedAt;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

   }

   }

Ответы [ 3 ]

0 голосов
/ 25 декабря 2018

@ GET ("top-headlines? Sources = abc-news & apiKey = 4969854e2d424ed9972370f709ace9cc") Вызов getAllResponse ();

Пример модификации вы должны сделать небольшое изменение ..

0 голосов
/ 25 декабря 2018

вам нужно заменить

<List <Response_Pojo>> 

на

<Response_Pojo> 

, и если вы хотите получить список статей, вы можете получить его из response.body.getArticles () в методе обратного вызова onResponse ().

0 голосов
/ 25 декабря 2018

В интерфейсе службы API ваш ответ должен быть классом Response_Pojo вместо List<Response_Pojo>, как показано ниже:

public interface Api {

@GET("top-headlines?sources=abc-news&apiKey=4969854e2d424ed9972370f709ace9cc")
Call<Response_Pojo> getAllResponse();

}

Также необходимо соответствующим образом изменить код API вызова.

MainActivity.class:

private void loadJSON_Data() {
    RetofitClient_SingleTone 
      retofitClient_singleTone=RetofitClient_SingleTone.getInstance();


Api api = retofitClient_singleTone.getApi();

Call<Response_Pojo> call = api.getAllResponse();

call.enqueue(new Callback<Response_Pojo>() {
    @Override
    public void onResponse(Call<Response_Pojo> call, 
     Response<Response_Pojo> response) {
        if (response.isSuccessful()) {

            Toast.makeText(context, "Success" + response.body().getArticles().size(), 
       Toast.LENGTH_SHORT).show();
            Log.e("Home  - --", "onResponse: " + response.body());
            swipeRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void onFailure(Call<Response_Pojo> call, Throwable t) {
        Log.e("Home Failed - -", "Failed: " + t.getMessage());
        Toast.makeText(context, "Faield - - " +t.getMessage(), 
         Toast.LENGTH_SHORT).show();
        swipeRefreshLayout.setRefreshing(false);
    }
});
}

Пожалуйста, обновите код соответствующим образом, я не редактировал его в IDE.

Получите список из ответа, используя следующий код внутри onResponse метод:

@Override
    public void onResponse(Call<Response_Pojo> call, 
     Response<Response_Pojo> response) {
        if (response.isSuccessful()) {
          list = response.body().getArticles() // get the list from here

    }
...