简体   繁体   English

如何从函数onResponse of Retrofit返回值?

[英]How can I return value from function onResponse of Retrofit?

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? 我试图在retrofit调用请求中返回从onResponse方法获得的值,是否有一种方法可以从覆盖方法中获取该值? 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. 我无法获得jsonArray_GR的值,因为能够在onResponse方法中使用它我需要声明它最终,我不能给它一个值。

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. 问题是你试图同步返回enqueue的值,但它是一个使用回调的异步方法,所以你不能这样做。 You have 2 options: 你有2个选择:

  1. You can change your RequestGR method to accept a callback and then chain the enqueue callback to it. 您可以更改RequestGR方法以接受回调,然后将enqueue回调链接到它。 This is similar to mapping in frameworks like rxJava. 这类似于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. 您可以使用类似于BlockingQueuePromiseObservable对象,甚至可以使用自己的容器对象(小心保证线程安全),以便检查和设置值。

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这样的框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM