Reputation: 10211
I want to use volley to build http connection with authentication. Following this answer I add the segment
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
String creds = String.format("%s:%s","USERNAME","PASSWORD");
String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
params.put("Authorization", auth);
return params;
}
in Anonymous Inner Class StringRequest
, and it looks like:
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
//the segment below is what I add
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
String creds = String.format("%s:%s","USERNAME","PASSWORD");
String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
params.put("Authorization", auth);
return params;
}
//the segment above is what I add
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
However, IDE hints that getHeaders()
doesn't override its superclass.
Why?I found that StringRequest extends class Request<String>
, and the latter does have a method called getHeaders()
.
Upvotes: 0
Views: 302
Reputation: 946
You are overriding public Map<String, String> getHeaders()
inside a new instance of Response.Listener<String>
instead of Request<String>
and Response.Listener<String>
does not have such method (only onResponse(String)
).
Upvotes: 2