2017-09-02 111 views
0

我正在尝试请求服务器。当我通过Chrome扩展请求时,它正在工作。然而,它不能使用Volley。这是代码:Android - Volley - 请求GET使用标题不起作用

public void login(final String email, final String password) { 
    String url = BASE_URL + "login"; 
    RequestQueue queue = Volley.newRequestQueue(mContext); 
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, this, this) { 
     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      Map<String, String> headers = new HashMap<>(); 
      headers.put("email", email); 
      headers.put("password", password); 
      return headers; 
     } 
    }; 
    queue.add(stringRequest); 
} 

错误侦听器被触发而不是成功侦听器。 我错过了什么吗?

回答

0

尝试添加到这个代码

  try { 
        headers.putAll(super.getHeaders()); 
       } catch (AuthFailureError authFailureError) { 
        authFailureError.printStackTrace(); 
       } 

代码将会像下面

public void login(final String email, final String password) { 
    String url = BASE_URL + "login"; 
    RequestQueue queue = Volley.newRequestQueue(mContext); 
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, this, this) { 
     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      Map<String, String> headers = new HashMap<>(); 
      headers.put("email", email); 
      headers.put("password", password); 

       try { 
         headers.putAll(super.getHeaders()); 
        } catch (AuthFailureError authFailureError) { 
         authFailureError.printStackTrace(); 
        } 
      return headers; 
     } 
    }; 
    queue.add(stringRequest); 
} 
+0

没有工作......同样的问题... onErrorResponse被触发。 –

+0

您能否请您发布排球错误stacktrace –

+0

您能否请您发布排球错误stacktrace –