我使用了Retrofit2。 我正在尝试在onResponse内定义字符串“ stat”,但它返回null。
public class StatusConfig {
private String stat;
.......................
public String Status() {
APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
Call<Login> call = apiInterface.LoginCheck();
call.enqueue(new Callback<Login>() {
@Override
public void onResponse(Call<Login> call, Response<Login> response) {
if(response.isSuccessful()) stat="true";
else stat="false";
}
@Override
public void onFailure(Call<Login> call, Throwable t) {
}
});
return stat;
}
............
}
我正在尝试使用此代码,但仍返回null。
public class StatusConfig {
.......................
public String Status() {
final String[] stat = new String[1];
APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
Call<Login> call = apiInterface.LoginCheck();
call.enqueue(new Callback<Login>() {
@Override
public void onResponse(Call<Login> call, Response<Login> response) {
if(response.isSuccessful()) stat[0]="true";
else stat[0]="false";
}
@Override
public void onFailure(Call<Login> call, Throwable t) {
}
});
return stat[0];
}
............
}
出什么问题了?怎么了?
答案 0 :(得分:1)
调用v 42.4000015258785 -6.60893774032594 -1.60000002384146
v 42.4000015258785 -6.65268325805652 -1.20000004768452
v 42.0000000000008 -6.57929277420054 -1.60000002384146
v 42.0000000000004 -6.57929277420055 -1.60000002384106
将异步执行。这里发生的事情是您将立即返回未初始化的call.enqueue()
。返回stat
后,将执行该操作并将其分配给new Callback
。
调用stat
时必须使用interface来获得通知。
接口:
new Callback
初始化该接口的实例
public interface OnResponseCallback{
public void onGetResponse(String data);
}
并通过 OnResponseCallback callBack = new OnResponseCallback(){
public void onGetResponse(String data){
// your response is here
}
};
方法传递callBack
。
Status
这样称呼
public void Status(OnResponseCallback callBack) {
final String[] stat = new String[1];
APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
Call<Login> call = apiInterface.LoginCheck();
call.enqueue(new Callback<Login>() {
@Override
public void onResponse(Call<Login> call, Response<Login> response) {
if(response.isSuccessful()) {
// stat[0]="true";
if(callBack != null) callBack.onGetResponse("true");
}else{
if(callBack != null) callBack.onGetResponse("false");
}
// else stat[0]="false";
}
@Override
public void onFailure(Call<Login> call, Throwable t) {
}
});
// return stat[0];
}
查看class variable教程以了解使用界面进行的回调
答案 1 :(得分:0)
先生。
此代码工作正常,问题出在逻辑上。如果在onResponse或onFailure内编写Toast,将看到stat变量的值如何,但是为null,因为enqueue方法是异步的,因此Status方法将始终返回null,因为此方法不等待异步执行。
我回复了您的代码,调用了另一个Api服务,您可以使用您的代码对其进行测试:
public class StatusConfig {
private String stat;
public String Status() {
IpifyService apiService = initRetrofit();
Call<ResponseBody> call = apiService.getIp();
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//When the response is successfull we will asign the value true to stat
if(response.isSuccessful()){
stat="true";
Toast.makeText(getApplicationContext(),stat,Toast.LENGTH_SHORT).show();
}
//When the response is not successfull we will asign the value false to stat
else{
stat="false";
Toast.makeText(getApplicationContext(),stat,Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
//When the the onFailure is called we will asign the value error to stat
stat = "error";
Toast.makeText(getApplicationContext(),stat,Toast.LENGTH_SHORT).show();
}
});
//This always is null because the enqueue call is not executed yet.
return stat;
}
}
我希望这可以帮助您理解代码。