项目用到okhttp,需要进行封装以便调用。难点在于请求体的多种类型有的采用表单,有的采用json,有的是文件。代码如下:
public class OkHttpManger { public static OkHttpClient sOkHttpClient = new OkHttpClient.Builder() .addInterceptor(new Interceptor() { public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Response response = chain.proceed(request); return response; } }) .connectTimeout(4000, TimeUnit.MILLISECONDS) .readTimeout(4000, TimeUnit.MILLISECONDS) .writeTimeout(4000, TimeUnit.MILLISECONDS) .build(); /** * 适用于需要传参数和json对象的接口 * Post 异步请求 * * @param apiId * @param map 参数 * @param jsonString json字符串 * @param callback 异步回调 */ public static void asyPostJson(String apiId, Map<String, String> map, String jsonString, Callback callback) { if (TextUtils.isEmpty(jsonString)) { return; } MediaType JSON = MediaType.parse("application/json; charset=utf-8"); RequestBody requestBody = RequestBody.create(JSON, jsonString); Log.e("======>>", apiId + " 请求json:" + jsonString); String url = NetConfig.getUrl(apiId) + "?"; if (map != null) { // map 里面是请求中所需要的 key 和 value Set<Map.Entry<String, String>> entries = map.entrySet(); for (Map.Entry entry : entries) { String key = valueOf(entry.getKey()); String value = valueOf(entry.getValue()); url = url + "&" + key + "=" + value; } } Log.e("", "============URL============" + apiId + ":" + url); Request request = new Request.Builder() .url(url) .post(requestBody) .build(); sOkHttpClient.newCall(request).enqueue(callback); } /** * delete异步请求 * * @param apiId * @param map 无值可传null * @param callback */ private void delete(String apiId, Map<String, String> map, Callback callback) { FormBody.Builder builder = new FormBody.Builder(); for (String key : map.keySet()) { builder.add(key, map.get(key)); } Log.e("", "============URL============" + apiId + ":" + NetConfig.getUrl(apiId)); Request request = new Request.Builder() .url(NetConfig.getUrl(apiId)) .delete(builder.build()) .build(); sOkHttpClient.newCall(request).enqueue(callback); } /** * Get异步请求 * * @param apiId * @param map 无值可传null * @param callback */ public static void asyGet(String apiId, Map<String, String> map, Callback callback) { String url = NetConfig.getUrl(apiId); for (String key : map.keySet()) { url = url + "&" + key + "=" + map.get(key); } Log.e("", "============URL============" + apiId + ":" + url); Request request = new Request.Builder() .url(url) .get() .build(); sOkHttpClient.newCall(request).enqueue(callback); } /** * Post异步请求 * * @param apiId * @param map 无值可传null * @param callback */ public static void asyPost(String apiId, Map<String, String> map, Callback callback) { FormBody.Builder builder = new FormBody.Builder(); for (String key : map.keySet()) { builder.add(key, map.get(key)); } Log.e("", "============URL============" + apiId + ":" + NetConfig.getUrl(apiId)); Request request = new Request.Builder() .url(NetConfig.getUrl(apiId)) .post(builder.build()) .build(); sOkHttpClient.newCall(request).enqueue(callback); } /** * 单图上传方法,供参考 * 已测试A3033P094与A3033P105接口 * * @param map 参数类似apikey * @param file 图片文件 * @param imagekey 图片key,一般为"image" * @param callback * @ param url */ public static void postFile(final String apiId, final Map<String, String> map, File file, String imagekey, Callback callback) { MultipartBody.Builder requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM); if (file != null) { RequestBody body = RequestBody.create(MediaType.parse("multipart-formdata"), file); String filename = file.getName(); // 参数分别为, 请求key ,文件名称 , RequestBody requestBody.addFormDataPart(imagekey, filename, body); } String url = NetConfig.getUrl(apiId) + "?"; if (map != null) { // map 里面是请求中所需要的 key 和 value Set<Map.Entry<String, String>> entries = map.entrySet(); for (Map.Entry entry : entries) { String key = valueOf(entry.getKey()); String value = valueOf(entry.getValue()); // requestBody.addFormDataPart(key, value); url = url + "&" + key + "=" + value; } } Log.e("==========URL==========", "" + apiId + ":" + url); Request request = new Request.Builder().url(url).post(requestBody.build()).build(); sOkHttpClient.newCall(request).enqueue(callback); } /** * 多个图片表单形式上传,未测试 * * @param apiId * @param map apikey之类参数 * @param filemap 多张图片的key与值 * @param callback 回调 */ public static void postMoreFile(String apiId, Map<String, String> map, Map<String, File> filemap, Callback callback) { MultipartBody.Builder requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM); if (filemap != null) { for (String key : filemap.keySet()) { File file = filemap.get(key); if (file != null) { RequestBody body = RequestBody.create(MediaType.parse("multipart-formdata"), file); String filename = file.getName(); // 参数分别为, 请求key ,文件名称 , RequestBody requestBody.addFormDataPart(key, filename, body); } } } String url = NetConfig.getUrl(apiId) + "?"; if (map != null) { // map 里面是请求中所需要的 key 和 value Set<Map.Entry<String, String>> entries = map.entrySet(); for (Map.Entry entry : entries) { String key = valueOf(entry.getKey()); String value = valueOf(entry.getValue()); // requestBody.addFormDataPart(key, value); url = url + "&" + key + "=" + value; } } Log.e("==========URL==========", "" + apiId + ":" + url); Request request = new Request.Builder().url(url).post(requestBody.build()).build(); sOkHttpClient.newCall(request).enqueue(callback); } private static void postJson(final String url, final Map<String, String> map, String jsonstring, Callback callback) { OkHttpClient client = new OkHttpClient(); // form 表单形式上传 MultipartBody.Builder requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM); if (jsonstring != null) { MediaType JSON = MediaType.parse("application/json; charset=utf-8"); RequestBody body1 = RequestBody.create(JSON, jsonstring); requestBody.addPart(body1); } if (map != null) { // map 里面是请求中所需要的 key 和 value Set<Map.Entry<String, String>> entries = map.entrySet(); for (Map.Entry entry : entries) { String key = valueOf(entry.getKey()); String value = valueOf(entry.getValue()); Log.d("HttpUtils", "key==" + key + "value==" + value); requestBody.addFormDataPart(key, value); } } Request request = new Request.Builder().url(url).post(requestBody.build()).build(); // readTimeout("请求超时时间" , 时间单位); client.newBuilder().readTimeout(5000, TimeUnit.MILLISECONDS).build().newCall(request).enqueue(callback); } }