Skip to content

Instantly share code, notes, and snippets.

@TheCrazyMax
Forked from jchernandez/SimpleRequest.java
Created October 21, 2016 20:52
Show Gist options
  • Save TheCrazyMax/fb07036a9a14c2dbf210597c69994a2f to your computer and use it in GitHub Desktop.
Save TheCrazyMax/fb07036a9a14c2dbf210597c69994a2f to your computer and use it in GitHub Desktop.
Simple Post Request with basic auth, using android Volley library
StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
///handle response from service
}, new ErrorResponse() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//handle error response
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
//add params <key,value>
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers = Constants.getHeaders(context);
// add headers <key,value>
String credentials = USERNAME+":"+PASSWORD;
String auth = "Basic "
+ Base64.encodeToString(credentials.getBytes(),
Base64.NO_WRAP);
headers.put("Authorization", auth);
return headers;
}
};
mQueue.add(request);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment