我有这个json,它返回一些数据并将其放在json数组对象中:
{
"id": "58",
"p": "4297f44b13955235245b2497399d7a",
"name": "0634063306cc06340634063306cc",
"contacts": [{
"id": "1",
"name": "test1"
}, {
"id": "2",
"name": "test2"
}]
}
我想准备数组,这是我的代码:
联系方式代码:
public class Contact {
@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Contact withId(String id) {
this.id = id;
return this;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Contact withName(String name) {
this.name = name;
return this;
}
}
这是联系人类代码:
public class Contacts {
@SerializedName("id")
@Expose
private String id;
@SerializedName("p")
@Expose
private String p;
@SerializedName("name")
@Expose
private String name;
@SerializedName("contacts")
@Expose
private List<Contact> contacts = null;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Contacts withId(String id) {
this.id = id;
return this;
}
public String getP() {
return p;
}
public void setP(String p) {
this.p = p;
}
public Contacts withP(String p) {
this.p = p;
return this;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Contacts withName(String name) {
this.name = name;
return this;
}
public List<Contact> getContacts() {
return contacts;
}
public void setContacts(List<Contact> contacts) {
this.contacts = contacts;
}
public Contacts withContacts(List<Contact> contacts) {
this.contacts = contacts;
return this;
}
}
我已经写了这个接口来解析数据:
public interface UserLogin {
@GET("/getLogin3.php")
Call<Contacts>getItems();
}
这些是我的活动课中的代码:
Call<Contacts>rows=connection.getItems();
rows.enqueue(new Callback<Contacts>() {
@Override
public void onResponse(Call<Contacts> call, Response<Contacts> response) {
Log.v("this", "id" + response.body().getId());
Log.v("this", "name" + response.body().getName());
Contacts simpleItem = response.body();
List<Contact>contacts=simpleItem.getContacts();
for (int i=0; i<contacts.size();i++){
Log.v("this","result "+ contacts.get(i).getId()+ " - "
+ contacts.get(i).getName());
}
}
@Override
public void onFailure(Call<Contacts> call, Throwable t) {
Log.v("this",t.getMessage());
}
});
它进入失败方法,错误是:
Expected name at line 2 column 1 path $.
此代码有什么问题?我找不到
答案 0 :(得分:0)
尝试一下
Call<Contacts>rows=connection.getItems();
rows.enqueue(new Callback<Contacts>() {
@Override
public void onResponse(Call<Contacts> call, Response<Contacts> response) {
if (response.isSuccessful())
if (response.body() != null)
// Contacts simpleItem = response.body();
List<Contact>contacts=response.body().getContacts()
for (int i=0; i<contacts.size();i++){
Log.v("this","result "+ contacts.get(i).getId()+ " - "
+ contacts.get(i).getName());
}
}
@Override
public void onFailure(Call<Contacts> call, Throwable t) {
Log.v("this",t.getMessage());
}
});
答案 1 :(得分:0)
我认为您获取的数据存在问题,您可以通过添加带有Retroift的拦截器来进行检查:
builder
是OkHttpClient.Builder
的对象,您可能已在代码中使用过。
builder.interceptors().add(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
request = request.newBuilder()
.url(request.url())
.build();
okhttp3.Response response = chain.proceed(request);
// Check the responce here
Log.i("Response ",response+"");
return response;
}
});
放入有问题的答案。