0

Hi i am trying to get the json response which has attribute with jsonarray

like (ex: {A:one,B:[{a:one,b:two},{a:two,b:one}]}) i have trying to get

the a:one and b:two values only. But my logcat says error:

  1. RecyclerView: No adapter attached; skipping layout
  2. Json parsing error: Value custom_attributes of type java.lang.String cannot be converted to JSONArray

i want to get this values in textview for my product detail view...

My Coding is :

private class GetProductDetails extends AsyncTask<Void, Void, Void> {



        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            dialog_pro = new ProgressDialog(Product_Detail_Activity.this);
            dialog_pro.setMessage("Please wait...");
            dialog_pro.setCancelable(false);
            dialog_pro.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(BaseURL);

            //Log.e(TAG, "Response from url: " + jsonStr);

            if (jsonStr != null) {
                try {
                     JSONObject jsonObj = new JSONObject(jsonStr);

                    name = jsonObj.getString("name");


                    JSONArray items = new JSONArray("custom_attributes");
                    for (int i = 0; i < items.length(); i++) {
                        JSONObject c = items.getJSONObject(i);

                        String atrr = c.getString("attribute_code");

                        if(atrr.equalsIgnoreCase("short_description")) {

                            des = c.getString("value");
                        }

                    }


                }catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    Toast.LENGTH_LONG)
                                    .show();
                          //  pro_name.setText("Json error:" + e.getMessage());

                        }
                    });

                }
            } else {
                //Log.e(TAG, "Couldn't get json from server.");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Couldn't get json from server. Check LogCat for possible errors!",
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });
                // AppController.getPermission().addToRequestQueue(jsonObj);
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            /**
             * Updating parsed JSON data into ListView
             * */
            if (dialog_pro.isShowing())
                dialog_pro.dismiss();
            pro_name.setText(name);
            short_desc.setText(des);
        }

    }
1
  • share your complete JSON response Commented Jul 21, 2017 at 8:44

2 Answers 2

1

Being based on your JSON

try {
            JSONObject responseObject = new JSONObject(response);
            String A= responseObject.getString("A");
            JSONArray bArray= responseObject.getJSONArray("B");
            for(int i=0;i<bArray.length();i++){
                JSONObject innerObject=bArray.getJSONObject(i);
                String a= innerObject.getString("a");
                String b= innerObject.getString("b");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

In your code you have JSONArray items = new JSONArray("custom_attributes"); should be changed. You should get your custom_attributes array from the jsonObj object using object.getJSONArray().

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

Comments

0

For your first problem-RecyclerView: No adapter attached; skipping layout-> Just set an empty adapter first, update it as soon as you have the data (this is what works for me) For your second problem-Json parsing error: Value custom_attributes of type java.lang.String cannot be converted to JSONArray->

The e.g json that you have posted "{A:one,B:[{a:one,b:two},{a:two,b:one}]}" it is not in valid json format

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.