Skip to content

Commit 49fcf3f

Browse files
committed
增加jsonArray post方法,升级gradle
1 parent d1e1dc3 commit 49fcf3f

File tree

6 files changed

+32
-9
lines changed

6 files changed

+32
-9
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22
plugins {
3-
id 'com.android.application' version '7.2.2' apply false
4-
id 'com.android.library' version '7.2.2' apply false
3+
id 'com.android.application' version '7.3.0' apply false
4+
id 'com.android.library' version '7.3.0' apply false
55
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
66
}
77

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Sat Aug 27 15:58:56 SGT 2022
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME

oklibrary/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ afterEvaluate {
5151
// You can then customize attributes of the publication as shown below.
5252
groupId = 'com.gateoftruth.library'
5353
artifactId = 'OkSimple'
54-
version = "3.3.5"
54+
version = "3.3.7"
5555
}
5656
}
5757
}

oklibrary/src/main/java/com/gateoftruth/oklibrary/BaseRequest.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import okhttp3.MediaType.Companion.toMediaType
55
import okhttp3.RequestBody.Companion.asRequestBody
66
import okhttp3.RequestBody.Companion.toRequestBody
77
import okhttp3.internal.headersContentLength
8+
import org.json.JSONArray
89
import org.json.JSONObject
910
import java.io.File
1011
import java.io.IOException
@@ -34,6 +35,8 @@ abstract class BaseRequest(val url: String, val type: String) {
3435

3536
protected var postJSONObject = JSONObject()
3637

38+
protected var postJSONArray = JSONArray()
39+
3740
protected lateinit var uploadFile: File
3841

3942
internal var defaultFileMediaType = OkSimpleConstant.STREAM_MEDIA_TYPE_STRING.toMediaType()
@@ -87,12 +90,11 @@ abstract class BaseRequest(val url: String, val type: String) {
8790
}
8891

8992
OkSimpleConstant.POST_JSON -> {
90-
val jsonString = postJSONObject.toString()
91-
val requestBody =
92-
jsonString.toRequestBody(OkSimpleConstant.JSON_MEDIA_TYPE_STRING.toMediaType())
93-
contentString = "type:POST_JSON;$jsonString"
94-
requestBuilder.post(requestBody)
93+
stringPost(postJSONObject.toString(),OkSimpleConstant.POST_JSON)
94+
}
9595

96+
OkSimpleConstant.POST_JSON_ARRAY -> {
97+
stringPost(postJSONArray.toString(),OkSimpleConstant.POST_JSON_ARRAY)
9698
}
9799

98100
OkSimpleConstant.GET_BITMAP -> {
@@ -181,6 +183,12 @@ abstract class BaseRequest(val url: String, val type: String) {
181183

182184
}
183185

186+
private fun stringPost(string: String,type:String){
187+
val requestBody = string.toRequestBody(OkSimpleConstant.JSON_MEDIA_TYPE_STRING.toMediaType())
188+
contentString = "type:$type;$string"
189+
requestBuilder.post(requestBody)
190+
}
191+
184192
private fun appendParamsMapToUrl(map: Map<String, String>) {
185193
val stringBuilder = StringBuilder()
186194
stringBuilder.append(requestUrl)
@@ -231,6 +239,10 @@ abstract class BaseRequest(val url: String, val type: String) {
231239
postJSONObject = jsonObject
232240
}
233241

242+
fun postJsonArray(jsonArray: JSONArray) = apply {
243+
postJSONArray = jsonArray
244+
}
245+
234246
fun uploadFile(file: File) = apply {
235247
uploadFile = file
236248
}

oklibrary/src/main/java/com/gateoftruth/oklibrary/OkSimple.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import okhttp3.Interceptor
55
import okhttp3.MediaType.Companion.toMediaType
66
import okhttp3.OkHttpClient
77
import okhttp3.RequestBody
8+
import org.json.JSONArray
89
import org.json.JSONObject
910
import java.io.File
1011
import java.util.*
@@ -63,6 +64,15 @@ object OkSimple {
6364
return request
6465
}
6566

67+
fun postJson(url: String, jsonArray: JSONArray, isSync: Boolean = false): BaseRequest {
68+
val request = if (isSync) SynchronizeRequest(
69+
url,
70+
OkSimpleConstant.POST_JSON_ARRAY
71+
) else AsynchronousRequest(url, OkSimpleConstant.POST_JSON_ARRAY)
72+
request.postJsonArray(jsonArray)
73+
return request
74+
}
75+
6676
fun post(
6777
url: String,
6878
valueMap: Map<String, String> = HashMap(),

oklibrary/src/main/java/com/gateoftruth/oklibrary/OkSimpleConstant.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ object OkSimpleConstant {
1010
const val PATCH = "PATCH"
1111
const val POST = "POST"
1212
const val POST_JSON = "postJson"
13+
const val POST_JSON_ARRAY = "postJsonArray"
1314
const val GET_BITMAP = "getBitmap"
1415
const val UPLOAD_FILE = "uploadFile"
1516
const val POST_FORM = "postForm"

0 commit comments

Comments
 (0)