【问题标题】:fetching data from response.body gives null uisng retrofit in android从 response.body 获取数据会在 android 中提供 null uisng 改造
【发布时间】:2018-10-22 12:10:21
【问题描述】:

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

1) RequestInterface的代码

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

}

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

谢谢!!

编辑

【问题讨论】:

标签: android retrofit retrofit2 response jsonresponse


【解决方案1】:

既然响应是这样的

{
    "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();

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-04
  • 2021-08-30
  • 2015-11-16
  • 1970-01-01
相关资源
最近更新 更多