Android
Android

Reputation: 9023

android -using retrofit lib getting error in parsing the response

I am new to Retrofit Lib.. but i read it's features so i inspired to use this..i have used This link and i am having response like following

{
   "Users":[
      {
         "firstname":"Mike",
         "lastname":"Dalisay",
         "username":"mike143"
      },
      {
         "firstname":"Jemski",
         "lastname":"Panlilios",
         "username":"jemboy09"
      },

I am also done this in interface

public void GetUser(Callback<List<Users>> respose);

But getting 04-28 07:05:13.613 32242-32242/package name I/System.out﹕ error.toString() = retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $

My Users.class

public class Users {
//    @SerializedName(value="Users")
    String firstname;
    String astname;
    String username;
    public List<Users> user;

    public void setUsers(List<Users> users) {
        this.user = users ;

    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getAstname() {
        return astname;
    }

    public void setAstname(String astname) {
        this.astname = astname;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }


}

i have edited my class by following yozzy's answer and called it like

api.GetUser(new Callback<UserResponse>() {
    @Override
    public void success(UserResponse userResponse, Response response) {
        System.out.println(response.toString());
        loading.dismiss();

    }
    public void failure(RetrofitError error) {
        System.out.println("error.toString() = " + error.toString());
    }
});

but getting retrofit.client.Response@532aa440

Upvotes: 0

Views: 104

Answers (2)

yozzy
yozzy

Reputation: 344

Your JSON response starts with a { so it's an object.

Your interface should be like this :

public void GetUser(Callback<UserResponse> response);

And your model UserResponse should be like this :

public class UserResponse implements Serializable {

   private ArrayList<User> Users;

   public ArrayList<User> getUsers() {
        return Users;
   }

   public void setUsers(ArrayList<User> Users) {
       this.Users = Users;
   }
}

And finally your User model :

public class User implements Serializable {

   private String firstname;

   public String getFirstname() {
        return firstname;
   }

   public void setFirstname(String firstname) {
       this.firstname = firstname;
   }
}

By the way Retrofit has been updated see this link : http://square.github.io/retrofit/

Upvotes: 1

Amit Gupta
Amit Gupta

Reputation: 8939

Public class Result{
private List<Users> Users;
}


public class Users {
        private String firstname;
        private String lastname;
        private String username;

        public String getFirstname() {
            return firstname;
        }

        public void setFirstname(String firstname) {
            this.firstname = firstname;
        }

        public String getLastname() {
            return lastname;
        }

        public void setLastname(String lastname) {
            this.lastname = lastname;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }
    }

And use the Result class.

public void GetUser(Callback<Result> respose);

Upvotes: 0

Related Questions