【问题标题】:Why Android Studio prompt “Method doesn't override its superclass”?为什么 Android Studio 提示“方法不会覆盖其超类”?
【发布时间】: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&lt;String&gt;,而后者确实有一个方法叫getHeaders()

【问题讨论】:

  • 你在扩展Request这个类吗?例如。 public class GetUser extends Request&lt;User&gt;

标签: java android android-studio overriding


【解决方案1】:

您在 Response.Listener&lt;String&gt; 的新实例中覆盖 public Map&lt;String, String&gt; getHeaders() 而不是 Request&lt;String&gt;Response.Listener&lt;String&gt; 没有这种方法(只有 onResponse(String))。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多