Browse Source

更新JSON请求体示例代码以及api和文档

drake 4 years ago
parent
commit
a48356f3c6

+ 1 - 1
README.md

@@ -110,7 +110,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.12'
+implementation 'com.github.liangjingkanji:Net:2.3.13'
 ```
 
 <br>

+ 10 - 0
docs/index.md

@@ -77,6 +77,16 @@ scopeNetLife { // 创建作用域
 |`binary`|二进制|
 |`binaries`|多个二进制|
 |`body`|自定义请求体|
+|`json`|请求参数为JSONObject/JsonArray/String|
+
+
+
+### JSON请求体
+
+在Demo中可以查看具体JSON请求的代码
+对于某些可能JSON请求参数存在固定值, 可以考虑继承JsonBody来扩展出自己的新的Body(), 然后使用`body`函数传入请求中.
+
+> JsonBody实际上也是继承自StringBody
 
 
 ## RESTFUL

+ 3 - 0
docs/updates.md

@@ -1,3 +1,6 @@
+## 2.3.13
+简化Json的请求体创建
+
 ## 2.3.8
 日志记录器默认支持输出Json参数
 

+ 27 - 0
kalle/src/main/java/com/yanzhenjie/kalle/BodyRequest.java

@@ -15,6 +15,9 @@
  */
 package com.yanzhenjie.kalle;
 
+import org.json.JSONArray;
+import org.json.JSONObject;
+
 import java.io.File;
 import java.util.List;
 
@@ -333,6 +336,30 @@ public class BodyRequest extends Request {
             this.mBody = body;
             return (T) this;
         }
+
+        /**
+         * Set JSON request body.
+         */
+        public T json(JSONObject json) {
+            this.mBody = new JsonBody(json);
+            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(String json) {
+            this.mBody = new JsonBody(json);
+            return (T) this;
+        }
     }
 
     public static class Builder extends BodyRequest.Api<BodyRequest.Builder> {

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

@@ -15,6 +15,9 @@
  */
 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;
@@ -28,6 +31,14 @@ public class JsonBody extends StringBody {
         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);
     }

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

@@ -28,6 +28,8 @@ import com.drake.net.*
 import com.drake.net.sample.R
 import com.drake.net.utils.scopeNetLife
 import kotlinx.android.synthetic.main.fragment_async_task.*
+import org.json.JSONArray
+import org.json.JSONObject
 
 
 class RequestMethodFragment : Fragment(R.layout.fragment_request_method) {
@@ -84,6 +86,26 @@ class RequestMethodFragment : Fragment(R.layout.fragment_request_method) {
         }
     }
 
+
+    /**
+     * 请求参数为JSON
+     */
+    private fun JSON() {
+        scopeNetLife {
+            tv_fragment.text = Post<String>("api") {
+                json(JSONObject().run {
+                    put("name", "金城武")
+                    put("age", "23")
+                    put("measurements", JSONArray().run {
+                        put(100)
+                        put(100)
+                        put(100)
+                    })
+                })
+            }.await()
+        }
+    }
+
     override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
         super.onCreateOptionsMenu(menu, inflater)
         inflater.inflate(R.menu.menu_request_method, menu)
@@ -99,6 +121,7 @@ class RequestMethodFragment : Fragment(R.layout.fragment_request_method) {
             R.id.delete -> DELETE()
             R.id.trace -> TRACE()
             R.id.options -> OPTIONS()
+            R.id.json -> JSON()
         }
         return super.onOptionsItemSelected(item)
     }

+ 3 - 0
sample/src/main/res/menu/menu_request_method.xml

@@ -39,4 +39,7 @@
     <item
         android:id="@+id/options"
         android:title="OPTIONS" />
+    <item
+        android:id="@+id/json"
+        android:title="JSON请求" />
 </menu>