安卓Retrofit无响应

5

你好,我刚接触Retrofit服务,并按照这篇教程https://www.simplifiedcoding.net/retrofit-android-tutorial-to-get-json-from-server/进行操作,效果不错。现在想创建自己的服务,于是使用了一个新的JSON Webhttp://api.androidhive.info/contacts/

{
"contacts": [
    {
            "id": "c200",
            "name": "Ravi Tamada",
            "email": "ravi@gmail.com",
            "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": "johnny_depp@gmail.com",
            "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": "leonardo_dicaprio@gmail.com",
            "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": "john_wayne@gmail.com",
            "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": "angelina_jolie@gmail.com",
            "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": "dido@gmail.com",
            "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": "adele@gmail.com",
            "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": "hugh_jackman@gmail.com",
            "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": "will_smith@gmail.com",
            "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": "clint_eastwood@gmail.com",
            "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": "barack_obama@gmail.com",
            "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": "kate_winslet@gmail.com",
            "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": "eminem@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    }
]

我实现了自己的接口 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;}}

然后,最终在MainActivity.class中实现了我的RestAdapter。

  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秒延迟后将提示错误,该错误为public void failure(RetrofitError error),我不知道缺少什么,我检查了我的代码,但找不到任何错误,请帮助我,谢谢。


错误信息是什么? - Juvi
知道错误肯定会有帮助。您在清单文件中是否具有互联网权限? - Blackbelt
@Juvi - 这是出现在日志中的错误信息 E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaa9ef1b0 - user3262438
@Blackbelt - 是的,我在清单文件中有互联网权限,并且我的错误日志显示 E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaa9ef1b0。 - user3262438
这是一个与Android 6.0有关的已知问题,在6.0.1中已经修复。 - Juvi
1个回答

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
谢谢您的回复,但是给我提供的代码yog只给出了一个结果:"Contact@824ec91"。我有什么遗漏吗? - user3262438
1
好的,现在我明白了,你提供Contacts类的原因是为了为对象提供一个列表,然后我需要使用我的Contact类来调用对象的参数。感谢我的朋友和你,我将开发时间缩短了一半,谢谢!干杯! - user3262438

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接