public class NetUtilsOk { /** * get请求 * @param mUrl 请求地址 * @param okCallBack 回调接口 */ public static void getNetData(String mUrl, final OkCallBack okCallBack){ //build 一个请求 Request request = new Request.Builder() .get() .url(mUrl) .build(); //build 一个用户client OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(5000, TimeUnit.MILLISECONDS) .readTimeout(5000, TimeUnit.MILLISECONDS) .build(); // 用户client发出请求 Call call = client.newCall(request); call.enqueue(new Callback() { private String s = ""; @Override public void onFailure(Call call, IOException e) { okCallBack.onFailed("请求数据失败"); } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); okCallBack.onSuccess(string); } }); } /** * post请求 * @param mUrl 请求地址 * @param map 请求参数map * @param okCallBack 回调接口 */ public static void postNetData(String mUrl, Map<String,String> map, final OkCallBack okCallBack){ //build OkHttpClient 对象 client:客户, 委托人, 顾客, 名字很形象,一个客户 OkHttpClient client = new OkHttpClient.Builder() .readTimeout(5000, TimeUnit.MILLISECONDS) .connectTimeout(5000, TimeUnit.MILLISECONDS) .writeTimeout(5000,TimeUnit.MILLISECONDS) .build(); //build 一个 formbody的构造器 即请求需要的数据包构造器 FormBody.Builder builder = new FormBody.Builder(); //将map中的数据添加到 构造器里 if (map!=null) { for (Map.Entry<String,String> entry:map.entrySet()){ builder.add(entry.getKey(),entry.getValue()); } } //通过构造器 构造出formbody 即请求需要的数据包 FormBody body = builder.build(); //request设置数据、请求地址 Request request = new Request.Builder() .post(body) .url(mUrl) .build(); //client用户 发出request请求 Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { okCallBack.onFailed("请求失败"); } @Override public void onResponse(Call call, Response response) throws IOException { okCallBack.onSuccess(response.body().string()); } }); } /** * 回调接口 */ public interface OkCallBack{ public void onSuccess(String s); public void onFailed(String error); }
/** * 自定义拦截器 */ public class PublicParamInterceptor implements Interceptor{ private Map<String,String> paramMap = new HashMap<String, String>(); public PublicParamInterceptor(Map<String, String> paramMap) { this.paramMap = paramMap; } @Override public Response intercept(Chain chain) throws IOException { //获取之前的request Request oldRequest = chain.request(); //获取请求url String mUrl = oldRequest.url().toString(); //判断请求方式 if(oldRequest.method().equalsIgnoreCase("GET")){ if(paramMap!=null&¶mMap.size()>0){ StringBuffer stringBuffer = new StringBuffer(); //拼接公共请求参数 for (Map.Entry<String,String> entry:paramMap.entrySet()){ stringBuffer.append("&"+entry.getKey()+"="+entry.getValue()); } mUrl = stringBuffer.toString(); if(!mUrl.contains("?")){ mUrl.replaceFirst("&","?"); } //依据原来的request构造一个新的request Request newRequest = oldRequest.newBuilder() .get() .url(mUrl) .build(); return chain.proceed(newRequest); } }else { if(paramMap!=null&¶mMap.size()>0){ RequestBody body = oldRequest.body(); FormBody formBody = (FormBody) body; //将之前body里的参数添加到新的body中 FormBody.Builder builder = new FormBody.Builder(); //防止参数重复 Map<String,String> tempMap = new HashMap<String, String>(); for (int i = 0; i < formBody.size(); i++) { builder.add(formBody.name(i),formBody.value(i)); tempMap.put(formBody.name(i),formBody.value(i)); } //将要添加的参数添加到新的body中 for (Map.Entry<String,String> entry:paramMap.entrySet()) { if(!tempMap.containsKey(entry.getKey())){ tempMap.put(entry.getKey(),entry.getValue()); builder.add(entry.getKey(),entry.getValue()); } } FormBody newBody = builder.build(); Request newRequest = oldRequest.newBuilder() .post(newBody) .build(); return chain.proceed(newRequest); } } return chain.proceed(oldRequest); } }}