0

i´m new to JSON and when trying to fetch data i'm getting the following error E/Volley: com.android.volley.ParseError: org.json.JSONException:

Edit:

Full StackTrace:

2020-11-10 20:06:48.606 10505-10505/com.madcoderz.jsonparsing E/Volley: com.android.volley.ParseError: org.json.JSONException: Value {"message":{"affenpinscher":[],"african":[],"airedale":[],"akita":[],"appenzeller":[],"australian":["shepherd"],"basenji":[],"beagle":[],"bluetick":[],"borzoi":[],"bouvier":[],"boxer":[],"brabancon":[],"briard":[],"buhund":["norwegian"],"bulldog":["boston","english","french"],"bullterrier":["staffordshire"],"cairn":[],"cattledog":["australian"],"chihuahua":[],"chow":[],"clumber":[],"cockapoo":[],"collie":["border"],"coonhound":[],"corgi":["cardigan"],"cotondetulear":[],"dachshund":[],"dalmatian":[],"dane":["great"],"deerhound":["scottish"],"dhole":[],"dingo":[],"doberman":[],"elkhound":["norwegian"],"entlebucher":[],"eskimo":[],"finnish":["lapphund"],"frise":["bichon"],"germanshepherd":[],"greyhound":["italian"],"groenendael":[],"havanese":[],"hound":["afghan","basset","blood","english","ibizan","plott","walker"],"husky":[],"keeshond":[],"kelpie":[],"komondor":[],"kuvasz":[],"labrador":[],"leonberg":[],"lhasa":[],"malamute":[],"malinois":[],"maltese":[],"mastiff":["bull","english","tibetan"],"mexicanhairless":[],"mix":[],"mountain":["bernese","swiss"],"newfoundland":[],"otterhound":[],"ovcharka":["caucasian"],"papillon":[],"pekinese":[],"pembroke":[],"pinscher":["miniature"],"pitbull":[],"pointer":["german","germanlonghair"],"pomeranian":[],"poodle":["miniature","standard","toy"],"pug":[],"puggle":[],"pyrenees":[],"redbone":[],"retriever":["chesapeake","curly","flatcoated","golden"],"ridgeback":["rhodesian"],"rottweiler":[],"saluki":[],"samoyed":[],"schipperke":[],"schnauzer":["giant","miniature"],"setter":["english","gordon","irish"],"sheepdog":["english","shetland"],"shiba":[],"shihtzu":[],"spaniel":["blenheim","brittany","cocker","irish","japanese","sussex","welsh"],"springer":["english"],"stbernard":[],"terrier":["american","australian","bedlington","border","dandie","fox","irish","kerryblue","lakeland","norfolk","norwich","patterdale","russell","scottish","sealyham","silky","tibetan","toy","westhighland","wheaten","yorkshire"],"vizsla":[],"waterdog":["spanish"],"weimaraner":[],"whippet":[],"wolfhound":["irish"]},"status":"success"} of type org.json.JSONObject cannot be converted to JSONArray

JSON Data: https://dog.ceo/api/breeds/list/all

I've been diggin a lot here at SO but can´t seem to find the correct approach

Here is the code i've writen so far:

 private void getData() {
    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Loading...");
    progressDialog.show();

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            for (int i = 0; i < response.length(); i++) {
                try {
                    JSONObject jsonObject = response.getJSONObject(i);

                    Article article = new Article();
                    article.setImage(jsonObject.getString("image"));
                    article.setTitle(jsonObject.getString("title"));
                    article.setBody(jsonObject.getString("body"));
                    articles.add(article);


                } catch (JSONException e) {
                    e.printStackTrace();
                    progressDialog.dismiss();
                }
            }
            adaptor.setData(articles);
            adaptor.notifyDataSetChanged();
            progressDialog.dismiss();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Volley", error.toString());
            progressDialog.dismiss();
        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsonArrayRequest);
}

}

5
  • 1
    Please post the full stacktrace. Also, what's the structure of the input json? Can you post a sample json? Commented Nov 10, 2020 at 23:31
  • I just edited the question Commented Nov 10, 2020 at 23:43
  • Your code expects a JSON format that way off than the JSON on stacktrace. You need to 1) confirm if that's the JSON data you want to process. 2) update the JSON parsing code to match with expected JSON data. Commented Nov 10, 2020 at 23:52
  • See this for an example: stackoverflow.com/a/64487910/607637 Commented Nov 10, 2020 at 23:53
  • Use this may be it works. JSONObject json = new JSONObject(result); JSONArray obj= json..getJSONObject("message") JSONArray affenpinscherArray=obj.getJSONArray("affenpinscher"); JSONArray africanArray=obj.getJSONArray("african"); Commented Nov 11, 2020 at 10:59

1 Answer 1

0

Te answer is that i was using the wrong URL and the JSON call was expecting a JSONArray instead of a JSONObject. Here is the code that solved the problem:

private void fetchElements(){
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET, JSON_URL,
            null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

            try {
                JSONArray breedsList = response.getJSONArray("message");
                for (int i=0; i < breedsList.length(); i++){

                    Cat cat = new Cat();
                    cat.setCatBreed(breedsList.getString(i));

                    cats.add(cat);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
            catAdapter = new CatAdapter(getApplicationContext(), cats);
            recyclerView.setAdapter(catAdapter);
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });
    requestQueue.add(objectRequest);
}
Sign up to request clarification or add additional context in comments.

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.