my application wants to check whether the user's login credentials are valid to access the web API. To do so, I have implemented this with AsyncTask method.
Here is doInBackground() implementation.
protected Boolean doInBackground(String... params) {
HttpURLConnection urlConnection = null;
URL url = null;
try {
url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setConnectTimeout(1000);
urlConnection.setReadTimeout(1000);
Authenticator.setDefault (new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication (user, password.toCharArray());
}
});
return (HttpURLConnection.HTTP_OK == urlConnection.getResponseCode());
} catch (SocketTimeoutException e) {
} catch (IOException e){
}
return false;
}
When I enter the right user credentials, it would work fine. However, when I enter the wrong credentials, it would stay hanging at urlConnection.getResponseCode() (does not throw any exception). I tried to add urlConnection.connect() in between, but I found no success. I have internet access set up in manifest.
HTTP ERROR: 401
Problem accessing /webapi/v1/hosts. Reason:
Powered by Jetty:// – Cike Eministav Oct 28 '16 at 15:23