diff --git a/.gitignore b/.gitignore index ac7edcc2..1396e76d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,9 @@ *.iml .gradle -bintray.gradle /.idea /screenshots /local.properties .DS_Store /build /captures +libs/ diff --git a/README.md b/README.md index f7227e3b..4b7f776e 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ * 对于Android Studio的用户,可以选择添加: ```java - compile 'com.lzy.net:okhttputils:1.5.2' //可以单独使用,不需要依赖下方的扩展包 + compile 'com.lzy.net:okhttputils:1.6.0' //可以单独使用,不需要依赖下方的扩展包 compile 'com.lzy.net:okhttpserver:0.1.7' //扩展了下载管理和上传管理,根据需要添加 compile 'com.lzy.net:okhttputils:+' //版本号使用 + 可以自动引用最新版 @@ -46,7 +46,7 @@ ``` * 对于Eclipse的用户,可以选择添加 `/lib` 目录下的: ```java - okhttputils-1.5.2.jar + okhttputils-1.6.0.jar okhttpserver-0.1.7.jar ``` @@ -63,6 +63,7 @@ * 一般的 get,post,put,delete,head,options请求 * 基于Post的大文本数据上传 * 多文件和多参数统一的表单上传 +* 支持一个key上传一个文件,也可以一个Key上传多个文件 * 大文件下载和下载进度回调 * 大文件上传和上传进度回调 * 支持cookie的内存存储和持久化存储,支持传递自定义cookie @@ -143,7 +144,7 @@ OkHttpUtils.get(Urls.URL_DOWNLOAD)// }); ``` ### 4.普通Post,直接上传String类型的文本 -不建议这么用,该方法上传字符串会清空实体中其他所有的参数,但头信息不清除 +一般此种用法用于与服务器约定的数据格式,当使用该方法时,params中的参数设置是无效的,所有参数均需要通过需要上传的文本中指定,此外,额外指定的header参数仍然保持有效。 ```java OkHttpUtils.post(Urls.URL_TEXT_UPLOAD)// .tag(this)// @@ -157,7 +158,7 @@ OkHttpUtils.post(Urls.URL_TEXT_UPLOAD)// ``` ### 5.普通Post,直接上传Json类型的文本 -不建议这么用,该方法上传字符串会清空实体中其他所有的参数,但头信息不清除 +该方法与postString没有本质区别,只是数据格式是json,一般来说,需要自己创建一个实体bean或者一个map,把需要的参数设置进去,然后通过三方的Gson或者fastjson转换成json字符串,最后直接使用该方法提交到服务器。 ```java OkHttpUtils.post(Urls.URL_TEXT_UPLOAD)// .tag(this)// @@ -196,6 +197,9 @@ OkHttpUtils.get(Urls.URL_METHOD) // 请求方式和请求url, get请求不需要 .params("param2", "paramValue2") // 支持多请求参数同时添加 .params("file1", new File("filepath1")) // 可以添加文件上传 .params("file2", new File("filepath2")) // 支持多文件同时添加上传 + .addUrlParams("key", List values) //这里支持一个key传多个参数 + .addFileParams("key", List files) //这里支持一个key传多个文件 + .addFileWrapperParams("key", List fileWrappers) //这里支持一个key传多个文件 .addCookie("aaa", "bbb") // 这里可以传递自己想传的Cookie .addCookie(cookie) // 可以自己构建cookie .addCookies(cookies) // 可以一次传递批量的cookie diff --git a/app/build.gradle b/app/build.gradle index f7d642d9..647c51de 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "com.lzy.okhttpdemo" minSdkVersion 14 targetSdkVersion 22 - versionCode 17 - versionName "1.5.2" + versionCode 18 + versionName "1.6.0" } buildTypes { release { @@ -40,7 +40,7 @@ dependencies { compile 'com.jakewharton:butterknife:7.0.1' compile 'com.google.code.gson:gson:2.5' - compile 'com.lzy.widget:imagepicker:0.2.1' + compile 'com.lzy.widget:imagepicker:0.2.6' compile 'com.lzy.widget:view-core:0.2.1' // compile 'com.lzy.net:okhttputils:+' // compile 'com.lzy.net:okhttpserver:+' diff --git a/app/src/main/java/com/lzy/okhttpdemo/GApp.java b/app/src/main/java/com/lzy/okhttpdemo/GApp.java index a12bf347..89699d56 100644 --- a/app/src/main/java/com/lzy/okhttpdemo/GApp.java +++ b/app/src/main/java/com/lzy/okhttpdemo/GApp.java @@ -3,7 +3,6 @@ import android.app.Application; import com.lzy.okhttputils.OkHttpUtils; -import com.lzy.okhttputils.cookie.store.MemoryCookieStore; import com.lzy.okhttputils.cookie.store.PersistentCookieStore; import com.lzy.okhttputils.model.HttpHeaders; import com.lzy.okhttputils.model.HttpParams; diff --git a/app/src/main/java/com/lzy/okhttpdemo/activity/FormUploadActivity.java b/app/src/main/java/com/lzy/okhttpdemo/activity/FormUploadActivity.java index eda647ff..a9d6040d 100644 --- a/app/src/main/java/com/lzy/okhttpdemo/activity/FormUploadActivity.java +++ b/app/src/main/java/com/lzy/okhttpdemo/activity/FormUploadActivity.java @@ -22,7 +22,6 @@ import com.lzy.okhttpdemo.utils.Urls; import com.lzy.okhttputils.OkHttpUtils; import com.lzy.okhttputils.request.BaseRequest; -import com.lzy.okhttputils.request.PostRequest; import java.io.File; import java.util.ArrayList; @@ -80,8 +79,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { if (imageItems != null && imageItems.size() > 0) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < imageItems.size(); i++) { - if (i == imageItems.size() - 1) - sb.append("图片").append(i + 1).append(" : ").append(imageItems.get(i).path); + if (i == imageItems.size() - 1) sb.append("图片").append(i + 1).append(" : ").append(imageItems.get(i).path); else sb.append("图片").append(i + 1).append(" : ").append(imageItems.get(i).path).append("\n"); } tvImages.setText(sb.toString()); @@ -97,21 +95,24 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { @OnClick(R.id.formUpload) public void formUpload(View view) { + ArrayList 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)); + } + } //拼接参数 - PostRequest request = OkHttpUtils.post(Urls.URL_FORM_UPLOAD)// + OkHttpUtils.post(Urls.URL_FORM_UPLOAD)// .tag(this)// .headers("header1", "headerValue1")// .headers("header2", "headerValue2")// .params("param1", "paramValue1")// - .params("param2", "paramValue2");// - //拼接选中的文件参数(如果文件个数已知,可以链式调用到底,就不用这么断开了) - if (imageItems != null && imageItems.size() > 0) { - for (int i = 0; i < imageItems.size(); i++) { - request.params("file" + (i + 1), new File(imageItems.get(i).path)); - } - } - //执行请求 - request.execute(new ProgressUpCallBack<>(this, RequestInfo.class)); + .params("param2", "paramValue2")// +// .params("file1",new File("文件路径")) //这种方式为一个key,对应一个文件 +// .params("file2",new File("文件路径")) +// .params("file3",new File("文件路径")) + .addFileParams("file", files) // 这种方式为同一个key,上传多个文件 + .execute(new ProgressUpCallBack<>(this, RequestInfo.class)); } private class ProgressUpCallBack extends JsonCallback { diff --git a/app/src/main/java/com/lzy/okhttpdemo/activity/TestActivity.java b/app/src/main/java/com/lzy/okhttpdemo/activity/TestActivity.java index 732bce3d..c522f691 100644 --- a/app/src/main/java/com/lzy/okhttpdemo/activity/TestActivity.java +++ b/app/src/main/java/com/lzy/okhttpdemo/activity/TestActivity.java @@ -7,9 +7,13 @@ import com.lzy.okhttpdemo.R; import com.lzy.okhttpdemo.callback.StringDialogCallback; +import com.lzy.okhttpdemo.utils.Urls; import com.lzy.okhttputils.OkHttpUtils; import com.lzy.okhttputils.callback.StringCallback; +import java.io.File; +import java.util.ArrayList; + import butterknife.ButterKnife; import butterknife.OnClick; import okhttp3.Request; @@ -26,8 +30,15 @@ protected void onCreate(Bundle savedInstanceState) { @OnClick(R.id.btn1) public void btn1(View view) { - OkHttpUtils.get("http://www.qunar.com")// + ArrayList params = new ArrayList<>(); + params.add("111"); + params.add("222"); + params.add("333"); + params.add("444"); + OkHttpUtils.get(Urls.URL_METHOD)// .tag(this)// + .addUrlParams("aaa",params) + .params("ddd","444") .execute(new StringCallback() { @Override public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) { @@ -37,11 +48,17 @@ public void onResponse(boolean isFromCache, String s, Request request, @Nullable @OnClick(R.id.btn2) public void btn2(View view) { - OkHttpUtils.post("http://dev.11yuehui.com/WebApi/Login/login.html")// + ArrayList params = new ArrayList<>(); + params.add("111"); + params.add("222"); + params.add("333"); + params.add("444"); + OkHttpUtils.post(Urls.URL_METHOD)// .tag(this)// - .params("username", "273029") //用户名 - .params("password", "273029tc") //密码 - .execute(new StringDialogCallback(this) { + .addUrlParams("aaa",params) + .params("bbb","666") + .params("ccc","777") + .execute(new StringCallback() { @Override public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) { } @@ -50,6 +67,14 @@ public void onResponse(boolean isFromCache, String s, Request request, @Nullable @OnClick(R.id.btn3) public void btn3(View view) { - + OkHttpUtils.post("http://dev.11yuehui.com/WebApi/Login/login.html")// + .tag(this)// + .params("username", "273029") //用户名 + .params("password", "273029tc") //密码 + .execute(new StringDialogCallback(this) { + @Override + public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) { + } + }); } } diff --git a/app/src/main/java/com/lzy/okhttpdemo/callback/EncryptCallback.java b/app/src/main/java/com/lzy/okhttpdemo/callback/EncryptCallback.java index 042f8d30..345168af 100644 --- a/app/src/main/java/com/lzy/okhttpdemo/callback/EncryptCallback.java +++ b/app/src/main/java/com/lzy/okhttpdemo/callback/EncryptCallback.java @@ -5,6 +5,7 @@ import com.lzy.okhttputils.request.BaseRequest; import java.util.Comparator; +import java.util.List; import java.util.Map; import java.util.Random; import java.util.TreeMap; @@ -39,7 +40,7 @@ private void sign(HttpParams params) { params.put("nonce", getRndStr(6 + RANDOM.nextInt(8))); params.put("timestamp", "" + (System.currentTimeMillis() / 1000L)); StringBuilder sb = new StringBuilder(); - for (Map.Entry entry : getSortedMapByKey(params.urlParamsMap).entrySet()) { + for (Map.Entry> entry : getSortedMapByKey(params.urlParamsMap).entrySet()) { sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&"); } sb.delete(sb.length() - 1, sb.length()); @@ -59,15 +60,15 @@ private String getRndStr(int length) { } /** 按照key的自然顺序进行排序,并返回 */ - private Map getSortedMapByKey(ConcurrentHashMap map) { + private Map> getSortedMapByKey(ConcurrentHashMap> map) { Comparator comparator = new Comparator() { @Override public int compare(String lhs, String rhs) { return lhs.compareTo(rhs); } }; - TreeMap treeMap = new TreeMap<>(comparator); - for (Map.Entry entry : map.entrySet()) { + TreeMap> treeMap = new TreeMap<>(comparator); + for (Map.Entry> entry : map.entrySet()) { treeMap.put(entry.getKey(), entry.getValue()); } return treeMap; diff --git a/app/src/main/java/com/lzy/okhttpdemo/utils/Urls.java b/app/src/main/java/com/lzy/okhttpdemo/utils/Urls.java index b6d45a78..20db7520 100644 --- a/app/src/main/java/com/lzy/okhttpdemo/utils/Urls.java +++ b/app/src/main/java/com/lzy/okhttpdemo/utils/Urls.java @@ -11,7 +11,7 @@ */ public class Urls { public static final String SERVER = "http://server.jeasonlzy.com/OkHttpUtils/"; - // public static final String SERVER = "http://192.168.1.102:8080/OkHttpUtils/"; + // public static final String SERVER = "http://192.168.1.121:8080/OkHttpUtils/"; public static final String URL_METHOD = SERVER + "method"; public static final String URL_CACHE = SERVER + "cache"; public static final String URL_IMAGE = SERVER + "image"; diff --git a/build.gradle b/build.gradle index 9b53ff23..cd8e87f8 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.1.0' + classpath 'com.android.tools.build:gradle:2.1.2' classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' diff --git a/lib/okhttputils-1.5.2.jar b/lib/okhttputils-1.6.0.jar similarity index 61% rename from lib/okhttputils-1.5.2.jar rename to lib/okhttputils-1.6.0.jar index 38d26170..2f113698 100644 Binary files a/lib/okhttputils-1.5.2.jar and b/lib/okhttputils-1.6.0.jar differ diff --git a/okhttpserver/bintray.gradle b/okhttpserver/bintray.gradle new file mode 100644 index 00000000..9f7319d1 --- /dev/null +++ b/okhttpserver/bintray.gradle @@ -0,0 +1,100 @@ +apply plugin: 'com.github.dcendents.android-maven' +apply plugin: 'com.jfrog.bintray' + +version = "0.1.7" // 数据仓库依赖第三部分 + +def siteUrl = 'https://github.com/jeasonlzy0216/OkHttpUtils' +def gitUrl = 'https://github.com/jeasonlzy0216/OkHttpUtils.git' +group = "com.lzy.net" // 数据仓库依赖第一部分 + +install { + repositories.mavenInstaller { + pom { + project { + packaging 'aar' + name 'okhttpserver For Android' // 项目描述 + url siteUrl + licenses { + license { + name 'The Apache Software License, Version 2.0' + url 'http://www.apache.org/licenses/LICENSE-2.0.txt' + } + } + developers { + developer { + id 'jeasonlzy' // 开发者信息 + name 'LiaoZiYao' // 开发者信息 + email 'liaojeason@126.com' // 开发者信息 + } + } + scm { + connection gitUrl + developerConnection gitUrl + url siteUrl + } + } + } + } +} + +task sourcesJar(type: Jar) { + from android.sourceSets.main.java.srcDirs + classifier = 'sources' +} + +task javadoc(type: Javadoc) { + source = android.sourceSets.main.java.srcDirs + classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) +} + +task javadocJar(type: Jar, dependsOn: javadoc) { + classifier = 'javadoc' + from javadoc.destinationDir +} + +javadoc { + options { + encoding "UTF-8" + charSet 'UTF-8' + author true + version true + links "http://docs.oracle.com/javase/7/docs/api" + title 'okhttpserver For Android' // 文档标题 + } +} + +artifacts { +// archives javadocJar + archives sourcesJar +} + +//定义一个函数,target是生成jar包的文件名 +def makeJar(String target) { + exec { + executable "jar" //调用jar + args "cvf", target + args "-C", "build/intermediates/classes/release" + args "", "." + } +} + +//新建一个task,名为buildLib,依赖build(build是一个自带的task) +task buildLib(dependsOn: build) { + makeJar("../lib/okhttpserver-" + version + ".jar") +} + +Properties properties = new Properties() +properties.load(project.rootProject.file('local.properties').newDataInputStream()) +bintray { + user = properties.getProperty("bintray.user") + key = properties.getProperty("bintray.apikey") + configurations = ['archives'] + pkg { + repo = "maven" + name = "okhttpserver" // 数据仓库依赖第二部分 + websiteUrl = siteUrl + vcsUrl = gitUrl + licenses = ["Apache-2.0"] + publish = true + } +} diff --git a/okhttputils/bintray.gradle b/okhttputils/bintray.gradle new file mode 100644 index 00000000..5b751b8e --- /dev/null +++ b/okhttputils/bintray.gradle @@ -0,0 +1,93 @@ +apply plugin: 'com.github.dcendents.android-maven' +apply plugin: 'com.jfrog.bintray' + +version = "1.6.0" // 数据仓库依赖第三部分 + +def siteUrl = 'https://github.com/jeasonlzy0216/OkHttpUtils' +def gitUrl = 'https://github.com/jeasonlzy0216/OkHttpUtils.git' +group = "com.lzy.net" // 数据仓库依赖第一部分 + +install { + repositories.mavenInstaller { + pom { + project { + packaging 'aar' + name 'OkHttpUtils For Android' // 项目描述 + url siteUrl + licenses { + license { + name 'The Apache Software License, Version 2.0' + url 'http://www.apache.org/licenses/LICENSE-2.0.txt' + } + } + developers { + developer { + id 'jeasonlzy' // 开发者信息 + name 'LiaoZiYao' // 开发者信息 + email 'liaojeason@126.com' // 开发者信息 + } + } + scm { + connection gitUrl + developerConnection gitUrl + url siteUrl + } + } + } + } +} + +task sourcesJar(type: Jar) { + from android.sourceSets.main.java.srcDirs + classifier = 'sources' +} + +task javadoc(type: Javadoc) { + source = android.sourceSets.main.java.srcDirs + classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) +} + +task javadocJar(type: Jar, dependsOn: javadoc) { + classifier = 'javadoc' + from javadoc.destinationDir +} + +javadoc { + options { + encoding "UTF-8" + charSet 'UTF-8' + author true + version true + links "http://docs.oracle.com/javase/7/docs/api" + title 'OkHttpUtils For Android' // 文档标题 + } +} + +artifacts { +// archives javadocJar + archives sourcesJar +} + +// 生成jar包 +task releaseJar(type: Copy) { + from( 'build/intermediates/bundles/release') + into( '../lib') + include('classes.jar') + rename('classes.jar', 'okhttputils-' + version + '.jar') +} + +Properties properties = new Properties() +properties.load(project.rootProject.file('local.properties').newDataInputStream()) +bintray { + user = properties.getProperty("bintray.user") + key = properties.getProperty("bintray.apikey") + configurations = ['archives'] + pkg { + repo = "maven" + name = "okhttputils" // 数据仓库依赖第二部分 + websiteUrl = siteUrl + vcsUrl = gitUrl + licenses = ["Apache-2.0"] + publish = true + } +} diff --git a/okhttputils/build.gradle b/okhttputils/build.gradle index e20b1ce8..ae851edf 100644 --- a/okhttputils/build.gradle +++ b/okhttputils/build.gradle @@ -8,7 +8,7 @@ android { minSdkVersion 8 targetSdkVersion 22 versionCode 6 - versionName "1.5.2" + versionName "1.6.0" } buildTypes { release { diff --git a/okhttputils/src/main/java/com/lzy/okhttputils/model/HttpHeaders.java b/okhttputils/src/main/java/com/lzy/okhttputils/model/HttpHeaders.java index 52b15bd5..56cb0c70 100644 --- a/okhttputils/src/main/java/com/lzy/okhttputils/model/HttpHeaders.java +++ b/okhttputils/src/main/java/com/lzy/okhttputils/model/HttpHeaders.java @@ -152,6 +152,10 @@ public static String getCacheControl(String cacheControl, String pragma) { else return null; } + public static void setAcceptLanguage(String language) { + acceptLanguage = language; + } + /** * Accept-Language: zh-CN,zh;q=0.8 */ @@ -169,6 +173,10 @@ public static String getAcceptLanguage() { return acceptLanguage; } + public static void setUserAgent(String agent) { + userAgent = agent; + } + /** * User-Agent: Mozilla/5.0 (Linux; U; Android 5.0.2; zh-cn; Redmi Note 3 Build/LRX22G) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Mobile Safari/537.36 */ @@ -246,8 +254,6 @@ public static String formatMillisToGMT(long milliseconds) { @Override public String toString() { - return "HttpHeaders{" + - "headersMap=" + headersMap + - '}'; + return "HttpHeaders{" + "headersMap=" + headersMap + '}'; } -} +} \ No newline at end of file diff --git a/okhttputils/src/main/java/com/lzy/okhttputils/model/HttpParams.java b/okhttputils/src/main/java/com/lzy/okhttputils/model/HttpParams.java index a14cec20..ffd32506 100644 --- a/okhttputils/src/main/java/com/lzy/okhttputils/model/HttpParams.java +++ b/okhttputils/src/main/java/com/lzy/okhttputils/model/HttpParams.java @@ -4,6 +4,8 @@ import java.io.Serializable; import java.net.FileNameMap; import java.net.URLConnection; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.ConcurrentHashMap; import okhttp3.MediaType; @@ -13,17 +15,17 @@ * 作 者:廖子尧 * 版 本:1.0 * 创建日期:2015/10/9 - * 描 述:请求参数的包装类 + * 描 述:请求参数的包装类,支持一个key对应多个值 * 修订历史: * ================================================ */ public class HttpParams implements Serializable { /** 普通的键值对参数 */ - public ConcurrentHashMap urlParamsMap; + public ConcurrentHashMap> urlParamsMap; /** 文件的键值对参数 */ - public ConcurrentHashMap fileParamsMap; + public ConcurrentHashMap> fileParamsMap; private void init() { urlParamsMap = new ConcurrentHashMap<>(); @@ -47,14 +49,26 @@ public HttpParams(String key, File file) { 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.fileParamsMap != null && !params.fileParamsMap.isEmpty()) fileParamsMap.putAll(params.fileParamsMap); } } public void put(String key, String value) { if (key != null && value != null) { - urlParamsMap.put(key, value); + List urlValues = urlParamsMap.get(key); + if (urlValues == null) { + urlValues = new ArrayList<>(); + urlParamsMap.put(key, urlValues); + } + urlValues.add(value); + } + } + + public void putUrlParams(String key, List values) { + if (key != null && values != null && !values.isEmpty()) { + for (String value : values) { + put(key, value); + } } } @@ -66,9 +80,36 @@ public void put(String key, File file, String fileName) { put(key, file, fileName, guessMimeType(fileName)); } + 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) { if (key != null) { - fileParamsMap.put(key, new FileWrapper(file, fileName, contentType)); + List fileWrappers = fileParamsMap.get(key); + if (fileWrappers == null) { + fileWrappers = new ArrayList<>(); + fileParamsMap.put(key, fileWrappers); + } + fileWrappers.add(new FileWrapper(file, fileName, contentType)); + } + } + + public void putFileParams(String key, List files) { + if (key != null && files != null && !files.isEmpty()) { + for (File file : files) { + put(key, file); + } + } + } + + public void putFileWrapperParams(String key, List fileWrappers) { + if (key != null && fileWrappers != null && !fileWrappers.isEmpty()) { + for (FileWrapper fileWrapper : fileWrappers) { + put(key, fileWrapper); + } } } @@ -117,16 +158,21 @@ public String getFileName() { return "nofilename"; } } + + @Override + public String toString() { + return "FileWrapper{" + "file=" + file + ", fileName='" + fileName + ", contentType=" + contentType + ", fileSize=" + fileSize + '}'; + } } @Override public String toString() { StringBuilder result = new StringBuilder(); - for (ConcurrentHashMap.Entry entry : urlParamsMap.entrySet()) { + for (ConcurrentHashMap.Entry> entry : urlParamsMap.entrySet()) { if (result.length() > 0) result.append("&"); result.append(entry.getKey()).append("=").append(entry.getValue()); } - for (ConcurrentHashMap.Entry entry : fileParamsMap.entrySet()) { + for (ConcurrentHashMap.Entry> entry : fileParamsMap.entrySet()) { if (result.length() > 0) result.append("&"); result.append(entry.getKey()).append("=").append(entry.getValue()); } diff --git a/okhttputils/src/main/java/com/lzy/okhttputils/request/BaseRequest.java b/okhttputils/src/main/java/com/lzy/okhttputils/request/BaseRequest.java index 0f61c77d..b923087b 100644 --- a/okhttputils/src/main/java/com/lzy/okhttputils/request/BaseRequest.java +++ b/okhttputils/src/main/java/com/lzy/okhttputils/request/BaseRequest.java @@ -169,12 +169,30 @@ public R params(String key, String value) { return (R) this; } + @SuppressWarnings("unchecked") + public R addUrlParams(String key, List values) { + params.putUrlParams(key, values); + return (R) this; + } + @SuppressWarnings("unchecked") public R params(String key, File file) { params.put(key, file); return (R) this; } + @SuppressWarnings("unchecked") + public R addFileParams(String key, List files) { + params.putFileParams(key, files); + return (R) this; + } + + @SuppressWarnings("unchecked") + public R addFileWrapperParams(String key, List fileWrappers) { + params.putFileWrapperParams(key, fileWrappers); + return (R) this; + } + @SuppressWarnings("unchecked") public R params(String key, File file, String fileName) { params.put(key, file, fileName); @@ -240,15 +258,18 @@ public HttpHeaders getHeaders() { } /** 将传递进来的参数拼接成 url */ - protected String createUrlFromParams(String url, Map params) { + protected String createUrlFromParams(String url, Map> params) { 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()) { - String urlValue = URLEncoder.encode(urlParams.getValue(), "UTF-8"); - sb.append(urlParams.getKey()).append("=").append(urlValue).append("&"); + for (Map.Entry> urlParams : params.entrySet()) { + List urlValues = urlParams.getValue(); + for (String value : urlValues) { + String urlValue = URLEncoder.encode(value, "UTF-8"); + sb.append(urlParams.getKey()).append("=").append(urlValue).append("&"); + } } sb.deleteCharAt(sb.length() - 1); return sb.toString(); @@ -279,7 +300,10 @@ protected RequestBody generateMultipartRequestBody() { //表单提交,没有文件 FormBody.Builder bodyBuilder = new FormBody.Builder(); for (String key : params.urlParamsMap.keySet()) { - bodyBuilder.add(key, params.urlParamsMap.get(key)); + List urlValues = params.urlParamsMap.get(key); + for (String value : urlValues) { + bodyBuilder.add(key, value); + } } return bodyBuilder.build(); } else { @@ -287,14 +311,20 @@ protected RequestBody generateMultipartRequestBody() { MultipartBody.Builder multipartBodybuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); //拼接键值对 if (!params.urlParamsMap.isEmpty()) { - for (Map.Entry entry : params.urlParamsMap.entrySet()) { - multipartBodybuilder.addFormDataPart(entry.getKey(), entry.getValue()); + for (Map.Entry> entry : params.urlParamsMap.entrySet()) { + List urlValues = entry.getValue(); + for (String value : urlValues) { + multipartBodybuilder.addFormDataPart(entry.getKey(), value); + } } } //拼接文件 - for (Map.Entry entry : params.fileParamsMap.entrySet()) { - RequestBody fileBody = RequestBody.create(entry.getValue().contentType, entry.getValue().file); - multipartBodybuilder.addFormDataPart(entry.getKey(), entry.getValue().fileName, fileBody); + for (Map.Entry> entry : params.fileParamsMap.entrySet()) { + List fileValues = entry.getValue(); + for (HttpParams.FileWrapper fileWrapper : fileValues) { + RequestBody fileBody = RequestBody.create(fileWrapper.contentType, fileWrapper.file); + multipartBodybuilder.addFormDataPart(entry.getKey(), fileWrapper.fileName, fileBody); + } } return multipartBodybuilder.build(); } diff --git a/okhttputils_v1.5.2.apk b/okhttputils_v1.5.2.apk deleted file mode 100644 index 4a116eb9..00000000 Binary files a/okhttputils_v1.5.2.apk and /dev/null differ diff --git a/okhttputils_v1.6.0.apk b/okhttputils_v1.6.0.apk new file mode 100644 index 00000000..8a36bb9b Binary files /dev/null and b/okhttputils_v1.6.0.apk differ