Browse Source

post文本请求是否编码

kth_win_android 2 years ago
parent
commit
8d81af5c91

+ 22 - 0
okhttplib/src/main/java/com/benyanyi/okhttp/config/HttpRequest.java

@@ -82,6 +82,28 @@ public class HttpRequest implements RequestType {
         return send();
     }
 
+    @Override
+    public RequestConfig postTextEncoded(Map<String, Object> headerMap, Map<String, Object> bodyMap) {
+        FormBody.Builder builder = new FormBody.Builder();
+        if (FormatUtil.isMapNotEmpty(bodyMap)) {
+            mCacheName = mCacheName + bodyMap;
+            for (Map.Entry<String, Object> entry : bodyMap.entrySet()) {
+                builder.addEncoded(entry.getKey(), entry.getValue().toString());
+            }
+        }
+        FormBody build = builder.build();
+        Request.Builder builder1 = new Request.Builder();
+        if (FormatUtil.isMapNotEmpty(headerMap)) {
+            for (Map.Entry<String, Object> entry : headerMap.entrySet()) {
+                builder1.addHeader(entry.getKey(), entry.getValue().toString());
+            }
+        }
+        request = builder1.url(url)
+                .post(build)
+                .build();
+        return send();
+    }
+
     @Override
     public RequestConfig postMultipart(Map<String, Object> headerMap, Map<String, Object> bodyMap) {
         MultipartBody.Builder builder = new MultipartBody.Builder()

+ 10 - 0
okhttplib/src/main/java/com/benyanyi/okhttp/type/RequestType.java

@@ -22,12 +22,22 @@ public interface RequestType {
 
     /**
      * post文本请求
+     * 对请求体进行URL编码
      *
      * @param headerMap 请求头
      * @param bodyMap   请求体
      */
     RequestConfig postText(Map<String, Object> headerMap, Map<String, Object> bodyMap);
 
+    /**
+     * post文本请求
+     * 不对请求体进行URL编码
+     *
+     * @param headerMap 请求头
+     * @param bodyMap   请求体
+     */
+    RequestConfig postTextEncoded(Map<String, Object> headerMap, Map<String, Object> bodyMap);
+
     /**
      * post多图文上传
      *