Get data in response json using Volley [duplicate]

0

I have this method that connects to an API, basically it should show me a result in a textView, but it shows nothing and I do not know why. Thanks.

   public void sendResponse(){
            String url = "http://www.amiiboapi.com/api/amiibo/";
            RequestQueue requestQueue = Volley.newRequestQueue(this);

            JsonObjectRequest jsObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        miTextView.setText(response.getString("character"));


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.d("TAG", "Error Respuesta en JSON: " + error.getMessage());

                        }
                    });
            requestQueue.add(jsObjectRequest);
        }

Después de todo esto, no me vuelve a mostrar nada en el text view:
JsonObjectRequest jsObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray jsArray = response.getJSONArray("amiibo");
                    for(int i = 0;i < jsArray.length();i++){
                        JSONObject object = jsArray.getJSONObject(i);                      
                        miTextView.setText(object.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d("TAG", "Error Respuesta en JSON: " + error.getMessage());

                    }
                });
        requestQueue.add(jsObjectRequest);
    }
    
asked by PacoPepe 17.01.2018 в 23:26
source

2 answers

0

If the API responds to the full JSON that appears in the link, then you have to modify the JSON reading.

The URL shows a JSON like this:

{
  "amiibo": [
    {
      "amiiboSeries": "Super Smash Bros.", 
      "character": "Mario", 
      "gameSeries": "Super Mario", 
      "head": "00000000", 
      "image": "https://raw.githubusercontent.com/N3evin/AmiiboAPI/master/images/icon_00000000-00000002.png", 
      "name": "Mario", 
      "release": {
        "au": "2014-11-29", 
        "eu": "2014-11-28", 
        "jp": "2014-12-06", 
        "na": "2014-11-21"
      }, 
      "tail": "00000002", 
      "type": "Figure"
    }
....

It means that the JSON is composed of a key called amiboo , followed by an Array, and within that array are the elements separately. In that structure, to reach character , you would then have to do something like this:

JSONArray amiibo=response.getJSONArray("amiibo");
//Si quieres el primero: "Mario"
JSONObject firstJSON=amiibo[0]; //En amiibo hay varios elementos
String strCharacter=firstJSON.getString("character");  
miTextView.setText(strCharacter);

When wanting to put it in a TextView , it is assumed that you are looking for a single element, then the API should have a request that allows you to filter the results.

Depending on the JSON, you will then have to search the data in it.

I hope it serves you.

    
answered by 18.01.2018 / 00:39
source
1

The answer does not have a value of character in the root, it is inside the object of an array called amiibo

{  
   "amiibo":[  
      {  
         "amiiboSeries":"Super Smash Bros.",
         "character":"Mario",
         "gameSeries":"Super Mario",
         "head":"00000000",

Remember:

to answer and depending on that remember that the answer .Json can be of two types:

  

- If the .json starts with { it is considered as a Json object.

     

- If the .json starts with [ it is considered as a Json fix.

Therefore, you must obtain the JSONArray and then search within JSONObject the element character :

public void sendResponse(){
        String url = "http://www.amiiboapi.com/api/amiibo/";
        RequestQueue requestQueue = Volley.newRequestQueue(this);

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

                    JSONArray jsonArray = response.getJSONArray("amiibo");
                    JSONObject jsonObject = jsonArray.getJSONObject(0); //0 indica el primer objeto dentro del array.
                    miTextView.setText(jsonObject.getString("character")); //Agrega valor de character a TextView.


                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d("TAG", "Error Respuesta en JSON: " + error.getMessage());

                    }
                });
        requestQueue.add(jsObjectRequest);
    }
    
answered by 18.01.2018 в 00:41