-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4165465
commit 22383ad
Showing
19 changed files
with
306 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
sample/src/main/java/com/drake/net/sample/convert/SerializationConvert.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
@file:Suppress("UNCHECKED_CAST") | ||
|
||
package com.drake.net.sample.convert | ||
|
||
import com.drake.net.NetConfig | ||
import com.drake.net.convert.NetConverter | ||
import com.drake.net.exception.ConvertException | ||
import com.drake.net.exception.RequestParamsException | ||
import com.drake.net.exception.ResponseException | ||
import com.drake.net.exception.ServerResponseException | ||
import com.drake.net.request.kType | ||
import kotlinx.serialization.SerializationException | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.serializer | ||
import okhttp3.Response | ||
import org.json.JSONObject | ||
import java.lang.reflect.Type | ||
|
||
class SerializationConvert( | ||
val success: String = "0", | ||
val code: String = "code", | ||
val message: String = "msg" | ||
) : NetConverter { | ||
|
||
val jsonDecoder = Json { | ||
ignoreUnknownKeys = true // JSON和数据模型字段可以不匹配 | ||
coerceInputValues = true // 如果JSON字段是Null则使用默认值 | ||
} | ||
|
||
override fun <R> onConvert(succeed: Type, response: Response): R? { | ||
try { | ||
return NetConverter.DEFAULT.onConvert<R>(succeed, response) | ||
} catch (e: ConvertException) { | ||
|
||
val code = response.code | ||
when { | ||
code in 200..299 -> { // 请求成功 | ||
val body = response.body?.string() ?: return null | ||
if (succeed === String::class.java) return body as R | ||
val jsonObject = JSONObject(body) // 获取JSON中后端定义的错误码和错误信息 | ||
if (jsonObject.getString(this.code) == success) { // 对比后端自定义错误码 | ||
return run { | ||
val kType = response.request.kType() ?: return null | ||
try { | ||
jsonDecoder.decodeFromString(Json.serializersModule.serializer(kType), jsonObject.getString("data")) as R | ||
} catch (e: SerializationException) { | ||
throw ConvertException(response, cause = e) | ||
} | ||
} | ||
} else { // 错误码匹配失败, 开始写入错误异常 | ||
throw ResponseException(response, jsonObject.optString(message, NetConfig.app.getString(com.drake.net.R.string.no_error_message))) | ||
} | ||
} | ||
code in 400..499 -> throw RequestParamsException(response) // 请求参数错误 | ||
code >= 500 -> throw ServerResponseException(response) // 服务器异常错误 | ||
else -> throw ConvertException(response) | ||
} | ||
} | ||
} | ||
} |
21 changes: 0 additions & 21 deletions
21
sample/src/main/java/com/drake/net/sample/model/ListModel.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.drake.net.sample.model | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class UserModel(var name: String, var age: Int, var height: Int) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
sample/src/main/java/com/drake/net/sample/ui/fragment/converter/BaseConvertFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.drake.net.sample.ui.fragment.converter | ||
|
||
import android.os.Bundle | ||
import android.view.Menu | ||
import android.view.MenuInflater | ||
import android.view.MenuItem | ||
import androidx.annotation.LayoutRes | ||
import androidx.fragment.app.Fragment | ||
import androidx.navigation.fragment.findNavController | ||
import androidx.navigation.ui.onNavDestinationSelected | ||
import com.drake.net.sample.R | ||
|
||
open class BaseConvertFragment(@LayoutRes contentLayoutId: Int = 0) : Fragment(contentLayoutId) { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setHasOptionsMenu(true) | ||
} | ||
|
||
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { | ||
super.onCreateOptionsMenu(menu, inflater) | ||
inflater.inflate(R.menu.menu_converter, menu) | ||
} | ||
|
||
override fun onOptionsItemSelected(item: MenuItem): Boolean { | ||
item.onNavDestinationSelected(findNavController()) | ||
return true | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
sample/src/main/java/com/drake/net/sample/ui/fragment/converter/FastJsonConvertFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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.drake.net.sample.ui.fragment.converter | ||
|
||
import android.os.Bundle | ||
import android.view.View | ||
import com.drake.net.Get | ||
import com.drake.net.sample.R | ||
import com.drake.net.sample.convert.FastJsonConvert | ||
import com.drake.net.sample.model.Model | ||
import com.drake.net.utils.scopeNetLife | ||
import kotlinx.android.synthetic.main.fragment_custom_convert.* | ||
|
||
|
||
class FastJsonConvertFragment : BaseConvertFragment(R.layout.fragment_custom_convert) { | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
tv_convert_tip.text = """ | ||
1. 阿里巴巴出品的Json解析库 | ||
2. 引入kotlin-reflect库可以支持kotlin默认值 | ||
""".trimIndent() | ||
|
||
scopeNetLife { | ||
tv_fragment.text = Get<Model>("api") { | ||
converter = FastJsonConvert() // 单例转换器, 此时会忽略全局转换器 | ||
}.await().data.request_method | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
sample/src/main/java/com/drake/net/sample/ui/fragment/converter/MoshiConvertFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* 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.drake.net.sample.ui.fragment.converter | ||
|
||
import android.os.Bundle | ||
import android.view.View | ||
import com.drake.net.Get | ||
import com.drake.net.sample.R | ||
import com.drake.net.sample.convert.MoshiConvert | ||
import com.drake.net.sample.model.Model | ||
import com.drake.net.utils.scopeNetLife | ||
import kotlinx.android.synthetic.main.fragment_custom_convert.* | ||
|
||
|
||
class MoshiConvertFragment : BaseConvertFragment(R.layout.fragment_custom_convert) { | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
tv_convert_tip.text = """ | ||
1. Square出品的JSON解析库 | ||
2. 支持Kotlin构造默认值 | ||
3. 具备注解和反射两种使用方式 | ||
4. 非可选类型反序列化时赋值Null会抛出异常 | ||
5, 不支持动态解析 | ||
""".trimIndent() | ||
|
||
scopeNetLife { | ||
tv_fragment.text = Get<Model>("api") { | ||
converter = MoshiConvert() // 单例转换器, 此时会忽略全局转换器 | ||
}.await().data.request_method | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
.../src/main/java/com/drake/net/sample/ui/fragment/converter/SerializationConvertFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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.drake.net.sample.ui.fragment.converter | ||
|
||
import android.os.Bundle | ||
import android.view.View | ||
import com.drake.net.Get | ||
import com.drake.net.sample.R | ||
import com.drake.net.sample.convert.SerializationConvert | ||
import com.drake.net.sample.model.UserModel | ||
import com.drake.net.utils.scopeNetLife | ||
import kotlinx.android.synthetic.main.fragment_custom_convert.* | ||
|
||
|
||
class SerializationConvertFragment : BaseConvertFragment(R.layout.fragment_custom_convert) { | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
tv_convert_tip.text = """ | ||
1. kotlin官方出品, 推荐使用 | ||
2. kotlinx.serialization 是Kotlin上是最完美的序列化工具 | ||
3. 相对其他解析库他解决泛型擦除机制, 支持任何泛型, 可直接返回Map/List/Pair... | ||
4. 多配置选项 | ||
5. 支持动态解析 | ||
6. 支持ProtoBuf/CBOR/JSON等数据 | ||
""".trimIndent() | ||
|
||
scopeNetLife { | ||
val userList = Get<List<UserModel>>("list-data") { | ||
// 该转换器直接解析JSON中的data字段, 而非返回的整个JSON字符串 | ||
converter = SerializationConvert() // 单例转换器, 此时会忽略全局转换器 | ||
}.await() | ||
|
||
tv_fragment.text = userList[0].name | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<menu xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<item | ||
android:id="@+id/serializationConvertFragment" | ||
android:title="SerializationConverter(★★★★★)" /> | ||
<item | ||
android:id="@+id/gsonConvertFragment" | ||
android:title="GsonConverter(★★★)" /> | ||
<item | ||
android:id="@+id/fastJsonConvertFragment" | ||
android:title="FastJsonConverter(★★★)" /> | ||
<item | ||
android:id="@+id/moshiConvertFragment" | ||
android:title="MoshiConverter(★★)" /> | ||
</menu> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.