Android Http client

Status
Not open for further replies.
Code:
try
{
    new DefaultHttpClient().execute(new HttpGet("http://www.yahoo.com/"));
}
catch (Throwable ex)
{
    Util.createDialog("", ex.getLocalizedMessage(), null);
    System.exit(1);
}

Sometimes it works, sometimes it doesn't. The error is unknown host, and the problem is usually resolved by restarting the emulator. This happens with my employer's URL, too. I don't think it's the host.

???

Oh, and sometimes the program just crashes, even though I'm catching Throwable.
 

Mile Zero

Ars Praetorian
578
Subscriptor++
Tiiba":22xqqfmj said:
Code:
try
{
    new DefaultHttpClient().execute(new HttpGet("http://www.yahoo.com/"));
}
catch (Throwable ex)
{
    Util.createDialog("", ex.getLocalizedMessage(), null);
    System.exit(1);
}

Sometimes it works, sometimes it doesn't. The error is unknown host, and the problem is usually resolved by restarting the emulator. This happens with my employer's URL, too. I don't think it's the host.

???

Oh, and sometimes the program just crashes, even though I'm catching Throwable.

I don't think you need to call System.exit(). In fact, this may be causing what looks like a crash (Ending the process behind an activity will make ActivityManager throw up and relaunch things, because that's not how Android works). You might also try tossing your error messages into the Log classes, instead of using a dialog. It's a lot easier to track them that way.

I've noticed that network access can be a little wonky in the emulator, but not quite as frequently as maybe you're seeing. Have you tried building and debugging on a device? Does the same thing happen?
 
Status
Not open for further replies.