Android改造解析列表中的嵌套json响应
我正在创建一个基于新闻API的Android应用程序,它使用RecyclerView将特定频道的新闻(比如ABC新闻)加载到MainFragment中 我在MainFragment中对此进行API调用,如下所示: MainFragment.javaAndroid改造解析列表中的嵌套json响应,android,retrofit,android-arrayadapter,Android,Retrofit,Android Arrayadapter,我正在创建一个基于新闻API的Android应用程序,它使用RecyclerView将特定频道的新闻(比如ABC新闻)加载到MainFragment中 我在MainFragment中对此进行API调用,如下所示: MainFragment.java public class MainFragment extends Fragment { protected RecyclerView recyclerView; protected NewsAdapter adapter; pro
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);
}
}
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调用,因为JSON数据没有在我的ArcTileList列表和数组适配器中得到解析
应用程序在API调用执行中崩溃
请注意:API调用正在工作。适配器正在成功加载API结果。您需要了解的第一件事是,改型的方法是异步的。您的代码从上到下执行。平均时间
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
}
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
}
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
}
Source.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
}
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
}
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
}
News.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
}
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
}
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
}
通过这种方式,您可以很容易地发现在执行API请求时出错的地方
我在回答中跳过了你的部分代码,因为它是正确的,并且使我的回答有点长。请仔细查看我在回答中使用的方法名称和注释。使用新闻列表<代码>呼叫@sajad是的,我已经纠正了我的错误。在API执行期间,arralist Articles列表仍然返回null检查internet权限,检查json的java模型(搜索json2pojo以获取帮助),检查onFailure中的可丢弃项