How popular is a Spinner from data returned from a URL?

1

I have the following code that works fine, but with an example of array :

@Override
protected Boolean doInBackground(String...urls) {

    try {
        Log.e("****** MESSAGE ******", " Json Object  = " + JSONParser.getJSONFromUrl(URL).get("ReportDetailTextList"));
        //  
        //  por favor note que, já consigo pegar os JSON da URL sem problemas! como no exemplo: <br>
        // REOTORNO DO JSON:
        // {"ReportRetryNumber":4,"ReportDetailTextList":["alfa","beta","gama","yota","zeta"]}

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


// TEST: Temporaly list
List < String > categories = new ArrayList < String > ();
categories.add("aaa");
categories.add("bbb");
categories.add("ccc");
categories.add("ddd");
categories.add("eee");
// Adapter Creation
dataAdapter = new ArrayAdapter < String > (getBaseContext(), android.R.layout.simple_spinner_dropdown_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

return false;
}

I tried converting to work with JSON and my code looks like this:

@Override
protected Boolean doInBackground(String...urls) {

    try {
        Log.e("****** MESSAGE ******", " Json Object  = " + JSONParser.getJSONFromUrl(URL).get("ReportDetailTextList"));
        //  
        //  por favor note que, já consigo pegar os JSON da URL sem problemas! como no exemplo: <br>
        // REOTORNO DO JSON:
        // {"ReportRetryNumber":4,"ReportDetailTextList":["alfa","beta","gama","yota","zeta"]}



        List < JSONObject > categories = new ArrayList < > ();

        jsonarray = (JSONArray) JSONParser.getJSONFromUrl(URL).get("ReportDetailTextList");
        for (int i = 0; i < jsonarray.length(); i++) {

          jsonobject = jsonarray.getJSONObject(i);

          categories.add(jsonobject);

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

    // Adapter Creation
    dataAdapter = new ArrayAdapter < String > (getBaseContext(), android.R.layout.simple_spinner_dropdown_item, categories);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    return false;
}

It turns out that I am not able to put the URL data in my Spinner ,

For example, the line:

  

dataAdapter = new ArrayAdapter (getBaseContext (), android.R.layout.simple_spinner_dropdown_item, categories);

variable ... >

  • 1) How to correctly read json?
  • 2) How to put the data in the spinner?

Thank you!

    
asked by anonymous 31.01.2017 / 03:34

1 answer

2

Instead of defining your list as a <JSONObject> model, you will switch to String , which in this case would be a list of strings .

List<String> categories = new ArrayList<>();

Just below you at first do this, to verify that you are listing the data from the structure returned from the server correctly. See:

String str = "{\"ReportRetryNumber\":4,\"ReportDetailTextList\":[\"alfa\",\"beta\",\"gama\",\"yota\",\"zeta\"]}";

JSONObject jsonObj = new JSONObject(str);
JSONArray array = jsonObj.getJSONArray("ReportDetailTextList");

for(int i = 0; i<array.length(); i++){
    categories.add(array.get(i).toString());
}

To finish, if your JSONParser.getJSONFromUrl( URL ).get("ReportDetailTextList"); is actually returning an array, just do it this way below:

JSONArray array = (JSONArray) JSONParser.getJSONFromUrl( URL ).get("ReportDetailTextList");

The complete code within your try catch should look like this below:

try{
    List<String> categories = new ArrayList<>();
    JSONArray array = (JSONArray) JSONParser.getJSONFromUrl( URL ).get("ReportDetailTextList");

    for(int i = 0; i<array.length(); i++){
        categories.add(array.get(i).toString());
    }
} catch (JSONException e) {
    e.printStackTrace();
}
    
31.01.2017 / 04:26