Skip to content

Instantly share code, notes, and snippets.

@pankpan
Last active August 29, 2021 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pankpan/6dd03b7fae550975336f94bc68463b0c to your computer and use it in GitHub Desktop.
Save pankpan/6dd03b7fae550975336f94bc68463b0c to your computer and use it in GitHub Desktop.
Android Kotlin Volley POST data with header
// implementation 'com.android.volley:volley:1.2.1'
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
var apiUrl="https://your-api-url.com/"
val request: StringRequest = object : StringRequest(
Method.POST, apiUrl,
Response.Listener { response ->
if (response != null) {
Log.d("Your Array Response", response)
} else {
Log.d("Your Array Response", "Data Null")
}
},
Response.ErrorListener { error -> Log.e("error is ", "" + error) }) {
//This is for Headers If You Needed
@Throws(AuthFailureError::class)
override fun getHeaders(): Map<String, String> {
val params: MutableMap<String, String> = HashMap()
params["Cookie"] = "key1=val1; key2=val2"
params["User-Agent"] = "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0"
return params
}
//Pass Your Parameters here
override fun getParams(): Map<String, String>? {
val params: MutableMap<String, String> = HashMap()
params["var1"] = "foo"
params["var2"] = "bar"
return params
}
}
val queue = Volley.newRequestQueue(applicationContext)
queue.add(request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment