Browse Source

JSON请求体支持Pair和Map类型参数

drake 3 years ago
parent
commit
e4e95ec61c

+ 1 - 1
README.md

@@ -106,7 +106,7 @@ implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
 // 支持自动下拉刷新和缺省页的(可选)
 implementation 'com.github.liangjingkanji:BRV:1.3.19'
 
-implementation 'com.github.liangjingkanji:Net:2.3.14'
+implementation 'com.github.liangjingkanji:Net:2.3.15'
 ```
 
 <br>

+ 2 - 2
docs/index.md

@@ -85,7 +85,7 @@ scopeNetLife { // 创建作用域
 
 这里提供三种创建Json请求的示例代码. 酌情选用
 
-=== "JSON字符串(推荐)"
+=== "JSON键值对(推荐)"
     ```kotlin
     val name = "金城武"
     val age = 29
@@ -93,7 +93,7 @@ scopeNetLife { // 创建作用域
 
     scopeNetLife {
         tv_fragment.text = Post<String>("api") {
-            json("{name:$name, age:$age, measurements:$measurements}")
+            json("name" to name, "age" to age, "measurements" to measurements) // 同时也支持Map集合
         }.await()
     }
     ```

+ 3 - 0
docs/updates.md

@@ -1,3 +1,6 @@
+## 2.3.15
+[json]请求体支持Map和Pair参数
+
 ## 2.3.14
 支持在ViewModel中创建绑定ViewModel生命周期的作用域: scopeLife/scopeNetLife
 

+ 13 - 6
kalle/src/main/java/com/yanzhenjie/kalle/BodyRequest.java

@@ -20,6 +20,9 @@ import org.json.JSONObject;
 
 import java.io.File;
 import java.util.List;
+import java.util.Map;
+
+import kotlin.Pair;
 
 /**
  * Created by Zhenjie Yan on 2018/2/13.
@@ -345,17 +348,21 @@ public class BodyRequest extends Request {
             return (T) this;
         }
 
-        /**
-         * Set JSON request body.
-         */
         public T json(JSONArray json) {
             this.mBody = new JsonBody(json);
             return (T) this;
         }
 
-        /**
-         * Set JSON request body.
-         */
+        public T json(Pair<String, Object>... json) {
+            this.mBody = new JsonBody(json);
+            return (T) this;
+        }
+
+        public T json(Map<String, Object> json) {
+            this.mBody = new JsonBody(json);
+            return (T) this;
+        }
+
         public T json(String json) {
             this.mBody = new JsonBody(json);
             return (T) this;

+ 0 - 45
kalle/src/main/java/com/yanzhenjie/kalle/JsonBody.java

@@ -1,45 +0,0 @@
-/*
- * Copyright (C) 2018 Drake, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.yanzhenjie.kalle;
-
-import org.json.JSONArray;
-import org.json.JSONObject;
-
-import java.nio.charset.Charset;
-
-import static com.yanzhenjie.kalle.Headers.VALUE_APPLICATION_JSON;
-
-/**
- * Created by Zhenjie Yan on 2018/2/11.
- */
-public class JsonBody extends StringBody {
-
-    public JsonBody(String body) {
-        this(body, Kalle.getConfig().getCharset());
-    }
-
-    public JsonBody(JSONObject body) {
-        this(body.toString(), Kalle.getConfig().getCharset());
-    }
-
-    public JsonBody(JSONArray body) {
-        this(body.toString(), Kalle.getConfig().getCharset());
-    }
-
-    public JsonBody(String body, Charset charset) {
-        super(body, charset, VALUE_APPLICATION_JSON);
-    }
-}

+ 30 - 0
kalle/src/main/java/com/yanzhenjie/kalle/JsonBody.kt

@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2018 Drake, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.yanzhenjie.kalle
+
+import org.json.JSONArray
+import org.json.JSONObject
+import java.nio.charset.Charset
+
+class JsonBody @JvmOverloads constructor(
+    body: String,
+    charset: Charset = Kalle.getConfig().charset
+) : StringBody(body, charset, Headers.VALUE_APPLICATION_JSON) {
+    constructor(body: JSONObject) : this(body.toString())
+    constructor(body: JSONArray) : this(body.toString())
+    constructor(body: Map<String, Any?>) : this(JSONObject(body))
+    constructor(vararg body: Pair<String, Any?>) : this(body.toMap())
+}

+ 1 - 1
sample/src/main/java/com/drake/net/sample/ui/fragment/RequestMethodFragment.kt

@@ -106,7 +106,7 @@ class RequestMethodFragment : Fragment(R.layout.fragment_request_method) {
 
             // 创建JSON
             tv_fragment.text = Post<String>("api") {
-                json("{name:$name, age:$age, measurements:$measurements}")
+                json("name" to name, "age" to age, "measurements" to measurements) // 同时支持Map集合
             }.await()
         }
     }