Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For those who don't know, there are two major Http client API's available for use in Android:</p> <ul> <li>Apache HTTP Client</li> <li>java.net HttpURLConnection</li> </ul> <p>A recent overview of both of these is given in this <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html" rel="nofollow">Android Developers Blog post</a>.</p> <p>I've chosen to use the java.net implementation, because, quoting the blog: "<em>New applications should use HttpURLConnection; it is where we will be spending our energy going forward.</em>" Also, HttpURLConnection will connect using SSL automatically if the Url provided is Https. As I stated in the original post, I was looking for an interface within the API that would allow me to automatically handle authentication challenges, if necessary. I've found that the Authenticator class does what I want.</p> <p>Here is the code that I have so far, and it works: </p> <pre><code>URL Url = new URL(url); Authenticator.setDefault(new Authenticator () { protected PasswordAuthentication getPasswordAuthentication() { return (new PasswordAuthentication("username", "password".toCharArray())); } }); connection = (HttpURLConnection) Url.openConnection(); connection.setRequestProperty("Authorization", "Basic"); content = new BufferedInputStream(connection.getInputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line; while ((line = reader.readLine()) != null) { stringBuilder.append(line); } </code></pre> <p>The key (for me) to getting the Authenticator's method to actually be called, was this line:</p> <pre><code>connection.setRequestProperty("Authorization", "Basic"); </code></pre> <p>Otherwise, the request would actually return a response of 200 (OK), and redirect me to a login page.</p> <p><strong>Next Step</strong>: From the <code>getPasswordAuthentication</code> method above, prompt the user for a username and password, and somehow return it. If you can help with this, post an answer!</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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