retrofit2.0中使用okHTTP post get put 过滤请求内容或添加内容

okClient.interceptors().add(new Interceptor() {
    @Override
    public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Request temRequest;
        Request.Builder tempBuilder = request.newBuilder();
        if (request.method().equals("GET")) {
            URL url = request.url();
            tempBuilder.url(url.toString() + SGIN);
        } else {
            RequestBody body = request.body();
            Buffer sink = new Buffer();
            body.writeTo(sink);
            byte[] content = sink.write(SGIN.getBytes()).readByteArray();
            Log.d("OkHttp", new String(content, "UTF-8"));
            RequestBody tempbody = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), content);
            if (request.method().equals("POST")) {
                tempBuilder.post(tempbody);
            } else if (request.method().equals("PUT")) {
                tempBuilder.put(tempbody);
            }
        }
        temRequest = tempBuilder.build();
        com.squareup.okhttp.Response response = chain.proceed(temRequest);
        return response;
    }
});



https://github.com/FangWW/RetrofitLib

你可能感兴趣的:(Android)