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!