Change Redirect Policy of Volley Framework

后端 未结 4 1936
别跟我提以往
别跟我提以往 2020-12-14 05:25

I am using the Volley framework in a project where I always need to handle the redirects myself to handle the headers.

How redirects are handled depends right now o

4条回答
  •  独厮守ぢ
    2020-12-14 05:45

    Try on more call with the Location value from the network response headers

    Kotlin Implementation

    fun fetchRemoteList(
        url: String,
        context: Context,
        callback: OnFetchListCompleted,
        firstTry: Boolean = true
    ) {
        val queue = Volley.newRequestQueue(context)
        val stringRequest = StringRequest(
            Request.Method.GET, url,
            Response.Listener { response ->
                GlobalScope.launch {
                    callback.fetchListSucceed()
                }
            },
            Response.ErrorListener { error ->
                val locationRedirectUrl: String? = error?.networkResponse?.headers?.get("Location")
    
                if (firstTry && !locationRedirectUrl.isNullOrEmpty()) {
                    fetchRemoteList(locationRedirectUrl, context, callback, false)
                } else {
                    callback.fetchListFailed(error?.message ?: "")
                }
            })
    
        queue.add(stringRequest)
    }
    

提交回复
热议问题