Request request = new Request.Builder()
.url("http://example.com")
.post(RequestBody.create("foo",MediaType.get("text/plain")))
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//e.printStackTrace();
RequestBody b = call.request().body();
String t= b.toString(); ??? <-How would I get the original message?
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//do something
}
});
如果失败,我会尝试保存我发送的原始消息。
我查看了这些函数,但似乎无法弄清楚如何获取我在回调中发送的原始消息。有没有办法取回消息?
最佳答案
如果有人正在寻找这个:
Buffer b = new Buffer();
call.request().body().writeTo(b)
String result=b.readString(Charset.defaultCharset()); //Change this to whatever your character set is
关于java - 有没有办法从 okhttp3 的失败中获取原始的 http 发布消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66179933/