okhttp3 获取requesBody中的数据

拦截器我就不说了,最近的需求是从检测requestBody发出去的一个参数,查了很多资料,都只是没有提到

直接上代码

RequestBody requestBody = request.body();
Buffer buffer = new Buffer();
try {
    requestBody.writeTo(buffer);
} catch (IOException e) {
    e.printStackTrace();
    return "";
}
//编码设为UTF-8
Charset charset = Charset.forName("UTF-8");
MediaType contentType = requestBody.contentType();
if (contentType != null) {
    charset = contentType.charset(Charset.forName("UTF-8"));
}
//拿到request
String requestString = buffer.readString(charset);

你可能感兴趣的:(android)