【发布时间】: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