我有一个在其中实现一个类的方法:
public class RetrofitCallBackUtil {
private ArrayList<Message> messages = new ArrayList<>();
public ArrayList<Message> getLastTenMessageCallBack(int user_id, int sender_id, int offset_number, RESTDatabaseDAO service) {
Call<ArrayList<Message>> call = service.getLastTenMessage(user_id, sender_id, offset_number);
call.enqueue(new Callback<ArrayList<Message>>() {
@Override
public void onResponse(Call<ArrayList<Message>> call, Response<ArrayList<Message>> response) {
if (response.isSuccess()) {
messages = response.body();
Log.i("Success", "Good ten " + response.body().size());
} else {
Log.i("Success", "Not good ten" + response.raw());
}
}
@Override
public void onFailure(Call<ArrayList<Message>> call, Throwable t) {
Log.i("Fail", "Failure ten " + t.toString());
}
});
//Still get NullPointException from here. (messages.size())
Log.i("Success", "Good ten activity" + messages.size());
return messages;
}
}
我想获取消息变量的值,但不能。我还创建了一个全局变量
List<Message> messages
并返回,但仍然无法从response.body()
获取值。有人对此有解决方案吗?更新:
我想要这样的东西:
List<Message> testMessages = retrofitCallBackUtil.getLastTenMessageCallBack(AppConfig.USER_ID, userId, 0, service);
因为我想多次使用该方法的结果,所以我希望它向我返回数据。
最佳答案
onResponse
和onFailure
是异步调用的,因此当您尝试return messages;
时,尚未对其进行更新。
从回调中返回它。
编辑1:
public void getLastTenMessageCallBack(int user_id, int sender_id, int offset_number, RESTDatabaseDAO service) {
Call<ArrayList<Message>> call = service.getLastTenMessage(user_id, sender_id, offset_number);
call.enqueue(new Callback<ArrayList<Message>>() {
@Override
public void onResponse(Call<ArrayList<Message>> call, Response<ArrayList<Message>> response) {
if (response.isSuccess()) {
// THIS IS BEING CALLED AFTER YOUR METHODS RETURNS, SO UPDATE YOUR VIEWS LIKE THIS.
updateViews(response.body());
Log.i("Success", "Good ten " + response.body().size());
} else {
Log.i("Success", "Not good ten" + response.raw());
}
}
@Override
public void onFailure(Call<ArrayList<Message>> call, Throwable t) {
Log.i("Fail", "Failure ten " + t.toString());
}
});
}
public void updateViews(List<Messages> messages){
//use messages to update your views here.
}
编辑2:现在,由于您要从许多地方调用它,因此传递诸如requestCode之类的内容,并检查相同的onResponse以识别调用的来源。
public void getLastTenMessageCallBack(final int requestCode, int user_id, ..
和
onResponse
updateViews(requestCode, response.body());
并通过比较请求代码相应地更新您的视图。
public void updateViews(int requestCode, List<Messages> messages){
if(requestCode==XYZ)
//use messages to update your views here.
}
编辑3:
定义自己的
interface
以接收回调:public interface NetworkResponseCallback {
void onSuccess(ArrayList<Message>> messages);
void onFailure(); //if you want to pass something else, like cause of failure you can add it.
}
现在,更改您的方法定义:
public void getLastTenMessageCallBack(int user_id, ... , final NetworkResponseCallback networkResponseCallback){
实现
onResponse
和onFailure
:if (response.isSuccess()) {
if(networkResponseCallback!=null)
networkResponseCallback.onSuccess(response.body());
public void onFailure(Call<ArrayList<Message>> call, Throwable t) {
Log.i("Fail", "Failure ten " + t.toString());
networkResponseCallback.onFailure();
}
像这样调用
getLastTenMessageCallBack
方法:getLastTenMessageCallBack(int user_id, int sender_id, int offset_number, RESTDatabaseDAO service, new NetworkResponseCallback() {
@Override
public void onSuccess(List<Messages> messages) {
}
@Override
public void onFailure() {
}
});