Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.util.Base64;
- import com.android.volley.AuthFailureError;
- import com.android.volley.Response;
- import com.android.volley.toolbox.StringRequest;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * Created by Glenn on 8/25/2016.
- */
- public class AuthStringRequest extends StringRequest {
- private String username, password, token;
- public AuthStringRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
- super(method, url, listener, errorListener);
- }
- public AuthStringRequest(String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
- super(url, listener, errorListener);
- }
- public void prepareHeader(String username, String password) {
- this.username = username;
- this.password = password;
- }
- public void prepareHeader(String token) {
- this.token = token;
- }
- @Override
- public Map<String, String> getHeaders() throws AuthFailureError {
- Map<String, String> headers = new HashMap<String, String>();
- String credentials, auth;
- if(token.isEmpty()) {
- credentials = username + ":" + password;
- auth = "Basic "
- + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
- } else {
- credentials = token;
- auth = "Bearer "
- + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
- }
- headers.put("Content-Type", "application/json");
- headers.put("Authorization", auth);
- return headers;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement