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 .
Contactsclass?