Android JSON响应始终为int值返回零
我使用改型库调用HTTP请求,并从API获得结果。我在改装库中配置了登录,以便查看发生了什么。响应是一个简单的基元整数类型。 问题是它总是返回零。响应类(LoginModel)也是一个经典类,对于整数类型,它有一个setter和一个getterAndroid JSON响应始终为int值返回零,android,json,retrofit2,Android,Json,Retrofit2,我使用改型库调用HTTP请求,并从API获得结果。我在改装库中配置了登录,以便查看发生了什么。响应是一个简单的基元整数类型。 问题是它总是返回零。响应类(LoginModel)也是一个经典类,对于整数类型,它有一个setter和一个getter public class LoginModel { @SerializedName("result") private int result; public void setResult(int result){ this.res
public class LoginModel {
@SerializedName("result")
private int result;
public void setResult(int result){
this.result = result;
}
public int getResult(){
return result;
}
}
下图是我的日志,你们可以看到我得到了响应10,但当我在onResponse函数中记录getResult时,它总是打印为零
Log.i("BODY : ", String.valueOf(response.body().getResult()));
我搞不清楚出了什么问题。谢谢你的帮助。谢谢
更新:调用API的完整代码
Call<LoginModel> isSended = apiService.sendCode(phoneNumber);
isSended.enqueue(new Callback<LoginModel>() {
@Override
public void onResponse(Call<LoginModel> call, Response<LoginModel> response) {
Log.i("BODY : ", String.valueOf(response.body().getResult()));
if (!response.isSuccessful()) {
Toast.makeText(context, context.getResources().getString(R.string.errorServer), Toast.LENGTH_LONG).show();
dismissProgressBarDialog();
return;
}
myTimer = new MyTimer(SECOND_TIME, 1000);
views.reactionActivationPress(response.body().getResult());
}
@Override
public void onFailure(Call<LoginModel> call, Throwable t) {
Log.i(Constant.NETWORK_LOG, "sendCode : " + t.toString());
dismissProgressBarDialog();
Toast.makeText(MyApplication.getContext(), "Server Problem", Toast.LENGTH_LONG).show();
}
});
您在代码中的问题是您尝试访问结果两次,请按以下方式尝试 只声明一次
response.body().getResult()
Call issend=apiService.sendCode(电话号码);
enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
dismisssProgressBarDialog();
int model=response.body().getResult();
Log.i(“BODY:,String.valueOf(model));
如果(!response.issusccessful()){
Toast.makeText(context,context.getResources().getString(R.string.errorServer),Toast.LENGTH_LONG.show();
返回;
}
myTimer=新的myTimer(秒时间,1000);
视图。反应激活压力机(模型);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.i(Constant.NETWORK_Log,“sendCode:+t.toString());
dismisssProgressBarDialog();
Toast.makeText(MyApplication.getContext(),“服务器问题”,Toast.LENGTH_LONG.show();
}
});
尝试将“结果”字段设置为公共字段,而不是私有字段。我猜,改型是忽略json,而根本不初始化结果字段。删除getter和setter并公开结果字段
public class LoginModel {
@SerializedName("result")
public int result;
}
代码没有问题。它工作得很好。 确保您使用的是带改装的Gson。
我已经测试了代码,它运行良好。无需在模型类中进行更改。使用Integer,如果为空,则表示从服务器获得的json有另一个名称或类型,使用postman测试api,并检查响应名称和类型
public class LoginModel {
@SerializedName("result")
@Expose
private Integer result;
public void setResult(Integer result){
this.result = result;
}
public Integer getResult(){
return result;
}
}
ApiClient.getClient().create(ApiService.class).sendCode(phoneNumber).enqueue(new Callback()){
@凌驾
公共void onResponse(调用、响应){
Log.i(“BODY:,String.valueOf(response.BODY().getResult()));
如果(!response.issusccessful()){
Toast.makeText(context,context.getResources().getString(R.string.errorServer),Toast.LENGTH_LONG.show();
dismisssProgressBarDialog();
返回;
}
myTimer=新的myTimer(秒时间,1000);
views.reactionActivationPress(response.body().getResult());
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.i(Constant.NETWORK_Log,“sendCode:+t.toString());
dismisssProgressBarDialog();
Toast.makeText(MyApplication.getContext(),“服务器问题”,Toast.LENGTH_LONG.show();
}
});
我认为您的POJO课程中存在一个问题试试这个,它可能会对您有所帮助
public class LoginModel implements Serializable {
@SerializedName("result")
@Expose
private Integer result;
public Integer getResult() {
return result;
}
public void setResult(Integer result) {
this.result = result;
}
}
与接口共享调用api的代码。使用@Expose with SerializedName注释并重试,如果问题仍然存在,请将代码和响应字符串添加为jsonlogs@MehulKabaria-我更新了我的问题,并将调用api的代码放在你想要的地方。@PJain-json响应如图所示。OkHttp记录json。{“result”:10}您是否尝试过使用@Expose annotation with SerializedName?result是一个整数,而不是LoginModel。whole response.body()是LoginModel的对象。将int替换为更新后的ans中的LoginModel并尝试@mehranzamani您在任何地方见过这样的模型,或者您有任何证据可以证明您的答案吗?我从@MehranZamani那里得到了我的模型。我拥有的唯一证据是近5年的Android开发经验:)试着用这个模型提出一个请求,然后检查它是否有效。您还可以尝试向模型中的结果字段添加Expose注释,然后它也应该可以工作。public class LoginModel{@SerializedName(“result”)@Expose private int result;public void setResult(int result){this.result=result;}public int getResult(){return result;}}我想我同意@AndriyAntoniv,不要让它成为私有的,至少要使用包保护。在改造中添加GsonConverter的地方共享您的代码。这可以解决问题,但问题出在其他地方。我在build variant中禁用了缩小和收缩功能,它不见了!你在这里的小例子帮助我从我的应用程序中删除了大约100行冗余代码。谢谢
public class LoginModel {
@SerializedName("result")
@Expose
private Integer result;
public void setResult(Integer result){
this.result = result;
}
public Integer getResult(){
return result;
}
}
ApiClient.getClient().create(ApiService.class).sendCode(phoneNumber).enqueue(new Callback<LoginModel>() {
@Override
public void onResponse(Call<LoginModel> call, Response<LoginModel> response) {
Log.i("BODY : ", String.valueOf(response.body().getResult()));
if (!response.isSuccessful()) {
Toast.makeText(context, context.getResources().getString(R.string.errorServer), Toast.LENGTH_LONG).show();
dismissProgressBarDialog();
return;
}
myTimer = new MyTimer(SECOND_TIME, 1000);
views.reactionActivationPress(response.body().getResult());
}
@Override
public void onFailure(Call<LoginModel> call, Throwable t) {
Log.i(Constant.NETWORK_LOG, "sendCode : " + t.toString());
dismissProgressBarDialog();
Toast.makeText(MyApplication.getContext(), "Server Problem", Toast.LENGTH_LONG).show();
}
});
public class LoginModel implements Serializable {
@SerializedName("result")
@Expose
private Integer result;
public Integer getResult() {
return result;
}
public void setResult(Integer result) {
this.result = result;
}
}