我刚刚开始学习java和Android,我试图解析json数据并将数据应用到recyclerview但我无法做到。这是我的代码
public void JSON_DATA_WEB_CALL(){
jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
progressBar.setVisibility(View.INVISIBLE);
JSON_PARSE_DATA_AFTER_WEBCALL(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){
for(int i = 0; i<array.length(); i++) {
GetDataAdapter GetDataAdapter2 = new GetDataAdapter();
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetDataAdapter2.setImageTitleNamee(json.getString(JSON_IMAGE_TITLE_NAME));
//GetDataAdapter2.setImageServerLarger(json.getString(JSON_IMAGE_LARGER));
GetDataAdapter2.setImageServerUrl(json.getString(JSON_IMAGE_URL));
GetDataAdapter2.setMrp_price(json.getString(JSON_MRP_PRICE));
GetDataAdapter2.setDisc_price(json.getString(JSON_DISC_PRICE));
} catch (JSONException e) {
e.printStackTrace();
}
GetDataAdapter1.add(GetDataAdapter2);
}
recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);
recyclerView.setAdapter(recyclerViewadapter);
}
这是我的JSON响应
{"118":{"garment_color":"Blue","garment_name":"skjhkds","garment_price":"232"},"119":{"garment_color":"hjsadjjs","garment_name":"sdasd","garment_price":"23478"}}
请有人简单解释一下正确的代码。这将非常有帮助。感谢
答案 0 :(得分:0)
rvAdapter = new RvAdapter(getActivity());
recyclerview.setAdapter(rvAdapterHScode);
rvAdapter.set(responce.getcodes());
recyclerview.setLayoutManager(new GridLayoutManager(getActivity(), 3));
然后在适配器中 接受设定值 或
在recyclerview适配器中调用您的响应
答案 1 :(得分:0)
like this way 你的jsonArray应该遵循以下方式:
{
"jArray": [{
"id": "118",
"garment_color": "Blue",
"garment_name": "skjhkds",
"garment_price": "232"
},
{
"id": "119",
"garment_color": "hjsadjjs",
"garment_name": "sdasd",
"garment_price": "23478"
}
]
}
然后像这样读取json:
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("jArray");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
try {
GetDataAdapter2.setImageTitleNamee(c.getString("garment_name"));
GetDataAdapter2.setMrp_price(c.getString("garment_price"));
} catch (JSONException e) {
e.printStackTrace();
}
GetDataAdapter1.add(GetDataAdapter2);
}
recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);
recyclerView.setAdapter(recyclerViewadapter);