jijo jijo - 26 days ago 14
Java Question

JsonArray request using volley toolbox

I am trying to implement Recyclerview in Fragment but when downloading Json Data I got the error is

W/System.err: com.android.volley.ParseError: org.json.JSONException: Value {"Sales_Report": [{"id":"2","cash":"532","credit":"586","description":"lok","dates":"Nov-09- 2016","camimg_path":"Gallery_images\/Test_gl.jpeg"}, {"id":"3","cash":"214","credit":"980","description":"th","dates":"Nov-09- 2016","camimg_path":"Gallery_images\/Test_gl.jpeg"},{"id":"4","cash":"123","credit":"321","description":"erp","dates":"Nov-09-2016","camimg_path":"Gallery_images\/Test_gl.jpeg"},{"id":"15","cash":"5","credit":"8","description":"5","dates":"Nov-09-2016","camimg_path":"Gallery_images\/galimg2016-11-09T14:44:21.483+05302516.jpeg"},{"id":"14","cash":"2","credit":"3","description":"kk","dates":"Nov-09-2016","camimg_path":"..\/Gallery_images\/galimg2016-11-09T14:42:56.857+05302620.jpeg"}]} of type org.json.JSONObject cannot be converted to JSONArray
W/System.err: at com.android.volley.toolbox.JsonArrayRequest.parseNetworkResponse (JsonArrayRequest.java:55)
W/System.err: at com.android.volley.NetworkDispatcher.run (NetworkDispatcher.java:121)
W/System.err: Caused by: org.json.JSONException: Value {"Sales_Report": [{"id":"2","cash":"532","credit":"586","description":"lok","dates":"Nov-09- 2016","camimg_path":"Gallery_images\/Test_gl.jpeg"},{"id":"3","cash":"214","credit":"980","description":"th","dates":"Nov-09-2016","camimg_path":"Gallery_images\/Test_gl.jpeg"},{"id":"4","cash":"123","credit":"321","description":"erp","dates":"Nov-09-2016","camimg_path":"Gallery_images\/Test_gl.jpeg"},{"id":"15","cash":"5","credit":"8","description":"5","dates":"Nov-09-2016","camimg_path":"Gallery_images\/galimg2016-11-09T14:44:21.483+05302516.jpeg"},{"id":"14","cash":"2","credit":"3","description":"kk","dates":"Nov-09- 2016","camimg_path":"..\/Gallery_images\/galimg2016-11- 09T14:42:56.857+05302620.jpeg"}]} of type org.json.JSONObject cannot be converted to JSONArray
W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
W/System.err: at org.json.JSONArray.<init>(JSONArray.java:96)
W/System.err: at org.json.JSONArray.<init>(JSONArray.java:108)
W/System.err: at com.android.volley.toolbox.JsonArrayRequest.parseNetworkResponse (JsonArrayRequest.java:50)
W/System.err: ... 1 more


This is my fragment getting error from this page

public class MessageFragment extends Fragment {
private List<Content> listSuperHeroes;

//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;

public MessageFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_message, container, false);

recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);



//Adding adapter to recyclerview

listSuperHeroes = new ArrayList<>();

//Calling method to get data
getData();
adapter = new CardAdapter(listSuperHeroes, getActivity());
//Finally initializing our adapter
recyclerView.setAdapter(adapter);
return v;
}
private void getData(){
//Showing a progress dialog
// final ProgressDialog loading = ProgressDialog.show(getActivity(),"Loading Data", "Please wait...",false,false);

//Creating a json array request

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL,new Response.Listener<JSONArray>() {

@Override
public void onResponse(JSONArray response) {
//Dismissing progress dialog
//loading.dismiss();
//Log.d(TAG, response.toString());
//calling method to parse json array
parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();

}
});

//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());

//Adding request to the queue
requestQueue.add(jsonArrayRequest);
}

private void parseData(JSONArray array){
System.out.println("where is my parse");
for(int i = 0; i<array.length(); i++) {
Content superHero = new Content();
JSONObject json = null;
try {

json = array.getJSONObject(i);
superHero.setImageUrl(json.getString(Config.TAG_IMAGE_URL));
superHero.setName(json.getString(Config.TAG_NAME));
superHero.setRank(json.getString(Config.TAG_RANK));
superHero.setRealName(json.getString(Config.TAG_REAL_NAME));
superHero.setCreatedBy(json.getString(Config.TAG_CREATED_BY));
superHero.setFirstAppearance(json.getString (Config.TAG_FIRST_APPEARANCE));
superHero.setPowers(json.getString(Config.TAG_POWERS));
// ArrayList<String> powers = new ArrayList<String>();
//
// JSONArray jsonArray = json.getJSONArray (Config.TAG_POWERS);
//
// for(int j = 0; j<jsonArray.length(); j++){
// powers.add(((String) jsonArray.get(j))+"\n");
// }
// superHero.setPowers(powers);


} catch (JSONException e) {
e.printStackTrace();
}
listSuperHeroes.add(superHero);
}

adapter .notifyDataSetChanged();
}
}


I am using jsonArray request to download json data but it don't working

please help me to solve this error ... Thanks in advance

Answer

This code is fairly self-explanatory. Read the docs:

https://developer.android.com/training/volley/request.html

https://developer.android.com/reference/org/json/JSONArray.html

https://developer.android.com/reference/org/json/JSONObject.html

JsonObjectRequest jsObjRequest = new JsonObjectRequest
        (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

    @Override
    public void onResponse(JSONObject response) {
        JSONArray arr = response.getJSONArray("Sales_Report");
        // now you have the array. in this array you have the jsonObjects
        // iterate on this array using a for loop and parse like you did before
    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        // TODO Auto-generated method stub

    }
});