简体   繁体   中英

Accessing OKHttp Response Body

So I need to figure out how to access the value I get from my first response in my second. I would think that I could just store it to aa variable and access it in another request. However, that does not seem to be the case.

Here is the bit that is giving me issues. So my first request is getting me a token and then I need to use that which is stored in commatoken in my second request.

private final OkHttpClient client = new OkHttpClient();

    public void run() throws Exception {
        Request request = new Request.Builder()
                .url(API_URL + authPreferences.getToken())
                .build();

        client.newCall(request).enqueue(new Callback() {
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }


            public void onResponse(Call call, Response response) throws IOException {
                if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

                Headers responseHeaders = response.headers();
                for (int i = 0, size = responseHeaders.size(); i < size; i++) {
                    System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
                }

                System.out.println(response.body().string());
                String commatoken = response.body().string();
            }
        });

        Request dataRequest = new Request.Builder()
                .header("Authorization", "jwt"+commatoken)
                .url(ChffrMe_URL).build();

        client.newCall(dataRequest).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

                Headers responseHeaders = response.headers();
                for (int i = 0, size = responseHeaders.size(); i < size; i++) {
                    System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
                }

                System.out.println(response.body().string());

            }
        });
    }

I think we can call response.body().string() only once .... so save that data to a string variable first .. and access it wherever you need it.

String response_data;
..............
response_data = response.body().string();

You are calling response.body().string() twice ...

More info https://stackoverflow.com/a/27922818/3552066

If you want to avoid about empty result:

assert response.body() != null;
String r = response.body().string();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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