我已经将JSonarray数据作为字符串获取并将其添加到arraylist中,即 这是在选项卡片段回收站视图上完成的。
JSONObject jsonObject = response.getJSONObject(index);
// Log.d("TAG", jsonObject.getString("title"));
int id =jsonObject.getInt("id");
String title=jsonObject.getString("title");
// Log.d("TAG", jsonObject.getString("description"));
String shortDec=jsonObject.getString("description");
String longDec=jsonObject.getString("short_description");
String imguUrl=jsonObject.getString("image");
String createdAt=jsonObject.getString("created_at");
// Toast.makeText(NewsDetailsActivity.this, "\nTitle: "+title+"\n Description:", Toast.LENGTH_SHORT).show();
NewsListGetter newsListGetter=new NewsListGetter(id, title, shortDec,longDec,imguUrl,createdAt);
arrayList.add(newsListGetter);
adapter.notifyDataSetChanged();
我有另一项名为NewsDetailsAcivity的活动,其中我实施了一个回收者视图。 我想只将被点击的tab1fragment的recylerview中的数据传递给newsDetailActivity recylerview。
答案 0 :(得分:1)
您的响应已经在JSONArray中,无需存储在另一个JSON数组中。
请看下面的代码。
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "http://ec2-54-147-238-136.compute-1.amazonaws.com/hmc/api/getnewsfeeds?order=asc";
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int index = 0; index < response.length(); index++) {
try {
JSONObject jsonObject = response.getJSONObject(index);
Log.d("TAG", jsonObject.getString("title"));
Log.d("TAG", jsonObject.getString("description"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
答案 1 :(得分:0)
首先在String中使用Response.Listener,如
new Response.Listener<String>
然后将您的响应存储在JSONObject中,然后从存储的JSONObject中获取JSONArray。像这样:
JSONObject data = new JSONObject(response);
JSONArray responseArray = data.getJSONArray("data"); // here data is Key of your JsonArray.
希望这能帮到你!
答案 2 :(得分:0)
根据您的JSON响应您可以粘贴此代码。我在我的机器上尝试了这个并且正在工作,如果有任何问题,请告诉我
String json_url = "http://ec2-54-147-238-136.compute-.amazonaws.com/hmc/api/getnewsfeeds?order=asc";
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jObj = response.getJSONObject(i);
Log.d("TAG", jObj.getString("title"));
Log.d("TAG", jObj.getString("description"));
} catch (JSONException exc) {
exc.printStackTrace();
}
}
}
}
如何在ArrayList中保存数据
创建SampleModelClass
public class SampleModelClass {
private String id;
private String description;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
现在在您的活动中解析JSON,执行相同的
JSONArray root_array = root_obj.getJSONArray("YOUR_ARRAY_NODE_NAME");
for (int i = 0; i < root_array.length(); i++) {
SampleModelClass commonModels = new SampleModelClass();
JSONObject array_object = root_array.getJSONObject(i);
commonModels.setId(array_object.optString("id"));
commonModels.setDescription("description");
common_stop_list.add(commonModels);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();