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

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

问题描述

我正在尝试返回从 retrofit 调用请求中的 onResponse 方法获得的值,有没有办法从被覆盖的值中获取该值方法?这是我的代码:

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;
    }

我无法获得 jsonArray_GR 的值,因为为了能够在 onResponse 方法中使用它,我需要将它声明为 final,我不能给它一个价值.

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.

推荐答案

问题是您试图同步返回 enqueue 的值,但它是使用回调的异步方法,因此您可以不要那样做.您有 2 个选择:

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. 您可以更改您的 RequestGR 方法以接受回调,然后将 enqueue 回调链接到它.这类似于 rxJava 等框架中的映射.
  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.

大致如下:

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. 你可以使用类似于 BlockingQueuePromiseObservable 的对象,甚至你自己的容器对象(小心线程安全)允许您检查和设置值.
  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.

这看起来像:

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

我强烈建议阅读像 rxJava 这样的框架.

I would highly suggest reading up on a framework like rxJava though.

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

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