okhttp新增自定義攔截器,封裝公共請求引數
阿新 • • 發佈:2018-12-20
okhttp 進行網路請求
/** * date:2018/11/22 * author:QMY(QMY) * function: */ public class OkhttpUtils { Handler handler; OkHttpClient okHttpClient; private static OkhttpUtils mOkhttpUtils; //構造方法私有 private OkhttpUtils() { Map<String,String> map = new HashMap<>(); map.put("source","android"); MyInterceptor myInterceptor = new MyInterceptor(map); handler = new Handler(Looper.getMainLooper()); //建立okhttp okHttpClient = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) .readTimeout(10, TimeUnit.SECONDS) .addInterceptor(myInterceptor) .build(); } //單例模式 public static OkhttpUtils getInstance(){ if (mOkhttpUtils==null){ synchronized (OkhttpUtils.class){ if (mOkhttpUtils==null){ return mOkhttpUtils = new OkhttpUtils(); } } } return mOkhttpUtils; } //介面 public interface Utils{ void success(String string); } //封裝doget方法 public void doGet(String url, final Utils mUtils){ final Request request = new Request.Builder() .url(url) .build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(okhttp3.Call call, IOException e) { } @Override public void onResponse(okhttp3.Call call, Response response) throws IOException { final String string = response.body().string(); handler.post(new Runnable() { @Override public void run() { //介面 mUtils.success(string); } }); } }); } //封裝doPost public void doPost(String url, Map<String,String> map, final Utils mUtils){ //formbody FormBody.Builder builder = new FormBody.Builder(); for (String key:map.keySet()){ builder.add(key,map.get(key)); } FormBody formBody = builder.build(); final Request request = new Request.Builder() .post(formBody) .url(url) .build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(okhttp3.Call call, IOException e) { } @Override public void onResponse(okhttp3.Call call, Response response) throws IOException { final String string = response.body().string(); handler.post(new Runnable() { @Override public void run() { //介面 mUtils.success(string); } }); } }); } }
自定義攔截器
/** * date:2018/11/22 * author:QMY(QMY) * function:自定義一個攔截器,封裝公共請求引數 */ public class MyInterceptor implements Interceptor { private final Map<String, String> map; public MyInterceptor(Map<String, String> map) { this.map=map; } @Override public Response intercept(Chain chain) throws IOException { //拿到原來的request Request oldrequest = chain.request(); //拿到請求的url String url = oldrequest.url().toString(); //判斷是get還是post if (oldrequest.method().equalsIgnoreCase("GET")) { if (map!=null && map.size()>0){ StringBuilder stringBuilder = new StringBuilder(url); //拼接公共請求引數 for(Map.Entry<String,String> entry:map.entrySet()){ stringBuilder.append("&"+entry.getKey()+ "=" + entry.getValue()); } url = stringBuilder.toString(); //如果之前的url沒有?號,我們需要手動給他新增一個?號 if (!url.contains("?")){ url = url.replaceFirst("&", "?"); } //依據原來的request構造一個新的request Request request = oldrequest.newBuilder() .url(url) .build(); return chain.proceed(request); }else { if (map!=null&& map.size()>0){ RequestBody body = oldrequest.body(); if (body!=null && body instanceof FormBody){ FormBody formBody = (FormBody) body; //1.把原來的的body裡面的引數新增到新的body中 FormBody.Builder builder = new FormBody.Builder(); //為了防止重複新增相同的key和value Map<String,String> temmap = new HashMap<>(); for(int i=0;i<formBody.size() ; i++){ builder.add(formBody.encodedName(i), formBody.encodedValue(i)); temmap.put(formBody.encodedName(i), formBody.encodedValue(i)); } //2.把公共請求引數新增到新的body中 for (Map.Entry<String, String> entry : map.entrySet()) { if(!temmap.containsKey(entry.getKey())){ builder.add(entry.getKey(), entry.getValue()); } } FormBody newFormBody = builder.build(); //依據原來的request構造一個新的request, Request newRequest = oldrequest.newBuilder() .post(newFormBody) .build(); return chain.proceed(newRequest); } } } } return chain.proceed(oldrequest); } }