简体   繁体   English

我如何知道我正在回复的OkHttp电话

[英]How do I know what OkHttp call I am returning from

I have an activity that implements okhttp3.Callback and overrides the onFailure and onResume methods. 我有一个实现okhttp3.Callback的活动,并覆盖onFailure和onResume方法。 There are multiple different requests called in this activity, something similar to this: 此活动中调用了多个不同的请求,类似于:

Request request1 = RequestBuilder.login(username,password);
okHttpClient.newCall(request1).enqueue(this);
Request request2 = RequestBuilder.getData(token);
okHttpClient.newCall(request2).enqueue(this);

How can I know if I am returning from request1 or request2 in the onResponse method, since the response needs to be handled differently for each request? 如何在onResponse方法中从request1或request2返回,因为响应需要针对每个请求进行不同的处理?

Option 1: in onResponse(Response response) use response.request() to get the RequestObject and compare that with yours. 选项1:在onResponse(Response response)使用response.request()来获取RequestObject并与之进行比较。

Option 2: instead of this , use a anonymous class, like so: 选项2:而不是this ,使用匿名类,如下所示:

okHttpClient.newCall(request1).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, Throwable throwable) {
                ...
            }

            @Override
            public void onResponse(Response response) throws IOException {
                ...
            }
        });

and again for the second request: 并再次为第二个请求:

okHttpClient.newCall(request2).enqueue(new Callback() {
                @Override
                public void onFailure(Request request, Throwable throwable) {
                    ...
                }

                @Override
                public void onResponse(Response response) throws IOException {
                    ...
                }
            });

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

相关问题 我怎么知道我正在使用什么 jaxrs 实现? - How do I know what implementation of jaxrs I am using? 如何知道我是否在 Android 上通话? - How to know whether I am in a call on Android? 我想做一些我不知道该怎么称呼的东西 - I want to make something that i do not know what to call 我怎么知道我正在运行哪个版本的RichFaces? - How do I know which version of RichFaces I am running? 我怎么知道这是一种什么样的方法? - how do i know what kind of method is this? 为什么我从OkHttp请求中收到协议错误? - why am I getting a PROTOCOL ERROR from OkHttp request? 我试图调用的方法不是返回值 - The method I am trying to call is not returning a value 我正在为我的班级使用 java 在 JGrasp 中制作一个神奇的 8 球。 如何让我的代码运行? 我不知道我的错误是什么或如何解决它? - I am working on making a magic 8 ball in JGrasp using java for my class. how do I get my code to run? I do not know what my error is or how to fix it? 在将自定义对象返回到m流中时,我做错了什么?如何返回另一个an流可以消耗的对象? - What am I doing wrong in returning a custom object to a mule flow and How do I return an object that can be consumed by another mule flow? 我正在尝试打开我的文本文件,但我不知道问题出在哪里 - I am trying to open my text file but I do not know what the issue is
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM