Nella mia app Android devo chiamare un servizio web che assomiglia a questoinviare le informazioni di autenticazione con richiesta volley
http://mywesite.com/demo/mob/getmenubycategory/1
Sto usando il volley per inviare la richiesta, ma il risultato è VolleyError 401
. Ho sostituito il metodo getParams()
per aggiungere l'intestazione. ma non sta funzionando. Ecco il mio codice.
RequestQueue requestQueue = volleySingleton.getRequestQueue();
requestQueue.add(new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e(TAG, "onResponse = \n " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "response error \n" + error.networkResponse.statusCode);
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
try {
Map<String, String> map = new HashMap<String, String>();
String key = "Authorization";
String encodedString = Base64.encodeToString(String.format("%s:%s", "USERNAME", "Password").getBytes(), Base64.NO_WRAP);
String value = String.format("Basic %s", encodedString);
map.put(key, value);
return map;
} catch (Exception e) {
Log.e(TAG, "Authentication Filure");
}
return super.getParams();
}
});
Quando utilizzo il browser mostra una finestra di dialogo per inserire nome utente e password. Come posso inviare username e password con la richiesta usando volley. Qualsiasi aiuto per risolvere questo sarà apprezzato.
richiesta
Avete qualche registro errori? – Sree
no, si tratta semplicemente di registrare 401 in "onErrorResponse (Errore VolleyError)" – Sony
si prova a stampare quell'errore? – Sree