0

I have a problem with the Android http url connection. I have this method:

private String getHtml(String urlString) throws Exception {

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);

URL url = new URL(urlString);
URLConnection uc = url.openConnection();
uc.setRequestProperty("Authorization", "Basic " + password);
InputStream content = uc.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = in.readLine()) != null) {
    pw.println(line);
} 
return sw.toString();
}

When I use it from a normal java application it works just fine. But when I run it from within an Android application it doesn't work.

I have observed that the url connection is an org.apache...HttpURLConnection when run in Android. And when it uses this it doesn't enter the while-loop.

In the normal java application it uses a sun.net...HttpURLConnection (which works).

Anyone have any suggestions to how I can get this to work on Android?

user1066441
  • 1
  • 1
  • 2

1 Answers1

-1

You could try this solution:

        try 
        {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(url);
            HttpResponse response = client.execute(request);

            InputStream in = response.getEntity().getContent();
        } 
        catch (Exception e) 
        {
            // Code
        }

Can't remember the correct Exception at the moment, but I think you could find it.

iCantSeeSharp
  • 3,880
  • 4
  • 42
  • 65
  • That might work in other cases. But I need to authenticate using Http basic. That is why I use URLConnection. But thank you for the reply – user1066441 Nov 26 '11 at 02:30
  • Ah, ok. There are some resources, you could check [here](http://hc.apache.org/httpcomponents-client-ga/examples.html). – iCantSeeSharp Nov 26 '11 at 02:36