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

Getting a Null Object as response while calling GET using Retrofit2

我正在使用 Retrofit2 从我的 android 应用程序对我的服务器进行 GET 调用,其中 returns 一个 Null 对象作为响应,而当我通过 Postman 进行相同的 GET 调用时,它returns 所需的有效对象。

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

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

我的class对象如下

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

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

您应该以这种方式使用您的模型 class 现在您可以获得 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 class Link

是的,"User" 您创建的模型是错误的,因为您直接访问的字段是您响应的主要对象的内部对象。 . 所以只需在下面复制您的响应和害虫 link 并创建一个新模型 class 并使用此模型而不是用户模型。

http://www.jsonschema2pojo.org/

您需要在下面创建 class pojo

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

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 是您的 User 模型。你可以从这里生成一个 POJO 模型

您的 RetrofitInterface 将是

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

创建另一个 pojo class 用于响应,因为当您收到响应时,它会提供匹配的数据键对象,然后在 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);

如果你想从服务器给定的朋友列表中得到一个 pojo class 并通过 UserResponseModel 模型 class 因为我检查你的邮递员响应它给一个对象和大批。如果您需要好友列表数组,请在上面的用户响应模型中定义下面的行 class ...

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

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

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