2015-08-08 4 views
6

Sto provando a connettermi a un server django dalla mia applicazione Android. Sto cercando di accedere alla mia API, creando una richiesta POST con volly. Tutto è pronto. Tutti i parametri e le intestazioni richieste, ma ottengo ancora questo errore.com.android.volly.AuthFailureError nel rendere la richiesta POST volly di base a un server django

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

Non mi permette di accedere al mio django Api. Funziona bene con il server 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

Ho lo stesso errore con il server Php –

+0

@CodeWarrior hai risolto il problema? –

risposta

8

ho risolto la mia auto ... Questo codice ti porterà un auth-token, con il nome utente e la password che fornisci, e poi quella pedina sarà messo nell'intestazione per ottenere i dati dal server ...

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); 
} 

e ora quando si ha l'auth-token, utilizzare il seguente codice per inviare auth-token per ottenere i dati

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