0

I've a get request that return:

[{"user_id":"1","username":"user1","name":"Name1","profile_pic":"user1.jpg"},
{"user_id":"2","username":"user2","name":"Name2","profile_pic":"user2.jpg"},
{"user_id":"3","username":"user3","name":"Name3","profile_pic":"user3.jpg"}]

I want to create a Retrofit project that do that call so I created:

public interface ApiServiceUsers {
    @GET("/api/users")
    void getUsers(Callback<PojoUsers> callback);
    }

    public class PojoUsers {
    private String user_id;
    private String username;
    private String name;
    private String profile_pic;

    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public String getUsername() {
        return username;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getProfile_pic() {
        return profile_pic;
    }

    public void setProfile_pic(String profile_pic) {
        this.profile_pic = profile_pic;
    }
}

The RestClient class:

public class RestClientUsers {
    private static final String BASE_URL = "http://www.myurl.test/project";
    private ApiServiceUsers apiServiceUsers;

    public RestClientUsers() {
        Gson gson = new GsonBuilder()
                .setDateFormat("dd'-'MM'-'yyyy'T'HH':'mm':'ss'.'SSS'Z'")
                .create();
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setEndpoint(BASE_URL)
                .setConverter(new GsonConverter(gson))
                .build();

        apiServiceUsers = restAdapter.create(ApiServiceUsers.class);

    }

    public void getUsers(final Callback<PojoUsers> callback) {
        apiServiceUsers.getUsers(callback);
    }

    public ApiServiceUsers getApiServiceUsers() {
        return apiServiceUsers;
    }
}

Finally, in the main Activity:

RestClientUsers restClientUsers = new RestClientUsers();
restClientUsers.getUsers(new Callback<PojoUsers>() {
    @Override
    public void success(PojoUsers pojoUsers, Response response) {
        Log.i("RESPONSE OK", pojoUsers.getName());
    }

    @Override
    public void failure(RetrofitError error) {
        Log.i("RESPONSE ERROR", error.getMessage());
    }
});

But this log totally nothing.

By the Retrofit debug I can see that the request is ok, and also the result of the request is the users list, but the callback doesn't seem to work.

How can I fix that? And how can I store all the 3 users in a structure like array? Thanks in advance.

galath
  • 5,717
  • 10
  • 29
  • 41
helloimyourmind
  • 994
  • 4
  • 14
  • 30

0 Answers0