From c1090f681d96f7af09b55791b6730b5e7735070e Mon Sep 17 00:00:00 2001 From: liaoziyao Date: Sun, 16 Jul 2017 22:53:22 +0800 Subject: [PATCH 1/3] fix some logic about isReplace --- .../lzy/demo/callback/EncryptCallback.java | 2 +- .../com/lzy/demo/okgo/FormUploadActivity.java | 2 +- .../java/com/lzy/demo/okgo/TestActivity.java | 18 ++ .../lzy/demo/okrx2/RxFormUploadActivity.java | 6 +- .../okgo/cache/policy/BaseCachePolicy.java | 2 +- .../java/com/lzy/okgo/model/HttpParams.java | 204 ++++++++++-------- .../lzy/okgo/request/base/BodyRequest.java | 35 +-- .../com/lzy/okgo/request/base/HasBody.java | 15 +- .../lzy/okgo/request/base/NoBodyRequest.java | 2 +- .../com/lzy/okgo/request/base/Request.java | 44 ++-- .../java/com/lzy/okgo/utils/HttpUtils.java | 8 +- 11 files changed, 193 insertions(+), 145 deletions(-) diff --git a/demo/src/main/java/com/lzy/demo/callback/EncryptCallback.java b/demo/src/main/java/com/lzy/demo/callback/EncryptCallback.java index 9e6c509d..fc2975c6 100644 --- a/demo/src/main/java/com/lzy/demo/callback/EncryptCallback.java +++ b/demo/src/main/java/com/lzy/demo/callback/EncryptCallback.java @@ -56,7 +56,7 @@ private void sign(HttpParams params) { params.put("timestamp", "" + (System.currentTimeMillis() / 1000L)); StringBuilder sb = new StringBuilder(); Map map = new HashMap<>(); - for (Map.Entry> entry : params.urlParamsMap.entrySet()) { + for (Map.Entry> entry : params.stringParamsMap.entrySet()) { map.put(entry.getKey(), entry.getValue().get(0)); } for (Map.Entry entry : getSortedMapByKey(map).entrySet()) { diff --git a/demo/src/main/java/com/lzy/demo/okgo/FormUploadActivity.java b/demo/src/main/java/com/lzy/demo/okgo/FormUploadActivity.java index a541d626..f3e4bc42 100644 --- a/demo/src/main/java/com/lzy/demo/okgo/FormUploadActivity.java +++ b/demo/src/main/java/com/lzy/demo/okgo/FormUploadActivity.java @@ -138,7 +138,7 @@ public void formUpload(View view) { // .params("file1",new File("文件路径")) //这种方式为一个key,对应一个文件 // .params("file2",new File("文件路径")) // .params("file3",new File("文件路径")) - .addFileParams("file", files) // 这种方式为同一个key,上传多个文件 + .paramsFileList("file", files) // 这种方式为同一个key,上传多个文件 .execute(new JsonCallback>() { @Override public void onStart(Request, ? extends Request> request) { diff --git a/demo/src/main/java/com/lzy/demo/okgo/TestActivity.java b/demo/src/main/java/com/lzy/demo/okgo/TestActivity.java index 0cfd6970..22393b58 100644 --- a/demo/src/main/java/com/lzy/demo/okgo/TestActivity.java +++ b/demo/src/main/java/com/lzy/demo/okgo/TestActivity.java @@ -27,13 +27,21 @@ import com.lzy.okgo.OkGo; import com.lzy.okgo.adapter.Call; import com.lzy.okgo.callback.StringCallback; +import com.lzy.okgo.convert.StringConvert; import com.lzy.okgo.model.HttpHeaders; import com.lzy.okgo.model.Response; +import com.lzy.okgo.request.PostRequest; +import com.lzy.okrx.adapter.ObservableBody; import org.json.JSONObject; +import java.io.File; +import java.util.HashMap; +import java.util.Map; + import butterknife.Bind; import butterknife.OnClick; +import rx.Observable; /** * ================================================ @@ -108,5 +116,15 @@ public void onSuccess(Response response) { } }); + + Map urlMaps = new HashMap<>(); + Map fileMaps = new HashMap<>(); + + Observable observable = OkGo.post("url") + .tag(this) + .paramsStringMap(urlMaps) + .paramsFileMap(fileMaps) + .converter(new StringConvert()) + .adapt(new ObservableBody()); } } diff --git a/demo/src/main/java/com/lzy/demo/okrx2/RxFormUploadActivity.java b/demo/src/main/java/com/lzy/demo/okrx2/RxFormUploadActivity.java index 75e5f2b0..798ccc49 100644 --- a/demo/src/main/java/com/lzy/demo/okrx2/RxFormUploadActivity.java +++ b/demo/src/main/java/com/lzy/demo/okrx2/RxFormUploadActivity.java @@ -132,7 +132,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { @OnClick(R.id.formUpload1) public void formUpload1(View view) { - ArrayList files = new ArrayList<>(); + List files = new ArrayList<>(); if (imageItems != null && imageItems.size() > 0) { for (int i = 0; i < imageItems.size(); i++) { files.add(new File(imageItems.get(i).path)); @@ -148,7 +148,7 @@ public void formUpload1(View view) { // .params("file1",new File("文件路径")) // .params("file2",new File("文件路径")) // .params("file3",new File("文件路径")) - .addFileParams("file", files)// + .paramsFileList("file", files)// .converter(new StringConvert())// .adapt(new ObservableResponse())// .doOnSubscribe(new Consumer() { @@ -206,7 +206,7 @@ public void subscribe(@NonNull final ObservableEmitter e) throws Excep //.params("file1",new File("文件路径")) //.params("file2",new File("文件路径")) //.params("file3",new File("文件路径")) - .addFileParams("file", files)// + .paramsFileList("file", files)// .execute(new StringCallback() { @Override public void onSuccess(Response response) { diff --git a/okgo/src/main/java/com/lzy/okgo/cache/policy/BaseCachePolicy.java b/okgo/src/main/java/com/lzy/okgo/cache/policy/BaseCachePolicy.java index f43aad35..268ebc17 100644 --- a/okgo/src/main/java/com/lzy/okgo/cache/policy/BaseCachePolicy.java +++ b/okgo/src/main/java/com/lzy/okgo/cache/policy/BaseCachePolicy.java @@ -66,7 +66,7 @@ public boolean onAnalysisResponse(Call call, okhttp3.Response response) { public CacheEntity prepareCache() { //check the config of cache if (request.getCacheKey() == null) { - request.cacheKey(HttpUtils.createUrlFromParams(request.getBaseUrl(), request.getParams().urlParamsMap)); + request.cacheKey(HttpUtils.createUrlFromParams(request.getBaseUrl(), request.getParams().stringParamsMap)); } if (request.getCacheMode() == null) { request.cacheMode(CacheMode.NO_CACHE); diff --git a/okgo/src/main/java/com/lzy/okgo/model/HttpParams.java b/okgo/src/main/java/com/lzy/okgo/model/HttpParams.java index 4853a140..da33ad97 100644 --- a/okgo/src/main/java/com/lzy/okgo/model/HttpParams.java +++ b/okgo/src/main/java/com/lzy/okgo/model/HttpParams.java @@ -48,11 +48,8 @@ public class HttpParams implements Serializable { public static final boolean IS_REPLACE = true; - /** 普通的键值对参数 */ - public LinkedHashMap> urlParamsMap; - - /** 文件的键值对参数 */ - public LinkedHashMap> fileParamsMap; + public LinkedHashMap> stringParamsMap; //普通的键值对参数 + public LinkedHashMap> fileParamsMap; //文件的键值对参数 public HttpParams() { init(); @@ -65,147 +62,168 @@ public HttpParams(String key, String value) { public HttpParams(String key, File file) { init(); - put(key, file); + put(key, file, IS_REPLACE); } private void init() { - urlParamsMap = new LinkedHashMap<>(); + stringParamsMap = new LinkedHashMap<>(); fileParamsMap = new LinkedHashMap<>(); } public void put(HttpParams params) { - if (params != null) { - if (params.urlParamsMap != null && !params.urlParamsMap.isEmpty()) urlParamsMap.putAll(params.urlParamsMap); - if (params.fileParamsMap != null && !params.fileParamsMap.isEmpty()) fileParamsMap.putAll(params.fileParamsMap); - } - } + if (params == null) return; - public void put(Map params, boolean... isReplace) { - if (params == null || params.isEmpty()) return; - for (Map.Entry entry : params.entrySet()) { - put(entry.getKey(), entry.getValue(), isReplace); + if (params.stringParamsMap != null && !params.stringParamsMap.isEmpty()) { + stringParamsMap.putAll(params.stringParamsMap); } - } - - public void put(String key, String value, boolean... isReplace) { - if (isReplace != null && isReplace.length > 0) { - put(key, value, isReplace[0]); - } else { - put(key, value, IS_REPLACE); + if (params.fileParamsMap != null && !params.fileParamsMap.isEmpty()) { + fileParamsMap.putAll(params.fileParamsMap); } } public void put(String key, int value, boolean... isReplace) { - if (isReplace != null && isReplace.length > 0) { - put(key, String.valueOf(value), isReplace[0]); - } else { - put(key, String.valueOf(value), IS_REPLACE); - } + put(key, String.valueOf(value), isReplace); } public void put(String key, long value, boolean... isReplace) { - if (isReplace != null && isReplace.length > 0) { - put(key, String.valueOf(value), isReplace[0]); - } else { - put(key, String.valueOf(value), IS_REPLACE); - } + put(key, String.valueOf(value), isReplace); } public void put(String key, float value, boolean... isReplace) { - if (isReplace != null && isReplace.length > 0) { - put(key, String.valueOf(value), isReplace[0]); - } else { - put(key, String.valueOf(value), IS_REPLACE); - } + put(key, String.valueOf(value), isReplace); } public void put(String key, double value, boolean... isReplace) { - if (isReplace != null && isReplace.length > 0) { - put(key, String.valueOf(value), isReplace[0]); - } else { - put(key, String.valueOf(value), IS_REPLACE); - } + put(key, String.valueOf(value), isReplace); } public void put(String key, char value, boolean... isReplace) { - if (isReplace != null && isReplace.length > 0) { - put(key, String.valueOf(value), isReplace[0]); - } else { - put(key, String.valueOf(value), IS_REPLACE); - } + put(key, String.valueOf(value), isReplace); } public void put(String key, boolean value, boolean... isReplace) { + put(key, String.valueOf(value), isReplace); + } + + public void put(String key, String value, boolean... isReplace) { + if (key == null || value == null) return; + + boolean replace = IS_REPLACE; if (isReplace != null && isReplace.length > 0) { - put(key, String.valueOf(value), isReplace[0]); - } else { - put(key, String.valueOf(value), IS_REPLACE); + replace = isReplace[0]; } + + List stringParams = stringParamsMap.get(key); + if (stringParams == null) { + stringParams = new ArrayList<>(); + stringParamsMap.put(key, stringParams); + } + if (replace) stringParams.clear(); + stringParams.add(value); } - private void put(String key, String value, boolean isReplace) { - if (key != null && value != null) { - List urlValues = urlParamsMap.get(key); - if (urlValues == null) { - urlValues = new ArrayList<>(); - urlParamsMap.put(key, urlValues); - } - if (isReplace) urlValues.clear(); - urlValues.add(value); + public void putStringMap(Map params, boolean... isReplace) { + if (params == null || params.isEmpty()) return; + + for (Map.Entry entry : params.entrySet()) { + put(entry.getKey(), entry.getValue(), isReplace); } } - public void putUrlParams(String key, List values) { - if (key != null && values != null && !values.isEmpty()) { - for (String value : values) { - put(key, value, false); - } + public void putStringList(String key, List values, boolean... isReplace) { + if (key == null || values == null || values.isEmpty()) return; + + boolean replace = IS_REPLACE; + if (isReplace != null && isReplace.length > 0) { + replace = isReplace[0]; + } + if (replace) { + List params = stringParamsMap.get(key); + if (params != null) params.clear(); + } + + for (String value : values) { + put(key, value, false); } } - public void put(String key, File file) { - put(key, file, file.getName()); + public void put(String key, File file, boolean... isReplace) { + if (file == null) return; + + put(key, file, file.getName(), isReplace); + } + + public void put(String key, File file, String fileName, boolean... isReplace) { + put(key, file, fileName, HttpUtils.guessMimeType(fileName), isReplace); } - public void put(String key, File file, String fileName) { - put(key, file, fileName, HttpUtils.guessMimeType(fileName)); + public void put(String key, FileWrapper fileWrapper, boolean... isReplace) { + if (fileWrapper == null) return; + + put(key, fileWrapper.file, fileWrapper.fileName, fileWrapper.contentType, isReplace); } - public void put(String key, FileWrapper fileWrapper) { - if (key != null && fileWrapper != null) { - put(key, fileWrapper.file, fileWrapper.fileName, fileWrapper.contentType); + public void put(String key, File file, String fileName, MediaType contentType, boolean... isReplace) { + if (key == null || file == null) return; + + boolean replace = IS_REPLACE; + if (isReplace != null && isReplace.length > 0) { + replace = isReplace[0]; + } + + List fileParams = fileParamsMap.get(key); + if (fileParams == null) { + fileParams = new ArrayList<>(); + fileParamsMap.put(key, fileParams); } + if (replace) fileParams.clear(); + fileParams.add(new FileWrapper(file, fileName, contentType)); } - public void put(String key, File file, String fileName, MediaType contentType) { - if (key != null) { - List fileWrappers = fileParamsMap.get(key); - if (fileWrappers == null) { - fileWrappers = new ArrayList<>(); - fileParamsMap.put(key, fileWrappers); - } - fileWrappers.add(new FileWrapper(file, fileName, contentType)); + public void putFileMap(Map params, boolean... isReplace) { + if (params == null || params.isEmpty()) return; + + for (Map.Entry entry : params.entrySet()) { + put(entry.getKey(), entry.getValue(), isReplace); } } - public void putFileParams(String key, List files) { - if (key != null && files != null && !files.isEmpty()) { - for (File file : files) { - put(key, file); - } + public void putFileList(String key, List files, boolean... isReplace) { + if (key == null || files == null || files.isEmpty()) return; + + boolean replace = IS_REPLACE; + if (isReplace != null && isReplace.length > 0) { + replace = isReplace[0]; + } + if (replace) { + List params = fileParamsMap.get(key); + if (params != null) params.clear(); + } + + for (File file : files) { + put(key, file, false); } } - public void putFileWrapperParams(String key, List fileWrappers) { - if (key != null && fileWrappers != null && !fileWrappers.isEmpty()) { - for (FileWrapper fileWrapper : fileWrappers) { - put(key, fileWrapper); - } + public void putFileWrapperList(String key, List fileWrappers, boolean... isReplace) { + if (key == null || fileWrappers == null || fileWrappers.isEmpty()) return; + + boolean replace = IS_REPLACE; + if (isReplace != null && isReplace.length > 0) { + replace = isReplace[0]; + } + if (replace) { + List params = fileParamsMap.get(key); + if (params != null) params.clear(); + } + + for (FileWrapper fileWrapper : fileWrappers) { + put(key, fileWrapper, false); } } public void removeUrl(String key) { - urlParamsMap.remove(key); + stringParamsMap.remove(key); } public void removeFile(String key) { @@ -218,7 +236,7 @@ public void remove(String key) { } public void clear() { - urlParamsMap.clear(); + stringParamsMap.clear(); fileParamsMap.clear(); } @@ -262,7 +280,7 @@ public String toString() { @Override public String toString() { StringBuilder result = new StringBuilder(); - for (ConcurrentHashMap.Entry> entry : urlParamsMap.entrySet()) { + for (ConcurrentHashMap.Entry> entry : stringParamsMap.entrySet()) { if (result.length() > 0) result.append("&"); result.append(entry.getKey()).append("=").append(entry.getValue()); } diff --git a/okgo/src/main/java/com/lzy/okgo/request/base/BodyRequest.java b/okgo/src/main/java/com/lzy/okgo/request/base/BodyRequest.java index 844174fe..816a1d06 100644 --- a/okgo/src/main/java/com/lzy/okgo/request/base/BodyRequest.java +++ b/okgo/src/main/java/com/lzy/okgo/request/base/BodyRequest.java @@ -30,6 +30,7 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.List; +import java.util.Map; import okhttp3.MediaType; import okhttp3.RequestBody; @@ -75,43 +76,42 @@ public R isSpliceUrl(boolean isSpliceUrl) { @SuppressWarnings("unchecked") @Override - public R params(String key, File file) { - params.put(key, file); + public R params(String key, File file, boolean... isReplace) { + params.put(key, file, isReplace); return (R) this; } @SuppressWarnings("unchecked") @Override - public R addFileParams(String key, List files) { - params.putFileParams(key, files); + public R params(String key, File file, String fileName, boolean... isReplace) { + params.put(key, file, fileName, isReplace); return (R) this; } @SuppressWarnings("unchecked") @Override - public R addFileWrapperParams(String key, List fileWrappers) { - params.putFileWrapperParams(key, fileWrappers); + public R params(String key, File file, String fileName, MediaType contentType, boolean... isReplace) { + params.put(key, file, fileName, contentType, isReplace); return (R) this; } @SuppressWarnings("unchecked") - @Override - public R params(String key, File file, String fileName) { - params.put(key, file, fileName); + public R paramsFileMap(Map params, boolean... isReplace) { + this.params.putFileMap(params, isReplace); return (R) this; } @SuppressWarnings("unchecked") @Override - public R params(String key, File file, String fileName, MediaType contentType) { - params.put(key, file, fileName, contentType); + public R paramsFileList(String key, List files, boolean... isReplace) { + params.putFileList(key, files, isReplace); return (R) this; } @SuppressWarnings("unchecked") @Override - public R upRequestBody(RequestBody requestBody) { - this.requestBody = requestBody; + public R paramsFileWrapperList(String key, List fileWrappers, boolean... isReplace) { + params.putFileWrapperList(key, fileWrappers, isReplace); return (R) this; } @@ -199,9 +199,16 @@ public R upFile(File file, MediaType mediaType) { return (R) this; } + @SuppressWarnings("unchecked") + @Override + public R upRequestBody(RequestBody requestBody) { + this.requestBody = requestBody; + return (R) this; + } + @Override public RequestBody generateRequestBody() { - if (isSpliceUrl) url = HttpUtils.createUrlFromParams(baseUrl, params.urlParamsMap); + if (isSpliceUrl) url = HttpUtils.createUrlFromParams(baseUrl, params.stringParamsMap); if (requestBody != null) return requestBody; //自定义的请求体 if (content != null && mediaType != null) return RequestBody.create(mediaType, content); //上传字符串数据 diff --git a/okgo/src/main/java/com/lzy/okgo/request/base/HasBody.java b/okgo/src/main/java/com/lzy/okgo/request/base/HasBody.java index c2449495..f2c1a2e2 100644 --- a/okgo/src/main/java/com/lzy/okgo/request/base/HasBody.java +++ b/okgo/src/main/java/com/lzy/okgo/request/base/HasBody.java @@ -22,6 +22,7 @@ import java.io.File; import java.util.List; +import java.util.Map; import okhttp3.MediaType; import okhttp3.RequestBody; @@ -41,17 +42,17 @@ public interface HasBody { R isSpliceUrl(boolean isSpliceUrl); - R upRequestBody(RequestBody requestBody); + R params(String key, File file, boolean... isReplace); - R params(String key, File file); + R params(String key, File file, String fileName, boolean... isReplace); - R addFileParams(String key, List files); + R params(String key, File file, String fileName, MediaType contentType, boolean... isReplace); - R addFileWrapperParams(String key, List fileWrappers); + R paramsFileMap(Map params, boolean... isReplace); - R params(String key, File file, String fileName); + R paramsFileList(String key, List files, boolean... isReplace); - R params(String key, File file, String fileName, MediaType contentType); + R paramsFileWrapperList(String key, List fileWrappers, boolean... isReplace); R upString(String string); @@ -70,4 +71,6 @@ public interface HasBody { R upFile(File file); R upFile(File file, MediaType mediaType); + + R upRequestBody(RequestBody requestBody); } diff --git a/okgo/src/main/java/com/lzy/okgo/request/base/NoBodyRequest.java b/okgo/src/main/java/com/lzy/okgo/request/base/NoBodyRequest.java index 10707ad7..13d1b8b3 100644 --- a/okgo/src/main/java/com/lzy/okgo/request/base/NoBodyRequest.java +++ b/okgo/src/main/java/com/lzy/okgo/request/base/NoBodyRequest.java @@ -26,7 +26,7 @@ public RequestBody generateRequestBody() { } protected okhttp3.Request.Builder generateRequestBuilder(RequestBody requestBody) { - url = HttpUtils.createUrlFromParams(baseUrl, params.urlParamsMap); + url = HttpUtils.createUrlFromParams(baseUrl, params.stringParamsMap); okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder(); return HttpUtils.appendHeaders(requestBuilder, headers); } diff --git a/okgo/src/main/java/com/lzy/okgo/request/base/Request.java b/okgo/src/main/java/com/lzy/okgo/request/base/Request.java index f2d09e3e..b2ed8d23 100644 --- a/okgo/src/main/java/com/lzy/okgo/request/base/Request.java +++ b/okgo/src/main/java/com/lzy/okgo/request/base/Request.java @@ -129,6 +129,8 @@ public R converter(Converter converter) { @SuppressWarnings("unchecked") public R cacheMode(CacheMode cacheMode) { + HttpUtils.checkNotNull(cacheMode, "cacheMode == null"); + this.cacheMode = cacheMode; return (R) this; } @@ -169,30 +171,12 @@ public R headers(String key, String value) { return (R) this; } - @SuppressWarnings("unchecked") - public R removeHeader(String key) { - headers.remove(key); - return (R) this; - } - - @SuppressWarnings("unchecked") - public R removeAllHeaders() { - headers.clear(); - return (R) this; - } - @SuppressWarnings("unchecked") public R params(HttpParams params) { this.params.put(params); return (R) this; } - @SuppressWarnings("unchecked") - public R params(Map params, boolean... isReplace) { - this.params.put(params, isReplace); - return (R) this; - } - @SuppressWarnings("unchecked") public R params(String key, String value, boolean... isReplace) { params.put(key, value, isReplace); @@ -236,8 +220,26 @@ public R params(String key, boolean value, boolean... isReplace) { } @SuppressWarnings("unchecked") - public R addUrlParams(String key, List values) { - params.putUrlParams(key, values); + public R paramsStringMap(Map params, boolean... isReplace) { + this.params.putStringMap(params, isReplace); + return (R) this; + } + + @SuppressWarnings("unchecked") + public R paramsStringList(String key, List values, boolean... isReplace) { + params.putStringList(key, values, isReplace); + return (R) this; + } + + @SuppressWarnings("unchecked") + public R removeHeader(String key) { + headers.remove(key); + return (R) this; + } + + @SuppressWarnings("unchecked") + public R removeAllHeaders() { + headers.clear(); return (R) this; } @@ -261,7 +263,7 @@ public R uploadInterceptor(ProgressRequestBody.UploadInterceptor uploadIntercept /** 默认返回第一个参数 */ public String getUrlParam(String key) { - List values = params.urlParamsMap.get(key); + List values = params.stringParamsMap.get(key); if (values != null && values.size() > 0) return values.get(0); return null; } diff --git a/okgo/src/main/java/com/lzy/okgo/utils/HttpUtils.java b/okgo/src/main/java/com/lzy/okgo/utils/HttpUtils.java index 2fb575d9..d871b8db 100644 --- a/okgo/src/main/java/com/lzy/okgo/utils/HttpUtils.java +++ b/okgo/src/main/java/com/lzy/okgo/utils/HttpUtils.java @@ -93,8 +93,8 @@ public static RequestBody generateMultipartRequestBody(HttpParams params, boolea if (params.fileParamsMap.isEmpty() && !isMultipart) { //表单提交,没有文件 FormBody.Builder bodyBuilder = new FormBody.Builder(); - for (String key : params.urlParamsMap.keySet()) { - List urlValues = params.urlParamsMap.get(key); + for (String key : params.stringParamsMap.keySet()) { + List urlValues = params.stringParamsMap.get(key); for (String value : urlValues) { bodyBuilder.add(key, value); } @@ -104,8 +104,8 @@ public static RequestBody generateMultipartRequestBody(HttpParams params, boolea //表单提交,有文件 MultipartBody.Builder multipartBodybuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); //拼接键值对 - if (!params.urlParamsMap.isEmpty()) { - for (Map.Entry> entry : params.urlParamsMap.entrySet()) { + if (!params.stringParamsMap.isEmpty()) { + for (Map.Entry> entry : params.stringParamsMap.entrySet()) { List urlValues = entry.getValue(); for (String value : urlValues) { multipartBodybuilder.addFormDataPart(entry.getKey(), value); From b069c302272b9a19246e9715952fc40a288ee833 Mon Sep 17 00:00:00 2001 From: liaoziyao Date: Mon, 17 Jul 2017 11:23:47 +0800 Subject: [PATCH 2/3] separate params to paramsQuery paramsPath and params --- .../java/com/lzy/demo/okgo/TestActivity.java | 31 +++---- .../okgo/cache/policy/BaseCachePolicy.java | 2 +- .../java/com/lzy/okgo/model/HttpParams.java | 80 ++++++++++++++++++- .../lzy/okgo/request/base/BodyRequest.java | 65 ++++++++++++++- .../com/lzy/okgo/request/base/HasBody.java | 18 +++++ .../lzy/okgo/request/base/NoBodyRequest.java | 2 +- .../com/lzy/okgo/request/base/Request.java | 6 ++ .../java/com/lzy/okgo/utils/HttpUtils.java | 39 +++++---- 8 files changed, 201 insertions(+), 42 deletions(-) diff --git a/demo/src/main/java/com/lzy/demo/okgo/TestActivity.java b/demo/src/main/java/com/lzy/demo/okgo/TestActivity.java index 22393b58..7c877331 100644 --- a/demo/src/main/java/com/lzy/demo/okgo/TestActivity.java +++ b/demo/src/main/java/com/lzy/demo/okgo/TestActivity.java @@ -27,21 +27,12 @@ import com.lzy.okgo.OkGo; import com.lzy.okgo.adapter.Call; import com.lzy.okgo.callback.StringCallback; -import com.lzy.okgo.convert.StringConvert; -import com.lzy.okgo.model.HttpHeaders; import com.lzy.okgo.model.Response; -import com.lzy.okgo.request.PostRequest; -import com.lzy.okrx.adapter.ObservableBody; import org.json.JSONObject; -import java.io.File; -import java.util.HashMap; -import java.util.Map; - import butterknife.Bind; import butterknife.OnClick; -import rx.Observable; /** * ================================================ @@ -107,24 +98,22 @@ public void run() { @OnClick(R.id.btn3) public void btn3(View view) { - OkGo.get("asdfasf")// + + OkGo.post("http://server.jeasonlzy.com/{server}/{path}")// .tag(this)// - .headers(HttpHeaders.HEAD_KEY_USER_AGENT, "abcd")// + .paramsPath("server", "OkHttpUtils")// + .paramsPath("path", "method")// + .paramsQuery("aaa", "111")// + .paramsQuery("bbb", "222")// + .paramsQuery("ccc", "333")// + .params("xxx", "777")// + .params("yyy", "888")// + .params("zzz", "999")// .execute(new StringCallback() { @Override public void onSuccess(Response response) { } }); - - Map urlMaps = new HashMap<>(); - Map fileMaps = new HashMap<>(); - - Observable observable = OkGo.post("url") - .tag(this) - .paramsStringMap(urlMaps) - .paramsFileMap(fileMaps) - .converter(new StringConvert()) - .adapt(new ObservableBody()); } } diff --git a/okgo/src/main/java/com/lzy/okgo/cache/policy/BaseCachePolicy.java b/okgo/src/main/java/com/lzy/okgo/cache/policy/BaseCachePolicy.java index 268ebc17..f95eef4c 100644 --- a/okgo/src/main/java/com/lzy/okgo/cache/policy/BaseCachePolicy.java +++ b/okgo/src/main/java/com/lzy/okgo/cache/policy/BaseCachePolicy.java @@ -66,7 +66,7 @@ public boolean onAnalysisResponse(Call call, okhttp3.Response response) { public CacheEntity prepareCache() { //check the config of cache if (request.getCacheKey() == null) { - request.cacheKey(HttpUtils.createUrlFromParams(request.getBaseUrl(), request.getParams().stringParamsMap)); + request.cacheKey(HttpUtils.createUrlFromParams(request.getBaseUrl(), true, request.getParams())); } if (request.getCacheMode() == null) { request.cacheMode(CacheMode.NO_CACHE); diff --git a/okgo/src/main/java/com/lzy/okgo/model/HttpParams.java b/okgo/src/main/java/com/lzy/okgo/model/HttpParams.java index da33ad97..43eb9656 100644 --- a/okgo/src/main/java/com/lzy/okgo/model/HttpParams.java +++ b/okgo/src/main/java/com/lzy/okgo/model/HttpParams.java @@ -48,8 +48,10 @@ public class HttpParams implements Serializable { public static final boolean IS_REPLACE = true; - public LinkedHashMap> stringParamsMap; //普通的键值对参数 - public LinkedHashMap> fileParamsMap; //文件的键值对参数 + public LinkedHashMap pathParamsMap; //url路径参数 + public LinkedHashMap> stringParamsMap; //普通的键值对参数 + public LinkedHashMap> queryParamsMap; //普通的键值对参数 + public LinkedHashMap> fileParamsMap; //文件的键值对参数 public HttpParams() { init(); @@ -66,7 +68,9 @@ public HttpParams(String key, File file) { } private void init() { + pathParamsMap = new LinkedHashMap<>(); stringParamsMap = new LinkedHashMap<>(); + queryParamsMap = new LinkedHashMap<>(); fileParamsMap = new LinkedHashMap<>(); } @@ -222,6 +226,78 @@ public void putFileWrapperList(String key, List fileWrappers, boole } } + public void putPath(String key, String value) { + if (key == null || value == null) return; + + pathParamsMap.put(key, value); + } + + public void putQuery(String key, int value, boolean... isReplace) { + putQuery(key, String.valueOf(value), isReplace); + } + + public void putQuery(String key, long value, boolean... isReplace) { + putQuery(key, String.valueOf(value), isReplace); + } + + public void putQuery(String key, float value, boolean... isReplace) { + putQuery(key, String.valueOf(value), isReplace); + } + + public void putQuery(String key, double value, boolean... isReplace) { + putQuery(key, String.valueOf(value), isReplace); + } + + public void putQuery(String key, char value, boolean... isReplace) { + putQuery(key, String.valueOf(value), isReplace); + } + + public void putQuery(String key, boolean value, boolean... isReplace) { + putQuery(key, String.valueOf(value), isReplace); + } + + public void putQuery(String key, String value, boolean... isReplace) { + if (key == null || value == null) return; + + boolean replace = IS_REPLACE; + if (isReplace != null && isReplace.length > 0) { + replace = isReplace[0]; + } + + List stringParams = queryParamsMap.get(key); + if (stringParams == null) { + stringParams = new ArrayList<>(); + queryParamsMap.put(key, stringParams); + } + if (replace) stringParams.clear(); + stringParams.add(value); + } + + public void putQueryStringMap(Map params, boolean... isReplace) { + if (params == null || params.isEmpty()) return; + + for (Map.Entry entry : params.entrySet()) { + putQuery(entry.getKey(), entry.getValue(), isReplace); + } + } + + public void putQueryStringList(String key, List values, boolean... isReplace) { + if (key == null || values == null || values.isEmpty()) return; + + boolean replace = IS_REPLACE; + if (isReplace != null && isReplace.length > 0) { + replace = isReplace[0]; + } + if (replace) { + List params = stringParamsMap.get(key); + if (params != null) params.clear(); + } + + for (String value : values) { + putQuery(key, value, false); + } + } + public void removeUrl(String key) { stringParamsMap.remove(key); } diff --git a/okgo/src/main/java/com/lzy/okgo/request/base/BodyRequest.java b/okgo/src/main/java/com/lzy/okgo/request/base/BodyRequest.java index 816a1d06..99072562 100644 --- a/okgo/src/main/java/com/lzy/okgo/request/base/BodyRequest.java +++ b/okgo/src/main/java/com/lzy/okgo/request/base/BodyRequest.java @@ -115,6 +115,69 @@ public R paramsFileWrapperList(String key, List fileWrap return (R) this; } + @SuppressWarnings("unchecked") + @Override + public R paramsQuery(String key, String value, boolean... isReplace) { + params.putQuery(key, value, isReplace); + return (R) this; + } + + @SuppressWarnings("unchecked") + @Override + public R paramsQuery(String key, int value, boolean... isReplace) { + params.putQuery(key, value, isReplace); + return (R) this; + } + + @SuppressWarnings("unchecked") + @Override + public R paramsQuery(String key, float value, boolean... isReplace) { + params.putQuery(key, value, isReplace); + return (R) this; + } + + @SuppressWarnings("unchecked") + @Override + public R paramsQuery(String key, double value, boolean... isReplace) { + params.putQuery(key, value, isReplace); + return (R) this; + } + + @SuppressWarnings("unchecked") + @Override + public R paramsQuery(String key, long value, boolean... isReplace) { + params.putQuery(key, value, isReplace); + return (R) this; + } + + @SuppressWarnings("unchecked") + @Override + public R paramsQuery(String key, char value, boolean... isReplace) { + params.putQuery(key, value, isReplace); + return (R) this; + } + + @SuppressWarnings("unchecked") + @Override + public R paramsQuery(String key, boolean value, boolean... isReplace) { + params.putQuery(key, value, isReplace); + return (R) this; + } + + @SuppressWarnings("unchecked") + @Override + public R paramsQueryStringMap(Map params, boolean... isReplace) { + this.params.putQueryStringMap(params, isReplace); + return (R) this; + } + + @SuppressWarnings("unchecked") + @Override + public R paramsQueryStringList(String key, List values, boolean... isReplace) { + params.putQueryStringList(key, values, isReplace); + return (R) this; + } + /** 注意使用该方法上传字符串会清空实体中其他所有的参数,头信息不清除 */ @SuppressWarnings("unchecked") @Override @@ -208,7 +271,7 @@ public R upRequestBody(RequestBody requestBody) { @Override public RequestBody generateRequestBody() { - if (isSpliceUrl) url = HttpUtils.createUrlFromParams(baseUrl, params.stringParamsMap); + url = HttpUtils.createUrlFromParams(baseUrl, isSpliceUrl, params); if (requestBody != null) return requestBody; //自定义的请求体 if (content != null && mediaType != null) return RequestBody.create(mediaType, content); //上传字符串数据 diff --git a/okgo/src/main/java/com/lzy/okgo/request/base/HasBody.java b/okgo/src/main/java/com/lzy/okgo/request/base/HasBody.java index f2c1a2e2..ed9dab78 100644 --- a/okgo/src/main/java/com/lzy/okgo/request/base/HasBody.java +++ b/okgo/src/main/java/com/lzy/okgo/request/base/HasBody.java @@ -54,6 +54,24 @@ public interface HasBody { R paramsFileWrapperList(String key, List fileWrappers, boolean... isReplace); + R paramsQuery(String key, String value, boolean... isReplace); + + R paramsQuery(String key, int value, boolean... isReplace); + + R paramsQuery(String key, float value, boolean... isReplace); + + R paramsQuery(String key, double value, boolean... isReplace); + + R paramsQuery(String key, long value, boolean... isReplace); + + R paramsQuery(String key, char value, boolean... isReplace); + + R paramsQuery(String key, boolean value, boolean... isReplace); + + R paramsQueryStringMap(Map params, boolean... isReplace); + + R paramsQueryStringList(String key, List values, boolean... isReplace); + R upString(String string); R upString(String string, MediaType mediaType); diff --git a/okgo/src/main/java/com/lzy/okgo/request/base/NoBodyRequest.java b/okgo/src/main/java/com/lzy/okgo/request/base/NoBodyRequest.java index 13d1b8b3..c5be5f6e 100644 --- a/okgo/src/main/java/com/lzy/okgo/request/base/NoBodyRequest.java +++ b/okgo/src/main/java/com/lzy/okgo/request/base/NoBodyRequest.java @@ -26,7 +26,7 @@ public RequestBody generateRequestBody() { } protected okhttp3.Request.Builder generateRequestBuilder(RequestBody requestBody) { - url = HttpUtils.createUrlFromParams(baseUrl, params.stringParamsMap); + url = HttpUtils.createUrlFromParams(baseUrl, true, params); okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder(); return HttpUtils.appendHeaders(requestBuilder, headers); } diff --git a/okgo/src/main/java/com/lzy/okgo/request/base/Request.java b/okgo/src/main/java/com/lzy/okgo/request/base/Request.java index b2ed8d23..a38523a4 100644 --- a/okgo/src/main/java/com/lzy/okgo/request/base/Request.java +++ b/okgo/src/main/java/com/lzy/okgo/request/base/Request.java @@ -231,6 +231,12 @@ public R paramsStringList(String key, List values, boolean... isReplace) return (R) this; } + @SuppressWarnings("unchecked") + public R paramsPath(String key, String value) { + params.putPath(key, value); + return (R) this; + } + @SuppressWarnings("unchecked") public R removeHeader(String key) { headers.remove(key); diff --git a/okgo/src/main/java/com/lzy/okgo/utils/HttpUtils.java b/okgo/src/main/java/com/lzy/okgo/utils/HttpUtils.java index d871b8db..584befc0 100644 --- a/okgo/src/main/java/com/lzy/okgo/utils/HttpUtils.java +++ b/okgo/src/main/java/com/lzy/okgo/utils/HttpUtils.java @@ -26,12 +26,13 @@ import java.net.FileNameMap; import java.net.URLConnection; import java.net.URLDecoder; -import java.net.URLEncoder; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import okhttp3.FormBody; import okhttp3.Headers; +import okhttp3.HttpUrl; import okhttp3.MediaType; import okhttp3.MultipartBody; import okhttp3.Request; @@ -49,26 +50,32 @@ */ public class HttpUtils { /** 将传递进来的参数拼接成 url */ - public static String createUrlFromParams(String url, Map> params) { + public static String createUrlFromParams(String url, boolean isSpliceUrl, HttpParams httpParams) { try { - StringBuilder sb = new StringBuilder(); - sb.append(url); - if (url.indexOf('&') > 0 || url.indexOf('?') > 0) sb.append("&"); - else sb.append("?"); - for (Map.Entry> urlParams : params.entrySet()) { - List urlValues = urlParams.getValue(); - for (String value : urlValues) { - //对参数进行 utf-8 编码,防止头信息传中文 - String urlValue = URLEncoder.encode(value, "UTF-8"); - sb.append(urlParams.getKey()).append("=").append(urlValue).append("&"); + String tempUrl = url; + for (Map.Entry pathParams : httpParams.pathParamsMap.entrySet()) { + tempUrl = tempUrl.replace("{" + pathParams.getKey() + "}", String.valueOf(pathParams.getValue())); + } + HttpUrl httpUrl = HttpUrl.parse(tempUrl); + if (httpUrl == null) return tempUrl; + + HttpUrl.Builder urlBuilder = httpUrl.newBuilder(); + Map> params = new LinkedHashMap<>(); + if (isSpliceUrl) { + params.putAll(httpParams.stringParamsMap); + } + params.putAll(httpParams.queryParamsMap); + for (Map.Entry> queryParams : params.entrySet()) { + List queryValues = queryParams.getValue(); + for (String value : queryValues) { + urlBuilder.addQueryParameter(queryParams.getKey(), value); } } - sb.deleteCharAt(sb.length() - 1); - return sb.toString(); - } catch (UnsupportedEncodingException e) { + return urlBuilder.build().toString(); + } catch (Exception e) { OkLogger.printStackTrace(e); + return url; } - return url; } /** 通用的拼接请求头 */ From 52fdec66d0cf8311b596cfb6767e4d2318876816 Mon Sep 17 00:00:00 2001 From: liaoziyao Date: Wed, 26 Jul 2017 11:19:51 +0800 Subject: [PATCH 3/3] update ProgressRequestBody --- .../java/com/lzy/okgo/request/base/ProgressRequestBody.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/okgo/src/main/java/com/lzy/okgo/request/base/ProgressRequestBody.java b/okgo/src/main/java/com/lzy/okgo/request/base/ProgressRequestBody.java index 282ffcd8..f9e4c6e5 100644 --- a/okgo/src/main/java/com/lzy/okgo/request/base/ProgressRequestBody.java +++ b/okgo/src/main/java/com/lzy/okgo/request/base/ProgressRequestBody.java @@ -50,6 +50,11 @@ public class ProgressRequestBody extends RequestBody { this.callback = callback; } + /** 获取原始的RequestBody */ + public RequestBody getRawRequestBody() { + return requestBody; + } + /** 重写调用实际的响应体的contentType */ @Override public MediaType contentType() {