所以我需要弄清楚如何在第二个响应中访问我从第一个响应中获得的值。我认为我可以将其存储到一个变量中并在另一个请求中访问它。然而,情况似乎并非如此。
这是给我带来问题的一点。因此,我的第一个请求是获取一个令牌,然后我需要在第二个请求中使用存储在 commatoken 中的令牌。
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());
}
});
}
我想我们可以打电话response.body().string()
仅一次......因此首先将该数据保存到字符串变量中......并在需要时访问它。
String response_data;
..............
response_data = response.body().string();
你正在呼唤响应.body().string()两次...
更多信息https://stackoverflow.com/a/27922818/3552066 https://stackoverflow.com/a/27922818/3552066
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)