我正在尝试使用volley解析json并在android的tabbed fragment类中使用recycleler-card视图进行显示。 json响应未显示在片段中。没有显示错误或异常。该应用程序运行正常,但没有显示数据。看看下面的代码,告诉我哪里出错了。
My Fragment Class
public class HomeFrag_RecyclerAdapter extends RecyclerView.Adapter<HomeFrag_RecyclerAdapter.MyHolder> {
private Context context;
List<NewsItems> newsItemsList;
public HomeFrag_RecyclerAdapter(Context context, List<NewsItems> newsItemsList) {
this.context = context;
this.newsItemsList = newsItemsList;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.tab_frags_customview,parent,false);
return new MyHolder(v);
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
holder.title.setText(newsItemsList.get(position).getNewTitle());
holder.descn.setText(newsItemsList.get(position).getNewsDesc());
holder.time.setText(newsItemsList.get(position).getNewsTime());
holder.date.setText(newsItemsList.get(position).getNewsDate());
int i = position;
Picasso.with(context).load(newsItemsList.get(i).getImage_Id())
.centerCrop()
.error(R.drawable.ic_toc_black_24dp)
.into(holder.coverImg);
}
@Override
public int getItemCount() {
return newsItemsList.size();
}
public class MyHolder extends RecyclerView.ViewHolder{
private TextView title, descn, time, date;
private ImageView coverImg;
public MyHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.news_titleTxt);
descn = (TextView) itemView.findViewById(R.id.news_DescTxt);
time = (TextView) itemView.findViewById(R.id.news_TimeTxt);
date = (TextView) itemView.findViewById(R.id.newsDateTxt);
coverImg = (ImageView) itemView.findViewById(R.id.newsImage);
}
}
}
片段适配器代码如下所示:
<pre>
{
"0": {
"image": null,
"title": "Suresh Raina to miss 2nd ODI against New Zealand ",
"time": "08:29 pm ",
"date": "18 Oct ",
"content": "Indian cricketer Suresh Raina will miss the second ODI against New Zealand at Delhi on Thursday. ...",
"link": "https://full-story.newsinshorts.com/v1/article/a659a7be-0c4d-408c-851d-689da6b95498-1 "
},
"1": {
"image": null,
"title": "Harley-Davidson net income falls by 18.67% to $114 mn ",
"time": "08:29 pm ",
"date": "18 Oct ",
"content": "Motorcycle manufacturer Harley-Davidson on Tuesday reported an 18.67% year-on-year decline in net ...",
"link": "https://full-story.newsinshorts.com/v1/article/448cc170-1d42-4109-9c50-0fe3a3c36f44-1 "
},
"2": {
"image": null,
"title": "JNU to get 200 solar power operated street lights ",
"time": "08:27 pm ",
"date": "18 Oct ",
"content": "The Jawaharlal Nehru University in Delhi is set to get 200 solar power operated street lights, Vice ...",
"link": "https://full-story.newsinshorts.com/v1/article/96db26c8-be8f-4313-8d51-27fee9bd084d-1 "
},
"3": {
"image": null,
"title": "Patiala Court dismisses fake degree case against Irani ",
"time": "08:22 pm ",
"date": "18 Oct ",
"content": "Patiala House Court today dismissed a case against Union Minister Smriti Irani for allegedly ...",
"link": null
},
</pre>
要解析的示例JSON
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.joey.mydrawerapp.Fragments.HomeFragment_Fragments.Latest_News">
<!-- TODO: Update blank fragment layout -->
<include layout="@layout/recyclerview_layout"
android:id="@+id/latestNews_recyclerView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:gravity="center"
android:textStyle="bold"
android:text="Stay Tuned For Latest News" />
</RelativeLayout>
上面的Fragment类的XML布局:
$request->input('input_file')
答案 0 :(得分:0)
我通常不再手工解析JSON,我总是使用Gson
或jackson
。如果你的json是正确的,你的onResponse
可能看起来像这样:
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response)
@Override
public void onResponse(Stringresponse) {
Gson gson = new Gson();
Map<String,Item> map = gson.fromJson(response, new TypeToken<Map<String,Item>>(){}.getType());
newsItemLists = new ArrayList(); // Clear the list if desired
for (Item item : map.values()) {
newsItems = new NewsItems();
// Copy all item attributes to newsItems here.
// if NewsItems is annotated correct, it can even be used to parse
// the json instead of the Item class below.
newsItemsList.add(newsItems);
}
// Move this line into the onCreate() initializng the adapter with an empty list.
//recyclerView.setAdapter(new HomeFrag_RecyclerAdapter(getActivity(), newsItemsList));
// add this to notify the adapter that the list has changed.
adapter.notifyDataSetChanged();
}
你需要为gson对象添加一个pojo:
class Item {
String image;
String title;
String time;
String date;
String content;
String link;
}
<强>更新强>
我已经更新了我的答案,以便制作StringRequest
排球而不是JSONRequest
。我们将使用整个字符串响应并将其解析为JSON。或者,您可以将您的Volley JsonObjectRequest
更改为JsonArrayRequest
并接收JSON数组。然后,您将循环遍历数组中的每个项目并将其解析出来。