Skip to content

Instantly share code, notes, and snippets.

@mstfldmr
Created March 19, 2015 08:51
Show Gist options
  • Save mstfldmr/f6594b2337e3633673e5 to your computer and use it in GitHub Desktop.
Save mstfldmr/f6594b2337e3633673e5 to your computer and use it in GitHub Desktop.
Example of Volley GET and POST requests with parameters and headers
RequestQueue queue = Volley.newRequestQueue(context);
//for POST requests, only the following line should be changed to
StringRequest sr = new StringRequest(Request.Method.GET, "http://headers.jsontest.com/",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("HttpClient", "success! response: " + response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("HttpClient", "error: " + error.toString());
}
})
{
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("user","YOUR USERNAME");
params.put("pass","YOUR PASSWORD");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
};
queue.add(sr);
@hzandi
Copy link

hzandi commented Sep 26, 2015

getParams() just work in Post and Put request :(

@AndroVijay
Copy link

how it post because your have use Request.Method.GET method...

@KudzaiMutsvairo
Copy link

How do I post from my own variables instead of writing the strings for line 21 and 22

@bensongathee
Copy link

Really helpful!!!

@bensongathee
Copy link

Thank you

@yuri2397
Copy link

Merci

@siddhraj-sinh
Copy link

how it post because your have use Request.Method.GET method...

i think you miss understood this, first he performed GET request and then he added some parameters (here username & password) into the BASE URL

hence, the final URL request looks like BASE_URL+params

so in short it is GET request having some query parameters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment