当前位置: 首页 > 知识库问答 >
问题:

Android改装无响应

聂溪叠
2023-03-14

HI我只是新来的改造服务,并遵循本教程https://www.simplifiedcoding.net/retrofit-android-tutorial-to-get-json-from-server/它工作得很好,想创建我自己的,所以我使用了一个新的json网络http://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中实现了我的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秒延迟后,它会提示一个错误,这是公共无效失败(RetrofitError错误)我不知道我错过了什么,我检查了我的代码,没有发现任何错误,请提前帮助我谢谢。

共有1个答案

武嘉祥
2023-03-14

如果json是this,您需要一个类:

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

同时检查此链接,因为“”有问题,此错误

 类似资料:
  • 我正在建立一个新的android项目并使用改版,我的改版功能在模拟器(NOX)和邮递员中正常工作,但是当我尝试在移动设备中构建我的应用程序时,改版总是陷入失败,有人能给我解决方案吗?我的API发布在公共主机上, 这就是我所说的改装 我的回应 这是我的邮差回复

  • 我想拦截改造引擎收到的所有响应,并扫描HTTP错误代码,例如错误403。 我知道我可以使用每个请求的failure(reformationerror error)回调并检查403,但我想将响应打包为全局响应。 我可以看到请求拦截是可能的,但我看不到类似的响应选项。 有什么建议吗?

  • 我正在使用调用web服务,而reverfit会引发一个失败,来自“throwable”的消息会给我 我假设这是因为.NET web服务抛出了一个错误而没有返回JSON。但是为了证明这一点,我需要能够看到中的原始响应。有没有什么我能做到的? 这是我使用的代码

  • 问题内容: 大家好,我在android 4.3中有此代码,我现在正在使用改造,但是服务器向我抛出一条错误消息“输入内容不是有效的Base-64字符串,因为它包含非base 64字符,两个以上的填充字符,或填充字符中的非法字符。” 当我使用改造时 问题答案: 对于常规对象类型(包括),翻新将使用其来序列化值。在这种情况下,默认情况下会使用Gson将正文序列化为JSON。 为了上传要使用Base64编

  • 我知道这个问题已经被问了很多,但我所看到的答案都没有帮助我澄清这个问题。我需要更多的指导方针,而不是一个具体的答案。 错误,没有这样的用户 到目前为止,我所尝试的是为列表中收到的每个对象创建一个类: 和Api,我认为这就是问题所在: 提前感谢!!