0

Link :- http://api.androidhive.info/contacts/ problem :- from the given link i am able to parse contact attribute value but not able to parse phone attribute value. Code :-

class GetGSONdata extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... params) {
            try {
                URL url = new URL("http://api.androidhive.info/contacts/");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                int responsecode = urlConnection.getResponseCode();
                Log.e("responsecode", responsecode + "");
                InputStreamReader inputStreamReader = new InputStreamReader(urlConnection.getInputStream(), "UTF-8");
                Gson gson = new Gson();
                JsonReader reader = new JsonReader(inputStreamReader);
                reader.beginObject();
                while (reader.hasNext()) {
                    String aname = reader.nextName();
                    Log.e("Array name", aname);
                    if (aname.equalsIgnoreCase("contacts")) {
                        reader.beginArray();
                        while (reader.hasNext()) {
                            Contacts contacts = gson.fromJson(reader, Contacts.class);
                            list.add(contacts);
                        }
                    }
                }
                reader.endObject();
                reader.close();
            } catch (Exception e) {
                Log.e("Exception test", e.toString());
            }

for (int i = 0; i < list.size(); i++) {
                Log.e("json data", list.get(i).getId() + " " + list.get(i).getName());

            }
            return null;
        }
    }


    public class Contacts {
        private String id;
        private String name;
        private String email;

        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 getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        class Phone{
            private String mobile;
            private String home;
            private String office;

            public String getMobile() {
                return mobile;
            }

            public void setMobile(String mobile) {
                this.mobile = mobile;
            }

            public String getHome() {
                return home;
            }

            public void setHome(String home) {
                this.home = home;
            }

            public String getOffice() {
                return office;
            }

            public void setOffice(String office) {
                this.office = office;
            }
        }

    }

Thanks .

3
  • Could you please post your Contacts class? Commented Nov 2, 2015 at 5:04
  • @PauloAvelar yes, i have updated code. Thanks Commented Nov 2, 2015 at 5:11
  • Keep the Phone class as a separate class and add a getter method for Phone in your Contacts class, since phone is a separate object inside Contacts object. So, getPhone will return you the phone object entirely. Hope this helps Commented Nov 2, 2015 at 5:13

3 Answers 3

1

Have an instance of Phone class in your Contacts class.

public class Contacts{
   private Phone phone;  
   private String id;
   private String name;
   private String email;
   private String address;
   private String gender;

   // Getters & Setters

}  
public class Phone{
        private String mobile;
        private String home;
        private String office;

       // Getters & Setters
  }

This should work.

Sign up to request clarification or add additional context in comments.

Comments

1
        result  // result is string Json data which is get from sever(Async Task post Execute result)

            Gson gson = new Gson();

            Reader reader;

            try {
                reader = new InputStreamReader(new ByteArrayInputStream(
                        result.getBytes("UTF-8")));
                Type Collectiontype = new TypeToken<Contact>() {
                }.getType();
        Contact lstContact = gson.fromJson(reader, Collectiontype);
        //Contact is pojo class 

Use this link for create pojo file and then refer this code

1 Comment

if it not work then change two line of code reader = new InputStreamReader(new ByteArrayInputStream( result.getBytes("UTF-8"))); Type Collectiontype = new TypeToken<ArrayList<Contact>>() { }.getType(); ArrayList<Contact> lstContact = gson.fromJson(reader, Collectiontype);
0

I would recommend using this Website to create your POJO classes so you do not face any issues in the future http://www.jsonschema2pojo.org/

2 Comments

@N jay Thanks, very use-full link
No worries @pankaj gupta feel free to mark as answer if you believe it helped u.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.