如何从函数onResponse of Retrofit返回值? [英] How can I return value from function onResponse of Retrofit?

查看:105
本文介绍了如何从函数onResponse of Retrofit返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在改装呼叫请求中返回我从 onResponse 方法获得的值,是否存在一种方法,我可以从覆盖的方法中获得这个价值?这是我的代码:

  public JSONArray RequestGR(LatLng start,LatLng end)
{
final JSONArray jsonArray_GR;

EndpointInterface loginService = ServiceAuthGenerator.createService(EndpointInterface.class);
电话< GR> call = loginService.getroutedriver();
call.enqueue(新回调< GR>(){
@Override
public void onResponse(响应< GR>响应,Retrofit改装)
{

jsonArray_GR = response.body()。getRoutes();
//我需要在我的RequestGR方法中返回这个jsonArray_GR
}
@Override
public void onFailure(Throwable t){
}
});
返回jsonArray_GR;
}

我无法获得的值jsonArray_GR 因为能够在 onResponse 方法中使用它我需要将其声明为final并且我不能给它一个值。

解决方案

问题是你试图同步返回 enqueue 的值,但它是使用回调的异步方法,所以你不能这样做。您有2个选项:


  1. 您可以更改 RequestGR 方法以接受回调然后将 enqueue 回调链接到它。这类似于rxJava等框架中的映射。

这看起来大致如下:

  public void RequestGR(LatLng start,LatLng end,final Callback< JSONArray> arrayCallback)
{

EndpointInterface loginService = ServiceAuthGenerator.createService( EndpointInterface.class);
电话< GR> call = loginService.getroutedriver();
call.enqueue(新回调< GR>(){
@Override
public void onResponse(响应< GR>响应,Retrofit改装)
{

JSONArray jsonArray_GR = response.body()。getRoutes();
arrayCallback.onResponse(jsonArray_GR);
}
@Override
public void onFailure(Throwable t){
//错误处理?arrayCallback.onFailure(t)?
}
});
}

这种方法的警告是它只是将异步内容推到另一个层次,这可能是你的问题。


  1. 你可以使用类似于的对象BlockingQueue 承诺 Observable 甚至是你自己的容器对象(小心成为线程)安全),允许您检查和设置值。

这看起来像:

  public BlockingQueue< JSONArray> RequestGR(LatLng start,LatLng end)
{
//您可以在回调之外创建最终容器对象,然后从回调内部将值传递给它。
final BlockingQueue< JSONArray> blockingQueue = new ArrayBlockingQueue<>(1);
EndpointInterface loginService = ServiceAuthGenerator.createService(EndpointInterface.class);
电话< GR> call = loginService.getroutedriver();
call.enqueue(新回调< GR>(){
@Override
public void onResponse(响应< GR>响应,Retrofit改装)
{

JSONArray jsonArray_GR = response.body()。getRoutes();
blockingQueue.add(jsonArray_GR);
}
@Override
public void onFailure(Throwable t){
}
});
返回blockingQueue;
}

然后您可以在调用方法中同步等待结果,如下所示:

  BlockingQueue< JSONArray> result = RequestGR(42,42); 
JSONArray value = result.take(); //这将阻止你的线程

我强烈建议你阅读像rxJava这样的框架。 / p>

I'm trying to return a value that i get from onResponse method in retrofit call request, is there a way that i can get that value out of the overrided method? here is my code:

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_GR because to be able to use it in onResponse method i need to declare it final and i can't give it a value.

解决方案

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:

  1. You can change your RequestGR method to accept a callback and then chain the enqueue callback to it. This is similar to mapping in frameworks like 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.

  1. You can use an object similar to a BlockingQueue, Promise or an Observable or even your own container object (be careful to be thread safe) that allows you to check and set the value.

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.

这篇关于如何从函数onResponse of Retrofit返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆