Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to respond to HTTP authentication challenges in Android
    primarykey
    data
    text
    <p>In my app I make HTTP calls to a web service over HTTPS. My desire is to design my client in such a way that it only asks the user for their username and password when there is an authentication challenge. I want to be <em>reactive</em>, not <em>preemptive</em>. Due to the nature of my app, there are certain cases where it would not need to authenticate.</p> <p>In iOS, this process is made very easy via the <a href="http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSURLConnectionDelegate" rel="nofollow">NSURLConnectionDelegateProtocol</a>, which is implemented in the iOS version of my app. A connection is made using this class, and only when a challenge is presented does the class asks its delegate for authentication credentials.</p> <p>How can this be done in Android? Using the <a href="http://developer.android.com/reference/org/apache/http/client/AuthenticationHandler.html" rel="nofollow">AuthenticationHandler</a>? Or perhaps the <a href="http://developer.android.com/reference/java/net/Authenticator.html" rel="nofollow">Authenticator</a>? If so, could can example or tutorial be provided?</p> <p><strong>Edit 1</strong>:</p> <p>Using the Authenticator class (see my answer below) I am now able to respond to authentication challenges, but currently only with hard-coded credentials. I want to prompt the user for a username and password. According to the <a href="http://developer.android.com/reference/org/apache/http/client/AuthenticationHandler.html" rel="nofollow">Android documentation</a>, within the <code>getPasswordAuthentication</code> method you "usually prompt the user for required input".</p> <p>How is this possible? Without blocking the UI thread, which I know cannot/should-not be done, how can you display a dialog, wait for user input, then retrieve the entered credentials and return them, all within that method?</p> <p><strong>Edit 2</strong>:</p> <p>While there are little or no examples on the use of the Authenticator class, what I have been able to find suggests that prompting the user for input is impossible from within a single method, like <code>getPasswordAuthentication</code>. Might have to revert to the Apache Http client...</p> <p><strong>Final Edit</strong>:</p> <p>I am still using HttpURLConnection, but doing something along the lines of what Edward suggested below. There doesn't seem to be any configurable class for handling authentication challenges and prompting the user for input, so I am doing this manually.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload