嘿,我正在尝试通过对node.js后端进行改造来从我的api中获取数据,然后将其设置为Textview。我能够成功连接到api,但是当我尝试将文本设置为textview时,它给我带来了问题。当我尝试使用此功能来设置文本时,我在pojo中有一个功能 getCompanyname 。当我在Toast中尝试该功能时,它在Textview中什么都没有显示强>,所以我认为我的方法具有空值,但我找不到问题。以下是必需的代码
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后端代码
谢谢!!
修改
答案 0 :(得分: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等不同类型的Response,则可以使用这样的包装器
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();