Android Authentication FAILS in AsyncHttpClient but ok with browser
I am trying to authenticate from Android (Samsung device GT-i9300 Android 4.3 (API18)) to my platform using the loopj library for AsyncHttppClient. Browser authentication is fine, after the user + navigates, I will be redirected to my welcome page.
Here's my kind of logarithm:
15760-15760/Jac.example.com.my_login I/System.out﹕ Entity content encoding: null
15760-15760/Jac.example.com.my_login I/System.out﹕ Entity content type: Content-Type: text/plain; charset=ISO-8859-1
15760-15760/Jac.example.com.my_login I/System.out﹕ Entity content: java.io.ByteArrayInputStream@427a66b8
15760-15760/Jac.example.com.my_login I/System.out﹕http://mysite.example.net:8145/
15760-16179/Jac.example.com.my_login D/AsyncHttpClient﹕ Headers were overwritten! (Content-Type | application/json) overwrites (Content-Type | text/plain; charset=ISO- 8859-1)
15760-15760/Jac.example.com.my_login V/AsyncHttpResponseHandler﹕ Progress 12 from 1 (1200%)
15760-15760/Jac.example.com.my_login W/JsonHttpResponseHandler﹕ onFailure(int, Header[], String, Throwable) was not overriden, but callback was received org.apache.http.client.HttpResponseException: Unauthorized
at com.loopj.android.http.AsyncHttpResponseHandler.sendResponseMessage(AsyncHttpResponseHandler.java:404 )**
at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:161)
at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:178)
at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:109)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
I will try to explain something:
- 15760-16179 / Jac.example.com.my_login D / AsyncHttpClient: Headers have been rewritten! (Content-Type | application / json) overwrites (Content-Type | text / plain; charset = ISO-8859-1)
I don't understand if this is a good message or a bad one. I get this because if I don't configure the Header manually it gave me: W / AsyncHttpClient: The passed contentType will be ignored as HttpEntity is setting the content type so I decided to set client.setHeader ("Content-Type", "application / json "),
- 15760-15760 / Jac.example.com.my_login W / JsonHttpResponseHandler: onFailure (int, Header [], String, Throwable) was not overridden, but org.apache.http.client.HttpResponseException: Unauthorized callback was called
I guess there is something wrong in Base64.encode, so I tried with Base64.DEFAULT and NO_WRAP but nothing changed.
if (user != null && password != null)
{
String orig = user + ":" + password;
//byte[] encoded = Base64.encode(orig.getBytes(), Base64.DEFAULT);
byte[] encoded = Base64.encode(orig.getBytes(), Base64.NO_WRAP);
String credentials = new String(encoded);
// set header
// HEADER_CONTENT_TYPE = "Content-Type";
// HEADER_APPL_JSON = "application/json";
client.setHeader(HEADER_AUTHORIZATION, HEADER_BASIC + " " +credentials);
client.setHeader(HEADER_CONTENT_TYPE, HEADER_APPL_JSON);
}
This is mylogin:
public static void login(Context context, String user, String password) throws JSONException, UnsupportedEncodingException {
JSONObject jsonParams = new JSONObject();
jsonParams.put("username", user);
jsonParams.put("password", password);
StringEntity entity = new StringEntity(jsonParams.toString());
if (user != null && password != null){
String orig = user + ":" + password;
//byte[] encoded = Base64.encode(orig.getBytes(), Base64.DEFAULT);
byte[] encoded = Base64.encode(orig.getBytes(), Base64.NO_WRAP);
String credentials = new String(encoded);
// set header
// HEADER_CONTENT_TYPE = "Content-Type";
// HEADER_APPL_JSON = "application/json";
client.setHeader(HEADER_AUTHORIZATION, HEADER_BASIC + " " +credentials);
client.setHeader(HEADER_CONTENT_TYPE, HEADER_APPL_JSON);
}
client.post(context, "oauth2/auth-token", entity, null, new JsonHttpResponseHandler()
{
/*
@Override
public void onStart(){
// called before request is started
}*/
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response){
// called when response HTTP status is "200 OK"
try {
System.out.println("Status code: "+statusCode);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
System.out.println("Status code: "+statusCode);
System.out.println("Failure - JSONObj");
}
/*
@Override
public void onRetry(int retryNo) {
// called when request is retried
System.out.println("Retry No: "+retryNo);
}*/
});
}
How am I wrong? Correct url as well as port. Thank you in advance.
source to share
No one has answered this question yet
Check out similar questions: