79724607

Date: 2025-08-04 07:48:05
Score: 1
Natty:
Report link

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

  1. volley support http3, https://blog.csdn.net/yeshennet/article/details/149907813
  2. https://quic.aiortc.org/
  3. https://github.com/google/volley/wiki/Asynchronous-Volley
Reasons:
  • Probably link only (1):
  • Contains signature (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: yeshen