2015-08-08 121 views
6

我試圖從我的android應用程序連接到django服務器。我試圖訪問我的api,使用volly發出POST請求。一切都設定好了。所有的參數和頭文件都需要,但是我仍然得到這個錯誤。com.android.volly.AuthFailureError在向django服務器發出基本的volly POST請求

log: [490] BasicNetwork.performRequest: Unexpected response code 401 for https://example.com/ 

它不讓我訪問我的Django Api。它適用於PHP服務器。

public void vollyRequest() 
    { 
     RequestQueue queue = Volley.newRequestQueue(this); 
     StringRequest request = new StringRequest(Request.Method.POST , "https://example.com/", new Response.Listener<String>() { 

      @Override 
      public void onResponse(String response) { 
       Toast.makeText(MainActivity.this, "RESPONSE: " + response, Toast.LENGTH_SHORT).show(); 
      } 

     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Toast.makeText(MainActivity.this, "ERROR: " + error, Toast.LENGTH_SHORT).show(); 
      } 
     }) { 

      @Override 
      protected Map<String,String> getParams(){ 
       Map<String,String> params = new HashMap<String, String>(); 
       params.put("username","***"); 
       params.put("password","***"); 
       return params; 
       } 
//   @Override 
//   public Map<String, String> getHeaders() throws AuthFailureError { 
//   Map<String,String> params = new HashMap<String, String>(); 
//   params.put("Content-Type","application/x-www-form-urlencoded"); 
//   return params; 
//   } 
     }; 
     queue.add(request); 
    } 
+0

我與Php服務器有同樣的錯誤 –

+0

@CodeWarrior你解決了這個問題嗎? –

回答

8

我解決了這個問題我自己... 這個代碼將讓你的身份驗證令牌,與您提供的用戶名和密碼,然後該令牌將被放置在標題中得到數據服務器...

public void vollyRequestGetAuthToken() 
{ 
    RequestQueue queue = Volley.newRequestQueue(this); 
    StringRequest request = new StringRequest(Request.Method.POST , "https://example.com/get-auth-token/", new Response.Listener<String>() { 

     @Override 

    public void onResponse(String response) { 
     Toast.makeText(MainActivity.this, "RESPONSE: " + response, Toast.LENGTH_SHORT).show(); 
     System.out.println("RESPONSE:   >>> " + response + "<<<"); 
    } 
}, new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 
     Toast.makeText(MainActivity.this, "ERROR: " + error, Toast.LENGTH_SHORT).show(); 
    } 
}) { 
    @Override 
    protected Map<String,String> getParams(){ 
     Map<String,String> params = new HashMap<String, String>(); 
     params.put("username","*****"); 
     params.put("password","*****"); 
     return params; 
    } 
    @Override 
    public Map<String, String> getHeaders() throws AuthFailureError { 
     Map<String,String> params = new HashMap<String, String>(); 
     params.put("Authorization", 
       String.format("Basic %s", Base64.encodeToString(
         String.format("%s:%s", "<username>", "<password>").getBytes(), Base64.DEFAULT))); 
     params.put("username" , "*****"); 
     params.put("password" , "*****"); 
     return params; 
    } 
}; 
    queue.add(request); 
} 

,現在當你有身份驗證令牌,使用下面的代碼來發送身份驗證令牌來獲得數據

@Override 
      public Map<String, String> getHeaders() throws AuthFailureError { 
       HashMap<String, String> headers = new HashMap<String, String>(); 
       headers.put("Authorization", "Token <token>"); 
       return headers; 
      } 
相關問題