0

jsondata:

[{"product_id":"2","product_title":"test","product_thumbnail":"","product_description":"test","product_place":"","product_user":"1","product_store":"","product_date":"1546875653","product_cost":"","product_off":""},{"product_id":"3","product_title":"test1","product_thumbnail":"","product_description":"test1","product_place":"","product_user":"1","product_store":"","product_date":"1546875653","product_cost":"","product_off":""}]

I get this error:

Rest response error:: com.android.volley.ParseError: org.json.JSONException: Value [{"product_id":"2","product_title":"test","product_thumbnail":"","product_description":"test","product_place":"","product_user":"1","product_store":"","product_date":"1546875653","product_cost":"","product_off":""},{"product_id":"3","product_title":"test1","product_thumbnail":"","product_description":"test1","product_place":"","product_user":"1","product_store":"","product_date":"1546875653","product_cost":"","product_off":""}] of type org.json.JSONArray cannot be converted to JSONObject

my android code:

_httpHandler.addRequest(Request.Method.GET, "product", null, response -> {
        try {
            Log.e("response is:",response.toString());

            for (int i = 0; i < response.length(); i++) {
                JSONObject product = response.getJSONObject(String.valueOf(i));

                productList.add(new Product(
                        product.getString("product_id"),
                        product.getString("product_title"),
                        product.getString("product_thumbnail"),
                        product.getString("product_description"),
                        product.getString("product_place"),
                        product.getString("product_user"),
                        product.getString("product_store"),
                        product.getString("product_date"),
                        product.getString("product_off"),
                        product.getString("product_price")));
            }

            ProductAdapter adapter = new ProductAdapter(MainActivity.this, productList);
            recyclerView.setAdapter(adapter);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }, error -> {
        Toast.makeText(MainActivity.this, "Get Product Has Error", Toast.LENGTH_LONG).show();
        Log.e("Rest response error: ", error.toString());
    });

and AddRequest function is:

public void addRequest(int method, String url, JSONObject jsonRequest,
                       Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
    url = api_url + url;
    Log.e("url is: ", url);
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(method, url, jsonRequest, listener, errorListener){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type","application/x-www-form-urlencoded");
            params.put("Accept", "application/json");
            return params;
        }
    };
    requestQueue.add(jsonObjectRequest);}

Can you help me to fix it?

4 Answers 4

1

Try this it will help you

Here you need to get your JSONArray from your response

like this

try {
            JSONArray json = new JSONArray(response);

            for (int i = 0; i < json.length(); i++) {

                JSONObject product= json.getJSONObject(i);
                Log.e("json 1", product.getString("product_title"));

                productList.add(new Product(
                        product.getString("product_id"),
                        product.getString("product_title"),
                        product.getString("product_thumbnail"),
                        product.getString("product_description"),
                        product.getString("product_place"),
                        product.getString("product_user"),
                        product.getString("product_store"),
                        product.getString("product_date"),
                        product.getString("product_off"),
                        product.getString("product_price")));

            }
        ProductAdapter adapter = new ProductAdapter(MainActivity.this, productList);
        recyclerView.setAdapter(adapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

Let me know if it works for you. or mark it helpful if it works for you.

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

4 Comments

No I have another json url that work correct!!!! I think json has problem!?
it will work for you definatly as per your current response because your response has json Array at the top
If you have json Object at top then you get json object first and then get json array from your object
thanks a lot. "json_encode($array)" changed to "json_encode($array, JSON_FORCE_OBJECT)", the problem was fixed. Thank you for your guide
1

You response is JSONArray but you are executing JsonObjectRequest so your Response.Listener is alos expecting JSONObject. That's why volley can't cast JSONArray to JSONObject. Use JsonArrayRequest. So that your Response.Listener will expect JSONArray

Comments

0

try This

productList = new Gson().fromJson(response.toString(),new TypeToken<ArrayList<Category>>() {
    }.getType());

Comments

0

The error comes from the fact that you expect in response a JSONArray as result of your HTTP request but you make a JsonObjectRequest which has a JSOnObject parameter in the OnResponse listener

What you have to do is:

  • Make a JsonArrayRequest

As so, you will pass a JSONArray to the OnResponse listener as your parameter.

This link is very helpful because it provides an: android - Volley JSON Array request example

At the end, your AddRequest function should be something like this:

public void addRequest(int method, String url, JSONArray jsonRequest,
                   Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) {
url = api_url + url;
Log.e("url is: ", url);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(method, url, jsonRequest, listener, errorListener){
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> params = new HashMap<String, String>();
        params.put("Content-Type","application/x-www-form-urlencoded");
        params.put("Accept", "application/json");
        return params;
    }
};
requestQueue.add(jsonArrayRequest);}

Comments

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.