我需要从该URL https://newsapi.org/v1/articles?apiKey=6946d0c07a1c4555a4186bfcade76398&sortBy=top&source=bbc-news解析数据,但是当我进行apicall操作时,它永远不会进入onResponse函数,但始终会进入onFailure。
始终会收到onFailure中的“错误”吐司。

我尝试将BASE_URL和Query更改为几个不同的选项,但这无济于事。也许这是我的物体有问题,但是我找不到错误。

*****更新*****:我像注释中所说的那样更改了我的对象,但是仍然获得onFailure函数。

MainActivity.java

public class MainActivity extends AppCompatActivity implements
OnItemClickListener{

public static final String EXTRA_TITLE="Title";
public static final String EXTRA_DESCRIPTION="Description";
public static final String EXTRA_URL="URL";


private RecyclerView recycler;
private RecyclerAdapter adapter;
private ArrayList<NewsCell> newsList;
private Call<List<NewsCell>> apiCall;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    newsList=new ArrayList<>();
    setupRecycler();
    setUpApiCall();


}

private void setUpApiCall(){
 apiCall=NetworkUtils.getApiInterface().getNews(
             "6946d0c07a1c4555a4186bfcade76398","top","bbc-news");
 apiCall.enqueue(new Callback<List<NewsCell>>() {
        @Override
        public void onResponse(Call<List<NewsCell>> call,
Response<List<NewsCell>> response) {
            if(response.isSuccessful() && response.body().size()!=0){
                newsList.addAll(response.body());
                adapter=new
RecyclerAdapter(MainActivity.this,newsList,MainActivity.this);
                recycler.setAdapter(adapter);
            }
            else{
                Toast.makeText(MainActivity.this, "No results",
Toast.LENGTH_SHORT).show();

            }
        }

        @Override
        public void onFailure(Call<List<NewsCell>> call, Throwable t) {
            Toast.makeText(MainActivity.this, "Error",
Toast.LENGTH_SHORT).show();

        }
    });

}

private void setupRecycler() {
    recycler=findViewById(R.id.recyclerView);
    recycler.setLayoutManager(new LinearLayoutManager(this));
}

@Override
public void onItemClick(int position) {
    Intent detailIntent=new Intent(this,NewsSingle.class);
    NewsCell clickedItem=newsList.get(position);

    detailIntent.putExtra(EXTRA_TITLE,clickedItem.getTitle());
    detailIntent.putExtra(EXTRA_DESCRIPTION,clickedItem.getDescription());
    detailIntent.putExtra(EXTRA_URL,clickedItem.getUrlToImage());

    startActivity(detailIntent);
}

}


NetvorkUtils.java

public class NetworkUtils {
private static final String BASE_API="https://newsapi.org/v1/";
private static APIInterface apiInterface;

public static APIInterface getApiInterface(){
    if(apiInterface==null){
        Retrofit retrofit =new Retrofit.Builder()
                .baseUrl(BASE_API)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        apiInterface=retrofit.create(APIInterface.class);
    }
    return apiInterface;
}
}


APIInterface.java

public interface APIInterface {
@GET("articles")
Call<List<NewsCell>> getNews(@Query("apiKey") String key, @Query("sortBy")
String sort,@Query("source") String source);
}


Article.java

public class Article {

@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;

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;
}

}


NewsCell.java

public class NewsCell {

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

public String getStatus() {
    return status;
}

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

public String getSource() {
    return source;
}

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

public String getSortBy() {
    return sortBy;
}

public void setSortBy(String sortBy) {
    this.sortBy = sortBy;
}

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

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

}

最佳答案

您正在得到这样的响应java - APICall响应无法解析数据-LMLPHP

但是在模型类NewsCell中,您只解析title ,description and urlToImage数组中的articles

@SerializedName("title")
@Expose
private String title;
@SerializedName("description")
@Expose
private String description;
@SerializedName("urlToImage")
@Expose
private String urlToImage;


因此,我建议您复制整个答复。 goto this site并正确生成模型类。并粘贴到您的NewsCell

10-08 11:44