简体   繁体   中英

Android Studio - Issue loading JSON

I'm using Android Studio and I want to make a listview, which contains values that are received by JSON.

 protected Void doInBackground(Void... voids) {
    HttpHandler Handler = new HttpHandler();
    String JSONString = Handler.makeServiceCall(JSONUrl);
    Log.e(TAG, "Response:" + JSONString);

    if(JSONString != null){
        try {
            JSONObject CountriesJSONObject = new JSONObject(JSONString);

            JSONArray Countries = CountriesJSONObject.getJSONArray("countries");

            for (int i = 1; i < Countries.length(); i++) {
                JSONObject Country = Countries.getJSONObject(i);

                //Details
                String CountryID = Country.getString("id");
                String CountryName = Country.getString("name");
                String CountryImage = Country.getString("image");

                //Hashmap
                HashMap<String, String> TempCountry = new HashMap<>();

                //Details to Hashmap
                TempCountry.put("id", CountryID);
                TempCountry.put("name", CountryName);
                TempCountry.put("image", CountryImage);

                //Hashmap to Countrylist
                CountryList.add(TempCountry);
            }
        } catch (final JSONException e){
            Log.e(TAG,e.getMessage());
            ProgressDialog.setMessage("Error loading Data!");

    }

    }
        return null;
    }

This is the code for getting the JSON values, and i'm receiving an error "No value for id" What am I doing wrong?

You still have the "country" key to unwrap. Try like this:

for (int i = 1; i < Countries.length(); i++) {
                JSONObject Country = Countries.getJSONObject(i).getJSONObject("country");

                //Details
                String CountryID = Country.getString("id");
                String CountryName = Country.getString("name");
                String CountryImage = Country.getString("image");

                //Hashmap
                HashMap<String, String> TempCountry = new HashMap<>();

                //Details to Hashmap
                TempCountry.put("id", CountryID);
                TempCountry.put("name", CountryName);
                TempCountry.put("image", CountryImage);

                //Hashmap to Countrylist
                CountryList.add(TempCountry);
            }

First step is to create a new Java class model for the JSON - you can just copy and paste this.

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;

public class Countries {
    public class CountriesList implements Serializable {
        private Country[] countries;

        public Country[] getCountries() {
            return countries;
        }

        public void setCountries(Country[] countries) {
            this.countries = countries;
        }

        public ArrayList<Country> getCountriesAsList() {
            if(countries == null || countries.length == 0) {
                return new ArrayList<>();
            } else {
                return (ArrayList<Country>) Arrays.asList(countries);
            }
        }
    }

    public class Country implements Serializable {
        private String id;
        private String name;
        private String image;

        public Country() {
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }
    }
}

Now, it's simply converting the JSON into Java object like this. You can use that ArrayList for adapter or however you like.

protected Void doInBackground(Void... voids) {
    HttpHandler Handler = new HttpHandler();
    String jsonString = Handler.makeServiceCall(JSONUrl);

    Countries.CountriesList countries = new Gson().fromJson(jsonString, Countries.CountriesList.class);


    // this is the full list of all your countries form json
    ArrayList<Countries.Country> countryList = countries.getCountriesAsList();

}

Note: you will need the Gson library to use the solution I showed above. I use that to convert JSON into Java object.

compile 'com.google.code.gson:gson:2.8.0'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM