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.