James Robert Singleton
James Robert Singleton

Reputation: 461

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 a a 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());

            }
        });
    }

Upvotes: 2

Views: 11038

Answers (2)

Mori
Mori

Reputation: 4661

If you want to avoid about empty result:

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

And if you want access to each elements :

JSONObject json = new JSONObject(r);
Log.i("love", "Res: "+json.getString("result")); //Name -> Answer

See the result:

{"fname":"John","sname":"Alice","percentage":"46","result":"Can choose someone better."} // String from 
I/love: Res: All the best! // Json form by Name

Upvotes: 2

Dhiraj Sharma
Dhiraj Sharma

Reputation: 4879

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

Upvotes: 3

Related Questions