【问题标题】:Android retrofit parse nested json response in ListAndroid改造解析List中的嵌套json响应
【发布时间】:2019-02-26 15:21:36
【问题描述】:

我正在创建一个基于新闻 API 的 Android 应用程序,它使用 RecyclerView 将特定频道的新闻(比如 ABC 新闻)加载到 MainFragment 中。

我正在 MainFragment 中为此进行 API 调用,如下所示:

MainFragment.java

public class MainFragment extends Fragment
{
   protected RecyclerView recyclerView;
   protected NewsAdapter adapter;
   protected String API_KEY;
   String sourceTitle, sourceID;
   List<Articles> articleList;

public MainFragment() {

}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
{
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_main, null);

    sourceTitle = "ABC News";
    sourceID = "abc-news";

    getActivity().setTitle(sourceTitle);

    API_KEY = getString(R.string.API_KEY);

    recyclerView = root.findViewById(R.id.recyclerview);

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

    Call<News> call = apiService.getArticles(sourceID, API_KEY);

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

            if (response != null && response.isSuccessful())
            {
                articleList = response.body().getArticles();
                populateRecycleView();
            }
            else
            {
                Toast.makeText(getActivity(), "Something went wrong..", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<News> call, Throwable t)
        {
            Toast.makeText(getActivity(), "Error in API Call", Toast.LENGTH_SHORT).show();
        }
    });

    recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity().getApplicationContext(), recyclerView, new ClickListener() {
        @Override
        public void onClick(View view, int position)
        {
            //onclick code
        }

        @Override
        public void onLongClick(View view, int position) {
        }
    }));
    return root;
}

     private void populateRecycleView()
     {
         if (articleList.isEmpty() || articleList.size() == 0)
         {
            recyclerView.setAdapter(null);
            Toast.makeText(getActivity(), "Error in List", Toast.LENGTH_SHORT).show();
         }
         else
         {
            adapter = new NewsAdapter(articleList, getActivity());
            recyclerView.setAdapter(adapter);
         }
    }

在执行articleList = response.body().getArticles() 时 发生错误,并且 arraylist 仍然为空。

API 调用不会在其中加载值。

我创建了两个 Retrofit 类:APIInterface 和 APIClient,它们执行 GET API 调用:https://newsapi.org/v2/top-headlines?sources=abc-news&amp;apiKey=MY_API_KEY

APIInterface.java

  public interface ApiInterface
 {
   @GET("top-headlines")
   Call<List<News>> getArticles(@Query("sources") String source, @Query("apiKey") String apiKey);
 }

APIClient.java

 public class ApiClient
 {
  public static final String BASE_URL = "https://newsapi.org/v2/";
  private static Retrofit retrofit = null;


  public static Retrofit getClient()
  {
    if (retrofit==null)
    {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
  }
 }

我无法理解我是否在上述两个类中进行了正确的 API 调用,因为我的 arctilesList 列表和数组适配器中没有解析 JSON 数据。

应用程序在 API 调用执行中崩溃。

请注意:API 调用有效。适配器正在成功加载 API 结果。

【问题讨论】:

  • 使用新闻列表。 Call&lt;List&lt;News&gt;&gt;
  • @sajad 是的,我已经纠正了我的错误。在 API 执行期间,arralistarticleList 仍然返回 null
  • 检查互联网权限,检查你的json的java模型(搜索json2pojo寻求帮助),检查onFailure中的throwable

标签: android retrofit android-arrayadapter


【解决方案1】:

首先需要了解的是,Retrofit 的Call#enqueue() 方法是异步的。您的代码从上到下执行。同时enqueue()方法向API发起异步请求,成功则返回onResponse()方法,否则返回onFailure()方法。

那么,如何解决您的代码问题?

首先您需要为 API 响应创建 POJO 类(如果您尚未创建),如下所示。

Article.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

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 Object urlToImage;
    @SerializedName("publishedAt")
    @Expose
    private String publishedAt;
    @SerializedName("content")
    @Expose
    private String content;

    // constructors

    // getters and setter methods

    // use Alt + Insert to generate constructors, getter and setter methods in Android Studio

}

Source.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Source {

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

    // constructors

    // getters and setter methods
}

News.java

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class News {

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

    // constructors

    // getters and setter methods
}

现在在您的 MainFragment 类中进行以下更改,

public class MainFragment extends Fragment {

    // other part of the code here

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        // other part of the code here

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

                if (response != null && response.isSuccessful()) {

                    articleList = response.body().getArticles();

                    // request is successful just populate data in RecyclerView

                    populateRecyclerView(); 

                } else {

                    Toast.makeText(getActivity(), "Something went wrong...", Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFailure(Call<News> call, Throwable t) {
                Toast.makeText(getActivity(), "Error in API Call", Toast.LENGTH_SHORT).show();
            }
        });

        // just move ArrayList checking and setting adapter part code into some other method

        // other part of the code here

    }

    private void populateRecyclerView() {

        if (articleList.isEmpty() || articleList.size() == 0) {
            recyclerView.setAdapter(null);
            Toast.makeText(getActivity(), "Error in List", Toast.LENGTH_SHORT).show();
        } else {
            adapter = new NewsAdapter(articleList, getActivity());
            recyclerView.setAdapter(adapter);
        }

    }

}

不要忽略 onFailure() 方法中的 Throwable 对象。只需记录错误消息,而不是在 Toast 中显示错误消息。

Log.e("TAG", "Error occurred...", t);

通过这种方式,您可以轻松找出执行 API 请求时出现的问题。

我在答案中跳过了您的部分代码,因为它是正确的,并且使我的答案有点长。请查看我在答案中正确使用的方法名称和 cmets。

【讨论】:

    猜你喜欢
    • 2018-09-23
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多