Interceptor 網路攔截器 切換網路請求
阿新 • • 發佈:2019-01-24
package com.mylibrary; import android.text.TextUtils; import android.util.Log; import java.io.IOException; import java.nio.charset.Charset; import okhttp3.Interceptor; import okhttp3.MediaType; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import okhttp3.ResponseBody; import okio.Buffer; import okio.BufferedSource; /** * @author lsw * @date 2018/4/19 */ public class MyLoggingInterceptor implements Interceptor { private String bearer; private String tag = "lsw"; private String appType; public MyLoggingInterceptor(String bearer) { this.bearer = bearer; } @Override public Response intercept(Chain chain) throws IOException { Request.Builder newBuilder = chain .request() .newBuilder(); Request request; if (!bearer.isEmpty()) { request = newBuilder .addHeader("Authorization", bearer) // .addHeader("appType",appType) .build(); } else { request = newBuilder.build(); } String cacheControl = request.cacheControl().toString(); if (TextUtils.isEmpty(cacheControl)) { cacheControl = "public, max-age=60"; } Response response = chain.proceed(request); Log.e(tag, "========response' code : " + response.code()); if(response.code() == 200){ Response.Builder builder = response.newBuilder(); Response clone = builder.build(); ResponseBody body = clone.body(); if (body != null) { MediaType mediaType = body.contentType(); if (mediaType != null) { // Log.e(tag, "responseBody's contentType : " + mediaType.toString()); if (isText(mediaType)) { String resp = body.string(); body = ResponseBody.create(mediaType, resp); return response.newBuilder().body(body).build(); } else { Log.e(tag, "responseBody's content : " + " maybe [file part] , too large too print , ignored!"); } } } }else{ // Log.e(tag, "呼叫檢測介面: " ); //修改網路 請求 String json = "{ \"body\": {\"appType\": \""+appType+"\",\"versionType\": \"2\" },\"head\": {}}" ; // Log.e(tag, "呼叫檢測介面: " + bearer); // Log.e(tag, "json -- : " + json); RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json); Request newRequest = new Request.Builder() .addHeader("Authorization", this.bearer) .url("http://192.168.0.1/openapi/oauth/service/XXX").post(body).build(); response = chain.proceed(newRequest); // Log.e(tag, "body : " + response.body().toString()); // Log.e(tag, "response code new : " + response.code()); //新增列印伺服器返回的資料 ResponseBody responseBody = response.body(); long contentLength = responseBody.contentLength(); BufferedSource source = responseBody.source(); source.request(Integer.MAX_VALUE); // Buffer the entire body. Buffer buffer = source.buffer(); if (contentLength != 0) { Log.e("伺服器返回資料 二:", ""+buffer.clone().readString(Charset.forName("UTF-8"))); } } // return response. // newBuilder() // .header("Cache-Control", cacheControl) // .build(); return response; } private boolean isText(MediaType mediaType) { if (mediaType.type() != null && mediaType.type().equals("text")) { return true; } if (mediaType.subtype() != null) { if (mediaType.subtype().equals("json") || mediaType.subtype().equals("xml") || mediaType.subtype().equals("html") || mediaType.subtype().equals("webviewhtml") ) return true; } return false; } public void setBearer(String bearer,String appType) { this.bearer = bearer; this.appType = appType; } public void setBearer(String bearer) { this.bearer = bearer; } }