1

I am using Volley to request unsplash API but when I try requesting it as JsonObjectRequest it doesn't give me any errors but I know that is a wrong approach because the data I am receiving is JSONArray

MY APPROACH

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
            Request.Method.GET,
            URL,
            null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    if(response!=null){
                    // Process the JSON
                        try{
                            // Loop through the array elements
                            for (int i = 0; i < response.length(); i++) {
                                JSONObject js = response.getJSONObject(i);
                                Log.d("TESTING",js.getString("id"));
                            }
                        p.dismiss();
                    }catch (JSONException e){
                        e.printStackTrace();
                    }
                }}
            },
            new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError error){
                    Log.d("TESTING",error.getMessage());
                }
            });
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);

LOG Since the JSONArray is too big to post here this is the formate

D/TESTING: org.json.JSONException: Value {"total": 44760,
"total_pages": 4476,
"results":[{JSONObjects}]}

If you wish to view the JSONArray here is the link "https://api.unsplash.com/search/photos?query=wood&client_id=ACESS_KEY_REQUIRED"

also I have viewed this question but that is totally different

You can request a key simply by making an account here.

2 Answers 2

1

You need to create jsonObject first. You requested for JSONArray but your server response as JSONObject. Try StringRequest for getting the JSONObject.

Try this:

    StringRequest stringRequest = new StringRequest(Request.Method.GET,
            URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {
                        // Loop through the array elements
                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray jsonArray = jsonObject.getJSONArray("results");
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject js = jsonArray.getJSONObject(i);
                            Log.d("TESTING", js.getString("id"));
                        }
                        p.dismiss();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
             Log.d("TESTING",error.getMessage()); 
        }
    })
};

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);

Hope this will work.

Sign up to request clarification or add additional context in comments.

3 Comments

JSONObject jsonObject = new JSONObject(response); Cannot resolve constructor 'JSONObject(org.json.JSONArray)'
It worked thank you soo much ! but in every object of result there are fields such as "id", "height" etc and "urls" now these urls are like : "urls": {"raw":"data", "full":"data"......} Any idea how to get these fields of "raw" and "full"
I did it Thank You again for the time and suggestions
0

You are request for json object but request time you call jsonarrayrequest so JsonException is come

    JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,url,
            null, new Response.Listener<JSONObject>() {
   @Override
   public void onResponse(JSONObject response) {
       try {
           JSONArray jsonArray = response.getJSONArray("results");
            for (int i = 0; i < jsonArray.length(); i++) {
              JSONObject js = jsonArray.getJSONObject(i);
               Log.d("TESTING",js.getString("id"));
}
       } catch (JSONException e) {
           e.printStackTrace();
       }
   }
}, new Response.ErrorListener() {
   @Override
   public void onErrorResponse(VolleyError error) {
       VolleyLog.e("Error: ", error.getMessage());
   }
});

5 Comments

It's not working Logcat: " E/Volley: [1] 8.onErrorResponse: Error: "
you have get method i post for Post method PLease change it as per my edit
Your answer is also using GET but I tried using POST, same error
please check your api
If my API was wrong why would I get a JSONArray in my Exception ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.