0

I am using Retrofit2 to make a GET call to my server from my android app , which returns a Null object as a response , whereas when I make the same GET call by Postman it returns a valid object as desired.

I have an interface as follows, where findFriends() is a function in my node.js server

public interface RetrofitInterface
{
//for searching for friends
  @GET("find_friends/{email}")
  Call<User> findFriends(@Path("email") String email);
}

My class for the object is as follows

public class User
{
    private String name;
    private String email;
    private String city;
    private int age;

    private String password;
    private String created_at;
    private String newPassword;
    private String token;

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

    public void setEmail(String email) {
        this.email = email;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

    public String getCity()
    {
        return city;
    }

    public Integer getAge() {
        return age;
    }

    public String getCreated_at() {
        return created_at;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public void setToken(String token) {
        this.token = token;
    }
}

My caller function which uses the interface is as follows

public void searchFunction(View view)
    {
        fMail= searchTxtView.getText().toString();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RetrofitInterface retrofitInterface = retrofit.create(RetrofitInterface.class);

        Call<User> call = retrofitInterface.findFriends(fMail);

        call.enqueue(new Callback<User>()
        {
            @Override
            public void onResponse(Call<User> call, retrofit2.Response<User> response)
            {

                if (response.isSuccessful())
                {
                    User responseBody = response.body();
                    //data = new ArrayList<>(Arrays.asList(responseBody.getData()));
                    adapter = new DataAdapterForFriendList(responseBody);
                    recyclerView.setAdapter(adapter);
                    Log.d("success", response.toString());
                    Log.d("success2", responseBody.toString());
                }
                else
                {
                    ResponseBody errorBody = response.errorBody();

                    Gson gson = new Gson();

                    try
                    {
                        Log.d("error1", response.toString());;
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                        Log.d("error2", e.toString());
                    }
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t)
            {
                Log.d(TAG, "onFailure: "+t.getLocalizedMessage());
            }
        });
    }

My postman response is provided My postman Response

And the response as a null object in Android Studio while debugging is The NULL object as a response

What am I doing wrong here? The response is successful but instead of containing anything it contains all null values. Any help would be greatly appreciated.

1
  • You donot have the correct model, your model should be like the response as in the postman. Commented Mar 5, 2018 at 5:14

5 Answers 5

1

You should use your Model class in this way now you can get API Result like below

import com.google.gson.annotations.SerializedName;
import java.io.Serializable; 

public class User implements Serializable{

    @SerializedName("name") 
    private String name;

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

}

here you can easly create your POJO class Link

Sign up to request clarification or add additional context in comments.

Comments

0

Make one another pojo class for response bacause when you getting response it give data key object it match then show all the value in null. server key is important to getting data used below pojo call and pass into api interface ....

public class UserResponseModel {

    @SerializedName("data")
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

then after change api method like below ..

@GET("find_friends/{email}")
Call<UserResponseModel> findFriends(@Path("email") String email);

and if you want getting freind list from given by server then make one pojo class and pass above UserResponseModel model class bacause I check your postman response it give one object and array. If you required to friend list array define below line in above userresponse model class ...

@SerializedName("friend_list")
private List<Friend> friendList;

public List<Friend> getFriendList() {
    return friendList;
}

public void setFriendList(List<Friend> friendList) {
    this.friendList = friendList;
}

Comments

0

Yes, "User" model which you created is wrong because you are directly accessing field which is the inner object of the main object of your response. . so just copy your response and pest in below link and create a new model class and use this model instead of User model.

http://www.jsonschema2pojo.org/

1 Comment

Yes, you have to create response class from jsonschema2pojo.org. For that copy paste your response into this given site and it will give you generated class as required for response.
0

you need create class pojo below

public class DataGet {

    int status;
    boolean isSuccess;
    String message;
    User data;

}

and change

Call<User> call = retrofitInterface.findFriends(fMail);
    call.enqueue(new Callback<User>(){
        @Override
        public void onResponse (Call <User> call, retrofit2.Response <User> response){
            //...
        }
        @Override
        public void onFailure (Call <User> call, Throwable t){
            //...
        }
    });

to

Call<DataGet> call = retrofitInterface.findFriends(fMail);
call.enqueue(new Callback<DataGet>(){
    @Override
    public void onResponse (Call <DataGet> call, retrofit2.Response <DataGet> response){
        //...
    }
    @Override
    public void onFailure (Call <DataGet> call, Throwable t){
        //...
    }
});

Comments

0

Check, your user is inside another object. You can use,

public class Responce {
    private int status;
    private User data;
}

Here User is your User model. you can generate a POJO model from here

Your RetrofitInterface will be

public interface RetrofitInterface
{
    //for searching for friends
    @GET("find_friends/{email}")
    Call<Responce> findFriends(@Path("email") String email);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.