public class OkHttpUtils { /** * 懒汉 安全 加同步 * 1.私有的静态成员变量 只声明不创建 * 2.私有的构造方法 * 3.提供返回实例的静态方法 */ private static OkHttpClient okHttpClient = null; private OkHttpUtils() { } public static OkHttpClient getInstance() { if (okHttpClient == null) { //加同步安全 synchronized (OkHttpUtils.class) { if (okHttpClient == null) { //okhttp可以缓存数据....指定缓存路径 okHttpClient = new OkHttpClient.Builder()//构建器 .proxy(Proxy.NO_PROXY) //来屏蔽系统代理 .connectTimeout(10, TimeUnit.SECONDS)//连接超时 .writeTimeout(10, TimeUnit.SECONDS)//写入超时 .readTimeout(10, TimeUnit.SECONDS)//读取超时 .addInterceptor(new CommonParamsInterceptor())//添加应用拦截器的方法 .build(); } } } return okHttpClient; } /** * post请求上传文件....包括图片....流的形式传任意文件... * 参数1 url * file表示上传的文件 * fileName....文件的名字,,例如aaa.jpg * params ....传递除了file文件 其他的参数放到map集合 */ public static void uploadFile(String url, File file, String fileName, Map<String, String> params, Callback callback) { //创建OkHttpClient请求对象 OkHttpClient okHttpClient = getInstance(); //MultipartBody多功能的请求实体对象,,,formBody只能传表单形式的数据 MultipartBody.Builder builder = new MultipartBody.Builder(); builder.setType(MultipartBody.FORM); //参数 if (params != null) { for (String key : params.keySet()) { builder.addFormDataPart(key, params.get(key)); } } //文件...参数name指的是请求路径中所接受的参数...如果路径接收参数键值是fileeeee,此处应该改变 builder.addFormDataPart("file", fileName, RequestBody.create(MediaType.parse("application/octet-stream"), file)); //构建 MultipartBody multipartBody = builder.build(); //创建Request Request request = new Request.Builder().url(url).post(multipartBody).build(); //得到Call Call call = okHttpClient.newCall(request); //执行请求 call.enqueue(callback); } /** * Post请求发送JSON数据....{"name":"zhangsan","pwd":"123456"} * 参数一:请求Url * 参数二:请求的JSON * 参数三:请求回调 */ public static void doPostJson(String url, String jsonParams, Callback callback) { RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonParams); Request request = new Request.Builder().url(url).post(requestBody).build(); Call call = getInstance().newCall(request); call.enqueue(callback); } /** * 下载文件 以流的形式把apk写入的指定文件 得到file后进行安装 * 参数er:请求Url * saveDir:保存文件的文件夹....download */ public static void download(final String url, final String saveDir, OnDownloadListener downloadListener) { //get请求 Request request = new Request.Builder().url(url).build(); Call call = getInstance().newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, final Response response) throws IOException { InputStream is = null; byte[] buf = new byte[2048]; int len = 0; FileOutputStream fos = null; try { //response.body().string()...json限制是1M大小...byteStream()流的方式 is = response.body().byteStream();//以字节流的形式拿回响应实体内容 AppLog.e(getClass(), "read file length--->" + is.available()); long total = response.body().contentLength(); //保存路径 final String fileDir = isExistDir(saveDir); File file = new File(fileDir, getNameFromUrl(url)); fos = new FileOutputStream(file); long sum = 0; while ((len = is.read(buf)) != -1) { fos.write(buf, 0, len); sum += len; int progress = (int) (sum * 1.0f / total * 100); downloadListener.onDownloading(progress); } fos.flush(); AppLog.e(getClass(), "down load over"); downloadListener.onDownloadSuccess(fileDir + "/" + getNameFromUrl(url)); } catch (IOException e) { downloadListener.onDownloadFailed(); e.printStackTrace(); } finally { if (is != null) is.close(); if (fos != null) fos.close(); } } }); } /** * 判断下载目录是否存在......并返回绝对路径 * * @param saveDir * @return * @throws IOException */ public static String isExistDir(String saveDir) throws IOException { // 下载位置 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File downloadFile = new File(Environment.getExternalStorageDirectory(), saveDir); if (!downloadFile.mkdirs()) { downloadFile.createNewFile(); } String savePath = downloadFile.getAbsolutePath(); return savePath; } return null; } /** * @param url * @return 从下载连接中解析出文件名 */ private static String getNameFromUrl(String url) { return url.substring(url.lastIndexOf("/") + 1); } /** * 封装公共参数的拦截器 */ private static class CommonParamsInterceptor implements Interceptor { //intercept方法就是拦截的意思....拦截的是一个请求,,,一旦拦截之后可以对请求的参数进行处理 //Chain chain 链条的意思 @Override public Response intercept(Chain chain) throws IOException { Log.e("----", "拦截器------"); //调用request()方法得到拦截的请求 Request request = chain.request(); //获取请求的方式 String method = request.method();//GET POST //拦截的请求的路径 String oldUrl = request.url().toString(); //要添加的公共参数...map Map<String, String> map = new HashMap<>(); map.put("source", "android"); if ("GET".equals(method)) { StringBuilder stringBuilder = new StringBuilder();//创建一个stringBuilder...字符串缓冲区 stringBuilder.append(oldUrl); if (oldUrl.contains("?")) { //?在最后面....2类型 if (oldUrl.indexOf("?") == oldUrl.length() - 1) { } else { //3类型...拼接上& stringBuilder.append("&"); } } else { //不包含? 属于1类型,,,先拼接上?号 stringBuilder.append("?"); } //添加公共参数....source=android&appVersion=100& for (Map.Entry<String, String> entry : map.entrySet()) { //拼接 stringBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("&"); } //删掉最后一个&符号 if (stringBuilder.indexOf("&") != -1) { stringBuilder.deleteCharAt(stringBuilder.lastIndexOf("&")); } //得到含有公共参数的新路径.....使用新路径去请求 String newUrl = stringBuilder.toString(); //新的路径构建一个新的请求 request = request.newBuilder().url(newUrl).build(); } else if ("POST".equals(method)) { Log.e("--oldUrl--", oldUrl); //参数在请求的实体内容上....拿到以前的参数,把以前的参数和公共参数全都添加到请求实体上 RequestBody requestBody = request.body(); if (requestBody instanceof FormBody) {//前边是不是后边的子类/实例对象 FormBody oldBody = (FormBody) requestBody;//keywords...page FormBody.Builder builder = new FormBody.Builder(); //先添加之前的参数 for (int i = 0; i < oldBody.size(); i++) { //键值对的形式添加 builder.add(oldBody.name(i), oldBody.value(i)); } //添加公共参数 for (Map.Entry<String, String> entry : map.entrySet()) { builder.add(entry.getKey(), entry.getValue()); } //构建一个新的请求 request = request.newBuilder().url(oldUrl).post(builder.build()).build(); } } //执行请求 获取到响应 Response response = chain.proceed(request); return response; } } }