Skip to content
StormKid edited this page Jul 2, 2019 · 4 revisions

1、直接请求

fun doPost(){
  Okkt.instance.Builder().setUrl("/part").postString(object:StringCallback{
            override suspend fun onSuccess(entity: String, flag: String) {
            }

            override suspend fun onFailed(error: String) {
            }

  })
}

2、对象请求

data class MyData(val msg:String, val data:Any , val code:Int)

fun doPost(){
  Okkt.instance.Builder().setUrl("/part").post(object:CallbackRule<MyData>{
            override suspend fun onSuccess(entity: MyData, flag: String) {
            }

            override suspend fun onFailed(error: String) {
            }

  })
}

3、表单请求

data class MyData(val msg:String, val data:Any , val code:Int)

fun doPost(){
  Okkt.instance.Builder().setUrl("/part").putBody(hashMapOf("id" to "xxx")).post(object:CallbackRule<MyData>{
            override suspend fun onSuccess(entity: MyData, flag: String) {
            }

            override suspend fun onFailed(error: String) {
            }

  })
}

4、application/json 请求

data class MyData(val msg:String, val data:Any , val code:Int)

fun doPost(){
  Okkt.instance.Builder().setUrl("/part").putBody(hashMapOf("id" to "xxx")).postJson(object:CallbackRule<MyData>{
            override suspend fun onSuccess(entity: MyData, flag: String) {
            }

            override suspend fun onFailed(error: String) {
            }

  })
}

5、自定义请求

  class MainActivity : AppCompatActivity(),CallbackRule<YourDataClass>,OtherCallback{
    override suspend fun onSuccess(entity: YourDataClass, flag: String) {
    }

    override suspend fun onFailed(error: String) {
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Okkt.instance.Builder().setUrl("/part").putBody(hashMapOf("id" to "what")).setParams(hashMapOf("index" to "2")).setFlag("any").post(this)
    }

    ....
    override fun otherCallbackFunction(){}
}
方法名 参数类型 方法描述
setParams HashMap 拼接在url字符串后面的请求参数
putBody HashMap 请求传递的请求体
setFlag String 请求标志,根据不同请求标志来识别对应的请求从而在success中进行分别对应
setUrl String 请求base后拼接的内容