【发布时间】:2016-05-02 14:04:07
【问题描述】:
我想使用 volley 建立带有身份验证的 http 连接。关注this answer我添加段
@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;
}
在匿名内部类 StringRequest 中,它看起来像:
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) {
}
});
但是,IDE 提示 getHeaders() 不会覆盖其超类。
为什么?我发现StringRequest扩展了类Request<String>,而后者确实有一个方法叫getHeaders()。
【问题讨论】:
-
你在扩展
Request这个类吗?例如。public class GetUser extends Request<User>
标签: java android android-studio overriding