Browse Source

提交2.0.9版本,添加请求头及优化图文上传

kth_win_android 2 years ago
parent
commit
eff57df41d

+ 12 - 3
README.md

@@ -1,13 +1,15 @@
 # BaseOkHttp
 
-![](https://img.shields.io/badge/okhttplib-2.0.8-green)
+![](https://img.shields.io/badge/okhttplib-2.0.9-green)
 
 
 OkHttp请求封装
 ----------
 使用
 ---
+
 ### 根目录下build.gradle添加Maven地址
+
 ~~~
 repositories {
         maven {
@@ -15,12 +17,15 @@ repositories {
         }
     }
 ~~~
+
 ### module 下添加
+
 ~~~
-implementation 'com.yanyi.benyanyi:okhttplib:2.0.8'
+implementation 'com.yanyi.benyanyi:okhttplib:2.0.9'
 ~~~
 
 或者
+
 ~~~
 <dependency>
   <groupId>com.yanyi.benyanyi</groupId>
@@ -31,6 +36,8 @@ implementation 'com.yanyi.benyanyi:okhttplib:2.0.8'
 ~~~
 
 ### 更新记录
+
+* 2023-04-11(2.0.9) 修改图文上传,添加请求头
 * 2021-08-27(2.0.8) 升级build包版本,修改maven地址
 * 2021-03-13(2.0.7) 多文件上传
 * 2020-10-24(2.0.6) 修复缓存文件android6.0时磁盘未挂载时报错问题
@@ -43,7 +50,8 @@ implementation 'com.yanyi.benyanyi:okhttplib:2.0.8'
 * 2019-07-19(1.1.4) 优化回调方法
 * 2019-06-14(1.1.3) 优化代码,更改包名,用于统一化开源库
 * 2019-03-06(1.1.2) 去除在线更新方法,将在线更新单独做开源库,去除多余下载方法,减少库大小
-* 2018-11-20(1.1.1) 添加带弹窗下载方法(UpdateUtil类),支持下载弹窗提示,下载进度弹窗,下载进度通知栏,强行下载,多文件下载类(DownloadManager)(支持取消下载)
+* 2018-11-20(1.1.1) 添加带弹窗下载方法(UpdateUtil类),支持下载弹窗提示,下载进度弹窗,下载进度通知栏,强行下载,多文件下载类(DownloadManager)(
+  支持取消下载)
 * 2018-10-10(1.1.0) 添加写入超时限定
 * 2018-09-28(1.0.9) 添加自定义Request方法
 * 2018-09-14(1.0.8) 优化下载文件方法
@@ -54,4 +62,5 @@ implementation 'com.yanyi.benyanyi:okhttplib:2.0.8'
 作者:演绎<br/>
 QQ:1541612424<br/>
 email: work@yanyi.red<br/>
+博客网站:benyanyi(演绎未来)&nbsp;&nbsp;&nbsp;将会不定期的更新关于android的一些文章
 微信公众号:benyanyi(演绎未来)&nbsp;&nbsp;&nbsp;将会不定期的更新关于android的一些文章

+ 1 - 1
okhttplib/src/main/java/com/benyanyi/okhttp/OkHttpUtil.java

@@ -2,7 +2,7 @@ package com.benyanyi.okhttp;
 
 import android.content.Context;
 
-import com.benyanyi.okhttp.type.HttpRequest;
+import com.benyanyi.okhttp.config.HttpRequest;
 import com.benyanyi.okhttp.type.RequestType;
 import com.benyanyi.okhttp.util.FormatUtil;
 

+ 2 - 1
okhttplib/src/main/java/com/benyanyi/okhttp/type/ApiDns.java → okhttplib/src/main/java/com/benyanyi/okhttp/config/ApiDns.java

@@ -1,4 +1,4 @@
-package com.benyanyi.okhttp.type;
+package com.benyanyi.okhttp.config;
 
 import org.jetbrains.annotations.NotNull;
 
@@ -37,4 +37,5 @@ class ApiDns implements Dns {
             throw unknownHostException;
         }
     }
+
 }

+ 1 - 1
okhttplib/src/main/java/com/benyanyi/okhttp/type/CacheInterceptor.java → okhttplib/src/main/java/com/benyanyi/okhttp/config/CacheInterceptor.java

@@ -1,4 +1,4 @@
-package com.benyanyi.okhttp.type;
+package com.benyanyi.okhttp.config;
 
 
 import android.content.Context;

+ 1 - 1
okhttplib/src/main/java/com/benyanyi/okhttp/type/Client.java → okhttplib/src/main/java/com/benyanyi/okhttp/config/Client.java

@@ -1,4 +1,4 @@
-package com.benyanyi.okhttp.type;
+package com.benyanyi.okhttp.config;
 
 import android.content.Context;
 

+ 1 - 1
okhttplib/src/main/java/com/benyanyi/okhttp/type/HttpConfig.java → okhttplib/src/main/java/com/benyanyi/okhttp/config/HttpConfig.java

@@ -1,4 +1,4 @@
-package com.benyanyi.okhttp.type;
+package com.benyanyi.okhttp.config;
 
 import android.content.Context;
 

+ 40 - 110
okhttplib/src/main/java/com/benyanyi/okhttp/type/HttpRequest.java → okhttplib/src/main/java/com/benyanyi/okhttp/config/HttpRequest.java

@@ -1,4 +1,4 @@
-package com.benyanyi.okhttp.type;
+package com.benyanyi.okhttp.config;
 
 import android.content.Context;
 
@@ -6,12 +6,12 @@ import com.benyanyi.okhttp.call.HttpCall;
 import com.benyanyi.okhttp.call.RequestConfig;
 import com.benyanyi.okhttp.download.DownloadCall;
 import com.benyanyi.okhttp.download.DownloadConfig;
+import com.benyanyi.okhttp.type.RequestType;
 import com.benyanyi.okhttp.util.FormatUtil;
 import com.benyanyi.okhttp.util.OkHttpLog;
 import com.google.gson.Gson;
 
 import java.io.File;
-import java.util.List;
 import java.util.Map;
 
 import okhttp3.FormBody;
@@ -83,40 +83,23 @@ public class HttpRequest implements RequestType {
     }
 
     @Override
-    public RequestConfig postFileText(Map<String, Object> bodyMap) {
+    public RequestConfig postMultipart(Map<String, Object> headerMap, Map<String, Object> bodyMap) {
         MultipartBody.Builder builder = new MultipartBody.Builder()
                 .setType(MultipartBody.FORM);
         if (FormatUtil.isMapNotEmpty(bodyMap)) {
             for (Map.Entry<String, Object> entry : bodyMap.entrySet()) {
                 if (entry.getValue() instanceof File) {
-                    File file = new File(entry.getValue().toString());
+                    File file = (File) entry.getValue();
                     RequestBody requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
                     builder.addFormDataPart(entry.getKey(), file.getName(), requestBody);
                 } else {
-                    builder.addFormDataPart(entry.getKey(), entry.getValue().toString());
-                }
-            }
-        }
-        MultipartBody multipartBody = builder.build();
-        request = new Request.Builder()
-                .url(url)
-                .post(multipartBody)
-                .build();
-        return send(true);
-    }
-
-    @Override
-    public RequestConfig postFileText(Map<String, Object> headerMap, Map<String, Object> bodyMap) {
-        MultipartBody.Builder builder = new MultipartBody.Builder()
-                .setType(MultipartBody.FORM);
-        if (FormatUtil.isMapNotEmpty(bodyMap)) {
-            for (Map.Entry<String, Object> entry : bodyMap.entrySet()) {
-                if (entry.getValue() instanceof File) {
                     File file = new File(entry.getValue().toString());
-                    RequestBody requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
-                    builder.addFormDataPart(entry.getKey(), file.getName(), requestBody);
-                } else {
-                    builder.addFormDataPart(entry.getKey(), entry.getValue().toString());
+                    if (file.exists()) {//判断是否为文件路径
+                        RequestBody requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
+                        builder.addFormDataPart(entry.getKey(), file.getName(), requestBody);
+                    } else {
+                        builder.addFormDataPart(entry.getKey(), entry.getValue().toString());
+                    }
                 }
             }
         }
@@ -135,18 +118,23 @@ public class HttpRequest implements RequestType {
     }
 
     @Override
-    public RequestConfig postJson(Map<String, Object> map) {
+    public RequestConfig postJson(Map<String, String> headerMap, Map<String, Object> map) {
         MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
+        Request.Builder builder = new Request.Builder();
+        if (FormatUtil.isMapNotEmpty(headerMap)) {
+            for (Map.Entry<String, String> entry : headerMap.entrySet()) {
+                builder.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
         if (FormatUtil.isMapNotEmpty(map)) {
             mCacheName = mCacheName + map;
             RequestBody requestBody = RequestBody.create(new Gson().toJson(map), mediaType);
-            request = new Request.Builder()
+            request = builder
                     .url(url)
-                    .addHeader("content-type", "application/json")
                     .post(requestBody)
                     .build();
         } else {
-            request = new Request.Builder()
+            request = builder
                     .url(url)
                     .post(new FormBody.Builder().build())
                     .build();
@@ -155,18 +143,23 @@ public class HttpRequest implements RequestType {
     }
 
     @Override
-    public RequestConfig postJson(String string) {
+    public RequestConfig postJson(Map<String, String> headerMap, String jsonBody) {
         MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
-        if (FormatUtil.isNotEmpty(string)) {
-            mCacheName = mCacheName + string;
-            RequestBody requestBody = RequestBody.create(string, mediaType);
-            request = new Request.Builder()
+        Request.Builder builder = new Request.Builder();
+        if (FormatUtil.isMapNotEmpty(headerMap)) {
+            for (Map.Entry<String, String> entry : headerMap.entrySet()) {
+                builder.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
+        if (FormatUtil.isNotEmpty(jsonBody)) {
+            mCacheName = mCacheName + jsonBody;
+            RequestBody requestBody = RequestBody.create(jsonBody, mediaType);
+            request = builder
                     .url(url)
-                    .addHeader("content-type", "application/json")
                     .post(requestBody)
                     .build();
         } else {
-            request = new Request.Builder()
+            request = builder
                     .url(url)
                     .post(new FormBody.Builder().build())
                     .build();
@@ -175,88 +168,25 @@ public class HttpRequest implements RequestType {
     }
 
     @Override
-    public RequestConfig xml(String xml) {
-        String str = xml;
+    public RequestConfig xml(Map<String, String> headerMap, String xmlBody) {
+        String str = xmlBody;
         MediaType mediaType = MediaType.parse("text/xml; charset=UTF-8");
         if (FormatUtil.isNotEmpty(str)) {
             mCacheName = mCacheName + str;
         } else {
             str = "";
         }
-        request = new Request.Builder()
-                .url(url)
-                .post(RequestBody.create(str, mediaType))
-                .build();
-        return send();
-    }
-
-    @Override
-    public RequestConfig file(Map<String, Object> map) {
-        MultipartBody.Builder builder = new MultipartBody.Builder()
-                .setType(MultipartBody.FORM);
-        if (FormatUtil.isMapNotEmpty(map)) {
-            for (Map.Entry<String, Object> entry : map.entrySet()) {
-                File file = new File(entry.getValue().toString());
-                RequestBody requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
-                builder.addFormDataPart(entry.getKey(), file.getName(), requestBody);
-            }
-        }
-        MultipartBody multipartBody = builder.build();
-        request = new Request.Builder()
-                .url(url)
-                .post(multipartBody)
-                .build();
-        return send(true);
-    }
-
-    @Override
-    public RequestConfig file(String... filePaths) {
-        MultipartBody.Builder builder = new MultipartBody.Builder()
-                .setType(MultipartBody.FORM);
-        if (filePaths != null && filePaths.length > 0) {
-            if (filePaths.length == 1) {
-                File file = new File(filePaths[0]);
-                RequestBody requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
-                builder.addFormDataPart("file", file.getName(), requestBody);
-            } else {
-                for (int i = 0; i < filePaths.length; i++) {
-                    File file = new File(filePaths[i]);
-                    RequestBody requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
-                    builder.addFormDataPart("file" + i, file.getName(), requestBody);
-                }
-            }
-        }
-        MultipartBody multipartBody = builder.build();
-        request = new Request.Builder()
-                .url(url)
-                .post(multipartBody)
-                .build();
-        return send(true);
-    }
-
-    @Override
-    public RequestConfig file(List<String> filePaths) {
-        MultipartBody.Builder builder = new MultipartBody.Builder()
-                .setType(MultipartBody.FORM);
-        if (filePaths != null && filePaths.size() > 0) {
-            if (filePaths.size() == 1) {
-                File file = new File(filePaths.get(0));
-                RequestBody requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
-                builder.addFormDataPart("file", file.getName(), requestBody);
-            } else {
-                for (int i = 0; i < filePaths.size(); i++) {
-                    File file = new File(filePaths.get(i));
-                    RequestBody requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
-                    builder.addFormDataPart("file" + i, file.getName(), requestBody);
-                }
+        Request.Builder builder = new Request.Builder();
+        if (FormatUtil.isMapNotEmpty(headerMap)) {
+            for (Map.Entry<String, String> entry : headerMap.entrySet()) {
+                builder.addHeader(entry.getKey(), entry.getValue());
             }
         }
-        MultipartBody multipartBody = builder.build();
-        request = new Request.Builder()
+        request = builder
                 .url(url)
-                .post(multipartBody)
+                .post(RequestBody.create(xmlBody, mediaType))
                 .build();
-        return send(true);
+        return send();
     }
 
     @Override

+ 1 - 1
okhttplib/src/main/java/com/benyanyi/okhttp/type/SslConfig.java → okhttplib/src/main/java/com/benyanyi/okhttp/config/SslConfig.java

@@ -1,4 +1,4 @@
-package com.benyanyi.okhttp.type;
+package com.benyanyi.okhttp.config;
 
 import java.security.SecureRandom;
 import java.security.cert.CertificateException;

+ 48 - 13
okhttplib/src/main/java/com/benyanyi/okhttp/type/RequestType.java

@@ -3,7 +3,6 @@ package com.benyanyi.okhttp.type;
 import com.benyanyi.okhttp.call.RequestConfig;
 import com.benyanyi.okhttp.download.DownloadConfig;
 
-import java.util.List;
 import java.util.Map;
 
 /**
@@ -14,39 +13,75 @@ import java.util.Map;
  */
 public interface RequestType {
 
+    /**
+     * get文本请求
+     *
+     * @param map 请求文本
+     */
     RequestConfig getText(Map<String, Object> map);
 
+    /**
+     * post文本请求
+     *
+     * @param headerMap 请求头
+     * @param bodyMap   请求体
+     */
     RequestConfig postText(Map<String, Object> headerMap, Map<String, Object> bodyMap);
 
-    RequestConfig postFileText(Map<String, Object> bodyMap);
-
-    RequestConfig postFileText(Map<String, Object> headerMap, Map<String, Object> bodyMap);
-
-    RequestConfig postJson(Map<String, Object> map);
-
-    RequestConfig postJson(String string);
-
-    RequestConfig xml(String xml);
+    /**
+     * post多图文上传
+     *
+     * @param headerMap 请求头
+     * @param bodyMap   请求体
+     */
+    RequestConfig postMultipart(Map<String, Object> headerMap, Map<String, Object> bodyMap);
 
-    RequestConfig file(Map<String, Object> map);
+    /**
+     * post json字符上传
+     *
+     * @param headerMap 请求头
+     * @param map       需要拼接成json的键值对
+     */
+    RequestConfig postJson(Map<String, String> headerMap, Map<String, Object> map);
 
-    RequestConfig file(String... filePaths);
+    /**
+     * post json字符上传
+     *
+     * @param headerMap 请求头
+     * @param jsonBody  请求体
+     */
+    RequestConfig postJson(Map<String, String> headerMap, String jsonBody);
 
-    RequestConfig file(List<String> filePaths);
+    /**
+     * post xml字符上传
+     *
+     * @param headerMap 请求头
+     * @param xmlBody   请求体
+     */
+    RequestConfig xml(Map<String, String> headerMap, String xmlBody);
 
+    /**
+     * 文件下载
+     */
     DownloadConfig download();
 
     /**
+     * 文件下载
+     *
      * @param isCover 是否覆盖之前的文件
      */
     DownloadConfig download(boolean isCover);
 
     /**
+     * 文件下载
+     *
      * @param suffix 文件名后缀
      */
     DownloadConfig download(String suffix);
 
     /**
+     * 文件下载
+     *
      * @param isCover 是否覆盖之前的文件
      * @param suffix  文件名后缀
      */