开发者

Android and responding to bad authentication credentials

I'm doing something开发者_StackOverflow社区 like:

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setConnectTimeout(10*1000);
            urlConnection.setReadTimeout(10*1000);
            urlConnection.setUseCaches(false);

DataInputStream dis = new DataInputStream(urlConnection.getInputStream());

to receive a JSON string at url.

This works fine when the authentication credentials I enter are correct and set with

Authenticator.setDefault(new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(loginNameString, passwordString.toCharArray());
    }
});

However, when I intentionally supply bad credentials the request of course fails but the app eventually just times out; where and how can I handle this event to notify the user and return control to her - is a there a hook?

** Edit - I placed System.out.println("hello!"); in my getPasswordAuthentication() override; this is being called over and over even after multiple failures due to bad credentials.


My way of dealing with this connection / authentification issues is by surrounding them with a :

try 
{
     //your connection attempt
}
catch (Exception e)
{
 //something goes wrong, handling the situation... for example :
     error = true;
}
if (error)
{
     //return control and notify the user, with a toast an alertDialog or whatever
}

It is ugly but it usually works... someone here surely has a better solution.

EDIT : Maybe this link will help : http://sites.google.com/site/androidhowto/password-auth-http

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜