|
@@ -1,13 +1,12 @@
|
|
|
package com.drake.net.sample.ui.fragment
|
|
|
|
|
|
import android.os.Bundle
|
|
|
+import android.util.Log
|
|
|
import android.view.View
|
|
|
import androidx.fragment.app.Fragment
|
|
|
import com.drake.net.Net
|
|
|
-import com.drake.net.interfaces.NetCallback
|
|
|
import com.drake.net.sample.R
|
|
|
import kotlinx.android.synthetic.main.fragment_fastest.*
|
|
|
-import okhttp3.Call
|
|
|
|
|
|
class EnqueueRequestFragment : Fragment(R.layout.fragment_enqueue_request) {
|
|
|
|
|
@@ -24,12 +23,28 @@ class EnqueueRequestFragment : Fragment(R.layout.fragment_enqueue_request) {
|
|
|
// })
|
|
|
|
|
|
// NetCallback支持数据转换
|
|
|
- Net.post("api") {
|
|
|
- param("password", "Net123")
|
|
|
- }.enqueue(object : NetCallback<String>() {
|
|
|
- override fun onSuccess(call: Call, data: String) {
|
|
|
- tv_fragment.text = data // onSuccess 属于主线程
|
|
|
+ // Net.post("api") {
|
|
|
+ // param("password", "Net123")
|
|
|
+ // }.enqueue(object : NetCallback<String>() {
|
|
|
+ // override fun onSuccess(call: Call, result: String) {
|
|
|
+ // tv_fragment.text = result // onSuccess 属于主线程
|
|
|
+ // }
|
|
|
+ // })
|
|
|
+
|
|
|
+ // 简化版本的队列请求
|
|
|
+ Net.post("api").onResult<String> {
|
|
|
+
|
|
|
+ getOrNull()?.let { // 如果成功就不为Null
|
|
|
+ Log.d("日志", "请求成功")
|
|
|
+ tv_fragment.text = it
|
|
|
}
|
|
|
- })
|
|
|
+
|
|
|
+ exceptionOrNull()?.apply {
|
|
|
+ Log.d("日志", "请求失败")
|
|
|
+ printStackTrace() // 如果发生错误就不为Null
|
|
|
+ }
|
|
|
+
|
|
|
+ Log.d("日志", "完成请求")
|
|
|
+ }
|
|
|
}
|
|
|
}
|