Classe MainActivity:
// Download JSON file AsyncTask
new DownloadJSON().execute();
Classe DownloadJSON :
public class DownloadJSON extends AsyncTask<Void, Void, Void> {
public static ArrayList<String> mStrings = new ArrayList<>();
public static ArrayList<WorldPopulation> world= new ArrayList<>();
@Override
protected Void doInBackground(Void... params) {
// JSON file URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://www.androidbegin.com/tutorial/jsonparsetutorial.txt");
try {
// Locate the NodeList name
jsonarray = jsonobject.getJSONArray("worldpopulation");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
WorldPopulation worldpop = new WorldPopulation();
worldpop.setCountry(jsonobject.optString("country"));
world.add(worldpop);
// Populate spinner with country names
mStrings.add(jsonobject.optString("country"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
hi Micheal,
My test that does not work.
Classe MainActivity: // Download JSON file AsyncTask new DownloadJSON().execute();
Classe DownloadJSON :