Android okHttp-如何识别响应是针对哪个请求的?

时间:2019-04-11 16:05:51

标签: android okhttp

如果我在okHttpClient中创建多个请求,如何确定响应是针对哪个请求的? 请不要告诉我在enqueue方法中实现侦听器。

有什么解决办法吗?

public class OkHttpJavaActivity extends AppCompatActivity implements Callback {

    Request request1;
    Request request2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ok_http_java);

        String url1 = "https://reqres.in/api/users?page=1";
        String url2 = "https://reqres.in/api/unknown/2";
        OkHttpClient client = new OkHttpClient();

        request1 = new Request.Builder().url(url1).build();
        request2 = new Request.Builder().url(url2).build();

        client.newCall(request1).enqueue(this);
        client.newCall(request2).enqueue(this);
    }

    @Override
    public void onFailure(Call call, IOException e) {
        Log.e("ABC", "DEF");
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        Log.e("ABC", "DEF");
    }
}

1 个答案:

答案 0 :(得分:0)

call.request()。url应该可以工作