使用 android 中的改造从 response.body 获取数据给出 null

fetching data from response.body gives null uisng retrofit in android

嘿,我正在尝试使用 node.js 后端改造从我的 api 获取数据,然后将其设置为 Textview。我能够成功连接到 api,但是当我尝试将文本设置为 textview 时它给我带来了问题。我的 pojo 中有一个函数 getCompanyname 当我尝试使用此函数设置文本时,它在 textview 中没有显示任何内容,当我在 Toast 消息中尝试它时,消息为 null作为一条消息,所以我发现我的方法有空值,但我找不到问题所在。以下是必需的代码

1) 请求接口代码

public interface RetrofitInterface {

@POST("logins")
Call<ServerResponse> operation(@Body ServerRequest request);
@GET("getbusinessprofile/{username}")
Call<CompanyInfo> getCompanydetails(@Path("username")String username);
}

2) 仪表板片段

public void getCompanyData(String username){

           Retrofit retrofit = new Retrofit.Builder()
                              .baseUrl(Constants.BASE_URL)
                              .addConverterFactory(GsonConverterFactory.create())
                              .build();

    RetrofitInterface requestInterface = retrofit.create(RetrofitInterface.class);
    Call<CompanyInfo> callResponse = requestInterface.getCompanydetails(username);

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

           CompanyInfo info=response.body();
           comp_text.setText(info.getCompanyname());
           mail_text.setText(info.getEmail());
           gst_text.setText(info.getGstNo());
            Toast.makeText(getActivity(), "Company name-> "+info.getCompanyname(), Toast.LENGTH_SHORT).show();


        }

我的getCompanyname、getGST、getEmail 都是空的

这是我在控制台 json 上的回复

{"companyname":"hitouch","email":"a@b.com","gst_no":"12354"}

3) **CompanyInfo.java

public class CompanyInfo {

@SerializedName("companyname")
@Expose
private String companyname;
@SerializedName("email")
@Expose
private String email;
@SerializedName("gst_no")
@Expose
private String gstNo;

public String getCompanyname() {
    return companyname;
}

public void setCompanyname(String companyname) {
    this.companyname = companyname;
}

public String getEmail() {
    return email;
}

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

public String getGstNo() {
    return gstNo;
}

public void setGstNo(String gstNo) {
    this.gstNo = gstNo;
}

}

请给我一些建议,如果你愿意,我可以 post 我的后端代码 node.js

谢谢!!

编辑

既然回复是这样的

{
    "statusCode": 200,
    "headers": {
        "Content-Type": "application/json"
    },
    "body": { "companyname": "hitouch", "email": "a@b.com", "gst_no": "12354" }
}

你必须像这样使用 pojo

public class CompanyInfoResponse {
    @SerializedName("statusCode")
    int statusCode;

    @SerializedName("headers")
    Map<String, String> headers;        

    @SerializedName("body")
    CompanyInfo companyInfo;
}

改成

@GET("getbusinessprofile/{username}")
Call<ApiResponse<CompanyInfo>> 
        getCompanydetails(@Path("username")String username);

获取实际数据

CompanyInfo companyInfo = response.body().getBody();

如果你想将它应用于不同类型的响应,如 EmployeeInfo,你可以使用这样的包装器

public class ApiResponse<T> {
    private transient boolean isSuccess;

    @SerializedName("statusCode")
    private int statusCode;
    @SerializedName("headers")
    Map<String, String> headers;
    @SerializedName("body")
    private T body;

    @Nullable
    public int getStatusCode() {
        return statusCode;
    }

    @Nullable
    public Map<String, String> getHeaders() {
        return headers;
    }

    @Nullable
    public T getBody() {
        return body;
    }
}

然后这样声明

@GET("getbusinessprofile/{username}")
Call<ApiResponse<CompanyInfo>> getCompanydetails(@Path("username")String username);

@GET("getbusinessprofile/{username}")
Call<ApiResponse<EmployeeInfo>> getEmployeeDetails(@Path("username")String username);

获取实际数据

CompanyInfo companyInfo = response.body().getBody();
EmployeeInfo empInfo = response.body().getBody();