mirror of
https://github.com/tursom/TursomServer.git
synced 2025-03-14 03:40:06 +08:00
add BlockingCallAdapterFactory
This commit is contained in:
parent
604f87ff66
commit
43e9e7ed3e
@ -398,4 +398,11 @@ fun Class<*>.forAllFields(action: (Field) -> Unit) {
|
||||
clazz = clazz.superclass
|
||||
}
|
||||
clazz.declaredFields.forEach(action)
|
||||
}
|
||||
|
||||
fun <T, R> Iterable<T>.firstNotNull(selector: (T) -> R): R? {
|
||||
forEach {
|
||||
return selector(it) ?: return@forEach
|
||||
}
|
||||
return null
|
||||
}
|
@ -11,9 +11,9 @@ dependencies {
|
||||
//implementation("com.squareup.okhttp3:okhttp:3.14.1")
|
||||
//implementation group: 'cglib', name: 'cglib', version: '3.3.0'
|
||||
// https://mvnrepository.com/artifact/com.squareup.retrofit2/converter-gson
|
||||
implementation group: 'com.squareup.retrofit2', name: 'converter-gson', version: '2.9.0'
|
||||
api group: 'com.squareup.retrofit2', name: 'converter-gson', version: '2.9.0'
|
||||
// https://mvnrepository.com/artifact/com.squareup.retrofit2/retrofit
|
||||
implementation group: 'com.squareup.retrofit2', name: 'retrofit', version: '2.9.0'
|
||||
compile group: 'com.squareup.retrofit2', name: 'retrofit', version: '2.9.0'
|
||||
|
||||
// https://mvnrepository.com/artifact/org.jsoup/jsoup
|
||||
api group: 'org.jsoup', name: 'jsoup', version: '1.13.1'
|
||||
|
@ -0,0 +1,20 @@
|
||||
package cn.tursom.http
|
||||
|
||||
import retrofit2.Call
|
||||
import retrofit2.CallAdapter
|
||||
import retrofit2.Retrofit
|
||||
import java.lang.reflect.Type
|
||||
|
||||
object BlockingCallAdapterFactory : CallAdapter.Factory() {
|
||||
override fun get(
|
||||
returnType: Type,
|
||||
annotations: Array<Annotation>,
|
||||
retrofit: Retrofit
|
||||
): CallAdapter<*, *>? {
|
||||
if (annotations.any { it is retrofit2.SkipCallbackExecutor }) return null
|
||||
return object : CallAdapter<Any, Any> {
|
||||
override fun responseType(): Type = returnType
|
||||
override fun adapt(call: Call<Any>): Any? = call.execute().body()
|
||||
}
|
||||
}
|
||||
}
|
@ -1,12 +1,18 @@
|
||||
package cn.tursom.http
|
||||
|
||||
import cn.tursom.utils.coroutine.MainDispatcher
|
||||
import cn.tursom.utils.gson
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import org.jsoup.nodes.Document
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import retrofit2.create
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Path
|
||||
import retrofit2.http.Query
|
||||
|
||||
|
||||
interface CoroutineLocalTest {
|
||||
@GET("/")
|
||||
@ -17,21 +23,31 @@ interface CoroutineLocalTest {
|
||||
|
||||
@GET("status/{db}")
|
||||
suspend fun status(@Path("db") db: String): RoomStatus
|
||||
|
||||
@GET("room/v1/Room/get_info")
|
||||
suspend fun getRoomInfo(@Query("id") roomId: Int, @Query("from") from: String = "room"): String
|
||||
}
|
||||
|
||||
suspend fun main() {
|
||||
MainDispatcher.init()
|
||||
val retrofit = Retrofit.Builder()
|
||||
//.baseUrl("http://tursom.cn:15015")
|
||||
.baseUrl("https://www.baidu.com")
|
||||
//.baseUrl("https://www.baidu.com")
|
||||
.baseUrl("https://api.live.bilibili.com")
|
||||
.addCallAdapterFactory(BlockingCallAdapterFactory)
|
||||
.addConverterFactory(StringConverterFactory)
|
||||
.addConverterFactory(HtmlConverterFactory)
|
||||
.addConverterFactory(XmlConverterFactory)
|
||||
.addConverterFactory(GsonConverterFactory.create(gson))
|
||||
.build()
|
||||
val coroutineLocalTest: CoroutineLocalTest = retrofit.create()
|
||||
println(coroutineLocalTest.test())
|
||||
//println(coroutineLocalTest.status())
|
||||
//println(coroutineLocalTest.status("wula"))
|
||||
GlobalScope.launch(Dispatchers.Main) {
|
||||
launch {
|
||||
println(Thread.currentThread().name)
|
||||
}
|
||||
val coroutineLocalTest: CoroutineLocalTest = retrofit.create()
|
||||
//println(coroutineLocalTest.status())
|
||||
println(coroutineLocalTest.getRoomInfo(801580))
|
||||
}.join()
|
||||
}
|
||||
|
||||
data class RoomStatus(
|
||||
|
Loading…
Reference in New Issue
Block a user