我无法使用recylerview获取此JSON对象数据。
{" 0":{" id":" 1","图像":" acar.jpg"," name":" maruti"," rating":" 45"}," 1":{"id":" 2"," images":" bcar.jpg"," name":" bmw","评级":" 55"}," 2":{" ID":" 3","图像":" ccar.jpg"," name":" swift","评级":" 46"}," 3":{" id":" 4"," images":" dcar.jpeg","名称":" duster"," rating":" 56"}," 4":{" id":" 5"," images":" ecar.jpg"," name":" outi"," rating":"58"},"成功":1}
fragment.class
View view = inflater.inflate(R.layout.fragment_fragment_first, container, false);
final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycleview);
RecyclerView.LayoutManager lm = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(lm);
recyclerView.setHasFixedSize(true);
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject=new JSONObject(response);
Log.e(TAG,response);
for(int i=0;i<jsonObject.length();i++){
DataModel dataModel=new DataModel();
dataModel.setName(jsonObject.getString("name"));
dataModel.setRating(jsonObject.getString("rating"));
data.add(dataModel);
adapter=new CustomAdapter(data,getContext());
}
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(stringRequest);
// Inflate the layout for this fragment
return view;
}
我的数据模型类
public class DataModel {
private String images;
private String name;
private String rating;
public DataModel(String images, String name, String rating) {
this.images = images;
this.name = name;
this.rating = rating;
}
public DataModel(){
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRating() {
return rating;
}
public void setRating(String rating) {
this.rating = rating;
}
您的解析器是错误的。而且,您还在错误的位置创建适配器(您永远不会在循环中创建适配器)。使用下面的代码。
try {
JSONObject resObject = new JSONObject(jsonString);
Iterator<String> iterator = resObject.keys();
while (iterator.hasNext()) {
JSONObject inner = resObject.getJSONArray(iterator.next());
DataModel dataModel=new DataModel();
dataModel.setName(inner.getString("name"));
dataModel.setRating(inner.getString("rating"));
data.add(dataModel);
}
adapter=new CustomAdapter(data,getContext());
recyclerView.setAdapter(adapter);
}catch (Exception e){
e.printStackTrace();
}
尝试这个,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ArrayList<DataModel> dataList=new ArrayList<DataModel>();
View view = inflater.inflate(R.layout.fragment_fragment_first, container, false);
RequestQueue rq = Volley.newRequestQueue(getActivity());
String url = "...Your URL.....";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = null;
jsonObject = new JSONObject(response);
Iterator<String> iterator = jsonObject.keys();
JSONArray jsonArray = new JSONArray();
while (iterator.hasNext()) {
String key = iterator.next();
jsonArray.put(jsonObject.get(key));
}
for (int i = 0; i < jsonArray.length()-1; i++) {
String Image=jsonArray.getJSONObject(i).getString("images");
String Name = jsonArray.getJSONObject(i).getString("name");
String Rating= jsonArray.getJSONObject(i).getString("rating");
Log.d("LOG","IMAGE :"+Image+"tNAME"+Name+"tRATING ="+Rating);
DataModel dataModel=new DataModel();
dataModel.setName(Name);
dataModel.setRating(Rating);
dataList.add(dataModel);
}
adapter=new CustomAdapter(dataList,getActivity());
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("RESPONSE", "Error [" + error + "]");
}
});
stringRequest.setRetryPolicy(new DefaultRetryPolicy(90000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
rq.add(stringRequest);
}