java 如何从 Retrofit 的 onResponse 函数返回值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34184088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I return value from function onResponse of Retrofit?
提问by Cristian Cam G
I'm trying to return a value that i get from onResponsemethod in retrofitcall request, is there a way that i can get that value out of the overrided method? here is my code:
我正在尝试返回一个从调用请求中的onResponse方法获得的值,retrofit有没有办法可以从覆盖的方法中获取该值?这是我的代码:
public JSONArray RequestGR(LatLng start, LatLng end)
{
final JSONArray jsonArray_GR;
EndpointInterface loginService = ServiceAuthGenerator.createService(EndpointInterface.class);
Call<GR> call = loginService.getroutedriver();
call.enqueue(new Callback<GR>() {
@Override
public void onResponse(Response<GR> response , Retrofit retrofit)
{
jsonArray_GR = response.body().getRoutes();
//i need to return this jsonArray_GR in my RequestGR method
}
@Override
public void onFailure(Throwable t) {
}
});
return jsonArray_GR;
}
i can't get the value of jsonArray_GRbecause to be able to use it in onResponsemethod i need to declare it final and i can't give it a value.
我无法获得的值,jsonArray_GR因为为了能够在onResponse方法中使用它,我需要将其声明为 final 并且我不能给它一个值。
回答by torbinsky
The problem is you are trying to synchronously return the value of enqueue, but it is an asynchronous method using a callback so you can't do that. You have 2 options:
问题是您正在尝试同步返回 的值enqueue,但它是使用回调的异步方法,因此您不能这样做。您有 2 个选择:
- You can change your
RequestGRmethod to accept a callback and then chain theenqueuecallback to it. This is similar to mapping in frameworks like rxJava.
- 您可以更改
RequestGR方法以接受回调,然后将enqueue回调链接到它。这类似于 rxJava 等框架中的映射。
This would look roughly like:
这大概是这样的:
public void RequestGR(LatLng start, LatLng end, final Callback<JSONArray> arrayCallback)
{
EndpointInterface loginService = ServiceAuthGenerator.createService(EndpointInterface.class);
Call<GR> call = loginService.getroutedriver();
call.enqueue(new Callback<GR>() {
@Override
public void onResponse(Response<GR> response , Retrofit retrofit)
{
JSONArray jsonArray_GR = response.body().getRoutes();
arrayCallback.onResponse(jsonArray_GR);
}
@Override
public void onFailure(Throwable t) {
// error handling? arrayCallback.onFailure(t)?
}
});
}
The caveat with this approach is it just pushes the async stuff up another level, which might be a problem for you.
这种方法的警告是它只是将异步内容推到另一个级别,这对您来说可能是一个问题。
- You can use an object similar to a
BlockingQueue,Promiseor anObservableor even your own container object (be careful to be thread safe) that allows you to check and set the value.
- 您可以使用类似于 a 的对象
BlockingQueue,Promise或者Observable甚至是您自己的容器对象(注意线程安全),它允许您检查和设置值。
This would look like:
这看起来像:
public BlockingQueue<JSONArray> RequestGR(LatLng start, LatLng end)
{
// You can create a final container object outside of your callback and then pass in your value to it from inside the callback.
final BlockingQueue<JSONArray> blockingQueue = new ArrayBlockingQueue<>(1);
EndpointInterface loginService = ServiceAuthGenerator.createService(EndpointInterface.class);
Call<GR> call = loginService.getroutedriver();
call.enqueue(new Callback<GR>() {
@Override
public void onResponse(Response<GR> response , Retrofit retrofit)
{
JSONArray jsonArray_GR = response.body().getRoutes();
blockingQueue.add(jsonArray_GR);
}
@Override
public void onFailure(Throwable t) {
}
});
return blockingQueue;
}
You can then synchronously wait for your result in your calling method like this:
然后,您可以像这样在调用方法中同步等待结果:
BlockingQueue<JSONArray> result = RequestGR(42,42);
JSONArray value = result.take(); // this will block your thread
I would highly suggest reading up on a framework like rxJava though.
我强烈建议阅读像 rxJava 这样的框架。

