Skip to content

Instantly share code, notes, and snippets.

@nguyenngan
Forked from jchernandez/SimpleRequest.java
Created October 3, 2016 10:35
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 nguyenngan/308d08fcf3d7eb0c7a4278e5d1d7e6bb to your computer and use it in GitHub Desktop.
Save nguyenngan/308d08fcf3d7eb0c7a4278e5d1d7e6bb 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);
Copy link

ghost commented Feb 25, 2017

it is not working for me. Please help me

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