Indeed, the onCreate method of any component, including the
Application, is the wrong place to do any network operation or other
thing that can cause delay.

In fact, you don't want to be doing it in any method that runs on the
main thread, including onStartCommand. I'd suggest using an AsyncTask.
Anything that needs to happen on the main thread after that, put in
onPostExecute().

The URIException catch block allows the code to continue while it's in
a bad state. I'd suggest instead rethrowing as an Error, e.g.:

 } catch (URIException e) {
     throw new Error(e);
 }

The ClientProtocolException should possibly be rethrowing as well, so
it can be handled by whatever means you have in place for informing
the user that your app just crashed.

The IOException is something that can happen in normal use, so it
shouldn't be regarded as a crash, and generally, should not even be
logged or printed, except in debugging. Whether it should just be
caught and ignored, or rethrown and handled elsewhere, depends on the
consequence to the user of a failed operation. It can retry, or it can
update a little status indicator light to indicate you're not
connected, or bring up a dialog box warning him, depending what this
operation means to the user.

On Nov 8, 10:56 am, loweroctave <[email protected]> wrote:
> Try putting it in onStartCommand() instead.  I haven't had any
> problems doing posts from the service, but they all happen after the
> onCreate method.
>
> On Nov 5, 8:38 pm, Burk Ferden <[email protected]> wrote:
>
>
>
>
>
>
>
> > Hello,
> > I am trying to do a http post to a php script. the method works if I
> > place it in the onCreate event of the application but gets a
> > ConnectionTimeOut error when it tries to execute it from the service.
> > Here is an example of my method
>
> > String url = "http://myurl/"; +
> >                 "?tmpl=javascript" +
> >                 "&mode=recieved" +
> >                 "&id=" + id;
> >                 URI uri = null;
> >                 try {
> >                         url = url.replace(" ", "%20");
> >                         uri = new URI(url);
> >                 } catch (URISyntaxException e1) {
> >                         // TODO Auto-generated catch block
> >                         e1.printStackTrace();
> >                 }
> >                 HttpGet httpGet = new HttpGet(uri);
> >                 HttpParams httpParameters = new BasicHttpParams();
> >                 // Set the timeout in milliseconds until a connection is
> > established.
> >                 int timeoutConnection = 10000;
> >                 HttpConnectionParams.setConnectionTimeout(httpParameters,
> > timeoutConnection);
> >                 // Set the default socket timeout (SO_TIMEOUT)
> >                 // in milliseconds which is the timeout for waiting for 
> > data.
> >                 int timeoutSocket = 10000;
> >                 HttpConnectionParams.setSoTimeout(httpParameters, 
> > timeoutSocket);
>
> >                 DefaultHttpClient httpClient = new
> > DefaultHttpClient(httpParameters);
> >                 try {
> >                         HttpResponse response = httpClient.execute(httpGet);
> >                 } catch (ClientProtocolException e) {
> >                         System.out.println(e.getLocalizedMessage());
> >                         e.printStackTrace();
> >                 } catch (IOException e) {
> >                         System.out.println(e.getLocalizedMessage());
> >                         e.printStackTrace();
> >                 }
>
> > I have no idea why the service would get a time out error but the
> > application activity would not. I am baffled.

-- 
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

Reply via email to