dependencies
implementation("org.chromium.net:cronet-embedded:119.6045.31")
implementation("org.chromium.net:cronet-api:119.6045.31")
implementation("com.android.volley:volley:1.2.1")
implementation("com.android.volley:volley-cronet:1.2.1")
demo
fun test() {
val context = this@MainActivity
val cronetEngine = CronetEngine.Builder(this)
.enableHttpCache(
CronetEngine.Builder.HTTP_CACHE_IN_MEMORY,
100 * 1024.toLong()
)
.enableQuic(true)
.enableHttp2(true).build()
val httpStack = CronetHttpStack.Builder(context.applicationContext)
.setCronetEngine(cronetEngine).build()
val cache = DiskBasedCache(context.cacheDir, 1024 * 1024)
val network = BasicAsyncNetwork.Builder(httpStack).build()
val requestQueue = AsyncRequestQueue.Builder(network)
.setCache(cache).build()
requestQueue.start()
// GET
val getUrl = "https://quic.aiortc.org/123"
val stringRequest = StringRequest(
Request.Method.GET, getUrl,
{ response -> toast("Response is: $response") },
{ toast("GET didn't work!") }
)
requestQueue.add(stringRequest)
// POST
val postUrl = "https://quic.aiortc.org/echo"
val reqData = JSONObject().apply { put("time", "now") }
val jsonRequest = JsonObjectRequest(
Request.Method.POST, postUrl, reqData,
{ response: JSONObject -> toast("Response is: $response") },
{ toast("POST didn't work!") }
)
requestQueue.add(jsonRequest)
}
private fun toast(msg: String) {
val context = this@MainActivity
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
}
Reference
- volley support http3, https://blog.csdn.net/yeshennet/article/details/149907813
- https://quic.aiortc.org/
- https://github.com/google/volley/wiki/Asynchronous-Volley