2016-07-12 6 views
5

HALLO Ich bin nur neue Service nachzurüsten und folgte diesem Tutorial https://www.simplifiedcoding.net/retrofit-android-tutorial-to-get-json-from-server/ es gut funktioniert und wollte meine eigene erstellen, so habe ich eine neue json Web http://api.androidhive.info/contacts/ dieAndroid Retrofit keine Antwort

{ 
"contacts": [ 
    { 
      "id": "c200", 
      "name": "Ravi Tamada", 
      "email": "[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 enthält meine eigene Schnittstelle implementiert ContactAPI.java

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

und umgesetzt Modellklasse wie diese 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;}} 

dann umgesetzt schließlich mein Restadapter in 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(); 
     } 
    }); 

Die App reibungslos läuft aber nach einer 4-Sekunden-Verzögerung wird es einen Fehler promt, die ich weiß nicht, public void Versagen (RetrofitError Fehler) ist das, was ich bin fehlt Ich habe meinen Code überprüft und kann nichts falsch finden. Bitte helfen Sie mir, danke im Voraus.

+0

was ist die Fehlermeldung? – Juvi

+0

den Fehler zu wissen würde sicherlich helfen. Hast du die Internet-Erlaubnis im Manifest? – Blackbelt

+0

@Juvi - das ist der Fehler, der zu den Protokollen kommt E/Surface: getSlotFromBufferLocked: unbekannter Puffer: 0xaa9ef1b0 – user3262438

Antwort

1

wenn die json dies benötigen Sie eine Klasse mit:

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; 
    } 

} 

Auch in Ihrer Schnittstelle ContactAPI.java

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

Auch diesen Link überprüfen, weil es ein Problem mit "+" ist, dies error

+1

danke für die Beantwortung Sir aber das einzige Ergebnis, dass der angegebene Code Yog gab mir "Kontakt @ 824ec91" vermisse ich etwas? – user3262438

+1

Ok Nun, ich habe den Grund, dass Sie die Contacts-Klasse bereitstellen, um eine Liste für das Objekt zu erstellen, dann muss ich meine Contact-Klasse verwenden, um die Parameter des Objekts thx zu meinem Freund aufzurufen, und Sie schneiden meine Entwicklungszeit in zwei Hälften Sie! Prost! – user3262438