Skip to content

Instantly share code, notes, and snippets.

@elizarov
Created August 28, 2017 12:32
Show Gist options
  • Save elizarov/14eb243ed06298e111258f441f647e29 to your computer and use it in GitHub Desktop.
Save elizarov/14eb243ed06298e111258f441f647e29 to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.experimental.runBlocking
import okhttp3.*
import java.io.IOException
import kotlin.coroutines.experimental.suspendCoroutine
fun main(args: Array<String>) {
val http = OkHttpClient.Builder().build()
val appid = "mock"
val secret = "mock"
val code = "mock"
val api = "https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${code}&grant_type=authorization_code"
val request = Request.Builder().get().url(api).build()
val call1 = http.newCall(request)
runBlocking {
val result = suspendCoroutine<String?> { co ->
call1.enqueue(object: Callback {
override fun onFailure(call: Call, e: IOException) {
co.resumeWithException(e)
}
override fun onResponse(call: Call, response: Response) {
co.resume(response.body()?.string())
}
})
}
// Exception in thread "main" java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.String
println(result)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment