简体   繁体   中英

android - retrofit 2 - could not read json array and parsing it

I've this json , it returns some data and put it in a json array object:

{
"id": "58",
"p": "4297f44b13955235245b2497399d7a",
"name": "0634063306cc06340634063306cc",
"contacts": [{
    "id": "1",
    "name": "test1"
}, {
    "id": "2",
    "name": "test2"
}]
}

I want to ready the array , these are my codes:

contact class code :

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

this is the contacts class code :

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

I've wrote this interface for parsing the data :

public interface UserLogin {
@GET("/getLogin3.php")
Call<Contacts>getItems();
}

and these are the codes in my activity class:

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

it goes into on Failure method and the error is :

Expected name at line 2 column 1 path $.

what is wrong with this code ? I couldn't find it out

Try this

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

I think there is issue with data you are getting, you can check it by adding interceptor with retroift :

builder is object of OkHttpClient.Builder , you may have used in code.

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

Put that response in question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM