2016-07-12 4 views
5

HI Я просто новичок в дооснастить службу и последовал за этот учебник https://www.simplifiedcoding.net/retrofit-android-tutorial-to-get-json-from-server/ это хорошо работает, и хотел создать мой собственный, так что я использовал новый JSon веб http://api.androidhive.info/contacts/, который содержитAndroid дооснащения нет ответа

{ 
"contacts": [ 
    { 
      "id": "c200", 
      "name": "Ravi Tamada", 
      "email": "r[email protected]", 
      "address": "xx-xx-xxxx,x - street, x - country", 
      "gender" : "male", 
      "phone": { 
       "mobile": "+91 0000000000", 
       "home": "00 000000", 
       "office": "00 000000" 
      } 
    }, 
    { 
      "id": "c201", 
      "name": "Johnny Depp", 
      "email": "[email protected]", 
      "address": "xx-xx-xxxx,x - street, x - country", 
      "gender" : "male", 
      "phone": { 
       "mobile": "+91 0000000000", 
       "home": "00 000000", 
       "office": "00 000000" 
      } 
    }, 
    { 
      "id": "c202", 
      "name": "Leonardo Dicaprio", 
      "email": "[email protected]", 
      "address": "xx-xx-xxxx,x - street, x - country", 
      "gender" : "male", 
      "phone": { 
       "mobile": "+91 0000000000", 
       "home": "00 000000", 
       "office": "00 000000" 
      } 
    }, 
    { 
      "id": "c203", 
      "name": "John Wayne", 
      "email": "[email protected]", 
      "address": "xx-xx-xxxx,x - street, x - country", 
      "gender" : "male", 
      "phone": { 
       "mobile": "+91 0000000000", 
       "home": "00 000000", 
       "office": "00 000000" 
      } 
    }, 
    { 
      "id": "c204", 
      "name": "Angelina Jolie", 
      "email": "[email protected]", 
      "address": "xx-xx-xxxx,x - street, x - country", 
      "gender" : "female", 
      "phone": { 
       "mobile": "+91 0000000000", 
       "home": "00 000000", 
       "office": "00 000000" 
      } 
    }, 
    { 
      "id": "c205", 
      "name": "Dido", 
      "email": "[email protected]", 
      "address": "xx-xx-xxxx,x - street, x - country", 
      "gender" : "female", 
      "phone": { 
       "mobile": "+91 0000000000", 
       "home": "00 000000", 
       "office": "00 000000" 
      } 
    }, 
    { 
      "id": "c206", 
      "name": "Adele", 
      "email": "[email protected]", 
      "address": "xx-xx-xxxx,x - street, x - country", 
      "gender" : "female", 
      "phone": { 
       "mobile": "+91 0000000000", 
       "home": "00 000000", 
       "office": "00 000000" 
      } 
    }, 
    { 
      "id": "c207", 
      "name": "Hugh Jackman", 
      "email": "[email protected]", 
      "address": "xx-xx-xxxx,x - street, x - country", 
      "gender" : "male", 
      "phone": { 
       "mobile": "+91 0000000000", 
       "home": "00 000000", 
       "office": "00 000000" 
      } 
    }, 
    { 
      "id": "c208", 
      "name": "Will Smith", 
      "email": "[email protected]", 
      "address": "xx-xx-xxxx,x - street, x - country", 
      "gender" : "male", 
      "phone": { 
       "mobile": "+91 0000000000", 
       "home": "00 000000", 
       "office": "00 000000" 
      } 
    }, 
    { 
      "id": "c209", 
      "name": "Clint Eastwood", 
      "email": "[email protected]", 
      "address": "xx-xx-xxxx,x - street, x - country", 
      "gender" : "male", 
      "phone": { 
       "mobile": "+91 0000000000", 
       "home": "00 000000", 
       "office": "00 000000" 
      } 
    }, 
    { 
      "id": "c2010", 
      "name": "Barack Obama", 
      "email": "[email protected]", 
      "address": "xx-xx-xxxx,x - street, x - country", 
      "gender" : "male", 
      "phone": { 
       "mobile": "+91 0000000000", 
       "home": "00 000000", 
       "office": "00 000000" 
      } 
    }, 
    { 
      "id": "c2011", 
      "name": "Kate Winslet", 
      "email": "[email protected]", 
      "address": "xx-xx-xxxx,x - street, x - country", 
      "gender" : "female", 
      "phone": { 
       "mobile": "+91 0000000000", 
       "home": "00 000000", 
       "office": "00 000000" 
      } 
    }, 
    { 
      "id": "c2012", 
      "name": "Eminem", 
      "email": "[email protected]", 
      "address": "xx-xx-xxxx,x - street, x - country", 
      "gender" : "male", 
      "phone": { 
       "mobile": "+91 0000000000", 
       "home": "00 000000", 
       "office": "00 000000" 
      } 
    } 
] 

I реализовать мой собственный интерфейс ContactAPI.java

public interface ContactsAPI { 
@GET("/contacts/") 
public void getContacts(Callback<List<Contact>> response);} 

И реализована модель класса как этот Contact.java

public class Contact { 

@SerializedName("id") 
@Expose 
private String id; 
@SerializedName("name") 
@Expose 
private String name; 
@SerializedName("email") 
@Expose 
private String email; 
@SerializedName("address") 
@Expose 
private String address; 
@SerializedName("gender") 
@Expose 
private String gender; 
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;} 
public String getAddress() {return address;} 
public void setAddress(String address) {this.address = address;} 
public String getGender() {return gender;} 
public void setGender(String gender) {this.gender = gender;}} 

, наконец, реализовать свой Restadapter в MainActivity.class

public static final String ROOT_URL = "http://api.androidhive.info"; 
    private ListView listView; 
    private List<Contact> contacts; 
    RestAdapter adapter = new RestAdapter.Builder().setEndpoint(ROOT_URL).build(); 
    ContactsAPI api = adapter.create(ContactsAPI.class); 

    api.getContacts(new Callback<List<Contact>>() { 
     @Override 
     public void success(List<Contact> list, Response response) { 
      Toast.makeText(MainActivity.this,list.toString(),Toast.LENGTH_SHORT).show(); 
      showList(); 
     } 

     @Override 
     public void failure(RetrofitError error) { 
      //you can handle the errors here 
      Toast.makeText(MainActivity.this,"Error Occured:"+error.toString(),Toast.LENGTH_SHORT).show(); 
     } 
    }); 

Приложение работает плавно, но уже через 4 секунды задержать это ПРОМТ ошибку, которая является общественным недействительным отказ (RetrofitError ошибка) я не знаю, что я пропущенный я проверил мой код и не могу найти что-то неправильное PLS помочь мне спасибо заранее.

+0

что сообщение об ошибке? – Juvi

+0

Знание ошибки, безусловно, поможет. У вас есть разрешение на интернет в манифесте? – Blackbelt

+0

@Juvi - это ошибка, которая приходит в журналы E/Surface: getSlotFromBufferLocked: неизвестный буфер: 0xaa9ef1b0 – user3262438

ответ

1

если JSON это вам нужен класс с:

public class Contacts { 
    @SerializedName("contacts") 
    @Expose 
    private List<Contact> contacts = new ArrayList<Contact>(); 

    /** 
    * 
    * @return 
    * The contacts 
    */ 
    public List<Contact> getContacts() { 
     return contacts; 
    } 

    /** 
    * 
    * @param contacts 
    * The contacts 
    */ 
    public void setContacts(List<Contact> contacts) { 
     this.contacts = contacts; 
    } 

} 

Также в интерфейсе ContactAPI.java

public interface ContactsAPI { 
    @GET("/contacts/") 
    public void getContacts(Callback<Contacts> response); 
} 

Также проверьте эту ссылку, потому что есть проблема с «+», это error

+1

спасибо за ответ сэра, но единственный результат, который дал мне данный код, - «Контакт @ 824ec91» Я что-то пропустил? – user3262438

+1

Ok Теперь я понимаю, почему вы предоставляете класс «Контакты», чтобы предоставить список для объекта, тогда мне нужно использовать свой класс «Контакт», чтобы вызвать параметры объекта thx моему другу, и вы сократили время разработки наполовину благодаря вы! Ура! – user3262438

Смежные вопросы