使用Retrofit2调用GET时获取Null对象作为响应

问题描述 投票:0回答:5

我正在使用Retrofit2从我的Android应用程序对我的服务器进行GET调用,它返回一个Null对象作为响应,而当我通过Postman进行相同的GET调用时,它会根据需要返回一个有效的对象。

我有一个如下界面,其中findFriends()是我的node.js服务器中的一个函数

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

我的对象类如下

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

我使用该接口的调用函数如下

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

在调试时,Android Studio中作为空对象的响应是The NULL object as a response

我在这做错了什么?响应成功但不包含任何内容,它包含所有空值。任何帮助将不胜感激。

android null retrofit response retrofit2
5个回答
0
投票

为响应做出另一个pojo类,当你得到响应它给它匹配的数据键对象然后显示null中的所有值。服务器密钥对于在pojo调用下使用数据并传递到api接口非常重要....

public class UserResponseModel {

    @SerializedName("data")
    private User user;

    public User getUser() {
        return user;
    }

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

然后改变api方法,如下面..

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

如果你想从服务器给出的freind列表然后制作一个pojo类并传递上面的UserResponseModel模型类,因为我检查你的邮递员响应它给出一个对象和数组。如果您需要在上面的userresponse模型类中定义下面的朋友列表数组...

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

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

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

1
投票

您应该以这种方式使用Model类,现在您可以获得如下所示的API结果

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

}

在这里,您可以轻松创建您的POJO类qazxsw poi


0
投票

是的,您创建的“用户”模型是错误的,因为您正在直接访问作为响应主对象的内部对象的字段。 。所以只需在下面的链接中复制您的响应和有害生物并创建一个新的模型类并使用此模型而不是用户模型。

Link


0
投票

你需要在下面创建类pojo

http://www.jsonschema2pojo.org/

并改变

public class DataGet {

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

}

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){
            //...
        }
    });

0
投票

检查,您的用户在另一个对象内。您可以使用,

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){
        //...
    }
});

在这里public class Responce { private int status; private User data; } 是你的User模型。你可以从这里生成一个POJO模型

你的User将是

RetrofitInterface
© www.soinside.com 2019 - 2024. All rights reserved.