In addition to putting your http calls into a separate thread, you could also add HttpParams to your HttpGet object to set timeouts, both for the connection and for the operation. Something like the following:
HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 2000); // 2 seconds HttpConnectionParams.setSoTimeout(params, 15000); // 15 seconds httpget.setParams(params); If a timeout occurs during execute(), you'll get an IOException, technically a subclass of IOException depending on which timeout tripped. - dave www.androidbook.com On Sep 17, 9:39 pm, Indicator Veritatis <[email protected]> wrote: > Indeed! Why, one of the major benefits of using HttpClient instead of > barebones java.net methods is that it makes it easy to move all the > HTTP waiting -out- of the UI main thread. > > See, for example, how the authors of "Unlocking Android" have done > this in the sample code for chapter 6 > athttp://code.google.com/p/unlocking-android/ > . > > See especially the class HTTPRequestHelper in HTTPHelperForm.java in > NetworkExplorer, since this class 'wraps' HttpClient. > > Of course, to get a full explanation of how this separation works, you > need to buy and read the book;) > > NB: their approach is noticeably different from Mark Murphy's > solution. They use the Looper and Handler, which they call the "Swiss- > Army knife" of threading in Android. > > This approach may appear a little dated compared to AsyncTask, but at > least this way you learn to use the Swiss-Army knife, instead of > becoming dependent on AsyncTask. One should expect the Swiss-Army > knife to be more widely applicable than AsyncTask. > > Besides: it is a good exercise to rewrite it to use AsyncTask in place > of Looper/Handler or vice versa. > > On Sep 17, 1:04 pm, Kostya Vasilyev <[email protected]> wrote: > > > John, > > > It's not a good idea to do networking on the UI thread. As you found > > out, if there is a delay, for reasons that you can't control, the user > > gets the ANR dialog ("application not responding"). > > > Take a look at several options offered by Android to move your > > networking code to a worker thread: > > > - AsyncTask > > - IntentService > > > - An improvement upon IntentService, developed by Mark Murphy: > > WakefulIntentService > > > -- Kostya > > > 17.09.2010 23:59, john brown пишет: > > > > It does however display the error "Application MyApp (in process > > > mnt.android.myapp.MA103) is not responding." along with a Force Close > > > button and a Wait button. If I wait a moment and press the Wait > > > button, the app proceeds like it should. > > > > What can I do to avoid the long wait (and error) when there is no > > > service? > > > -- > > Kostya Vasilyev -- WiFi Manager + pretty widget > > --http://kmansoft.wordpress.com -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

