If you are using Android 1.5/Cupcake, you should definitely use
android.os.AsyncTask.

On Mon, Apr 20, 2009 at 7:44 AM, Arnaud Weber <[email protected]> wrote:
> You shall get a book or look on internet, there are some examples.
> Don't hesitate also to look in source code of open source projects. like in
> code.google. http://code.google.com/hosting/search?q=android&projectsearch=Search+projects
>
> What you're looking for is a handler.
> Look in the Handler object documentation. Handlers are the nicest way to
> send information back to your calling thread.
>
>
> Besides, here is a helper class i'm often using :
>
> package com.webyourdailyway.utils;
> import java.io.IOException;
> import org.apache.http.HttpResponse;
> import org.apache.http.client.ClientProtocolException;
> import org.apache.http.client.HttpClient;
> import org.apache.http.client.HttpResponseException;
> import org.apache.http.client.methods.HttpUriRequest;
> import org.apache.http.impl.client.BasicResponseHandler;
> import android.os.Handler;
> import android.os.Message;
> import android.util.Log;
> public class HttpRunnable implements Runnable {
>     public static final int MESSAGE_OK = 0x01;
>     public static final int MESSAGE_ERROR = 0x02;
>
>     public final HttpClient client;
>     public final HttpUriRequest request;
>     public final Handler handler;
>     public HttpRunnable(HttpClient client, HttpUriRequest request,
>             Handler handler) {
>         super();
>         this.client = client;
>         this.request = request;
>         this.handler = handler;
>     }
>     public void run() {
>         try {
>             BasicResponseHandler responseHandler = new
> BasicResponseHandler() {
>                 public String handleResponse(HttpResponse response)
>                         throws HttpResponseException, IOException {
>                     String resp = super.handleResponse(response);
>                     Message msg = new Message();
>                     msg.what=MESSAGE_OK;
>                     resp = new String(resp.getBytes(),"UTF-8");
>                     msg.obj = resp;
>                     handler.sendMessage(msg);
>                     return resp;
>                 }
>             };
>             client.execute(request, responseHandler);
>
>         } catch (ClientProtocolException e) {
>             Log.e("HTTP_TEST", "Couldn't connect to " + request.getURI(),
> e);
>             Message msg = new Message();
>             msg.what=MESSAGE_ERROR;
>             msg.obj = e.getLocalizedMessage();
>             handler.sendMessage(msg);
>         } catch (IOException e) {
>             Log.e("HTTP_TEST", "Couldn't connect to " + request.getURI(),
> e);
>             Message msg = new Message();
>             msg.what=MESSAGE_ERROR;
>             msg.obj = e.getLocalizedMessage();
>             handler.sendMessage(msg);
>         }
>     }
> }
>
> You use it like that :
>         client = new DefaultHttpClient();
>
>         String url = your url... ;
>         Log.d("Network","calling url "+url);
>         HttpUriRequest request = new HttpGet(url);
>         Handler handler = new Handler(){
>             public void handleMessage(Message msg) {
>                 super.handleMessage(msg);
>                 //Here is the piece of code executed in your main thread
> when the http has been returned.
>             }
>         };
>         HttpRunnable runnable = new HttpRunnable(client, request, handler);
>         Thread thread = new Thread(runnable);
>         thread.start();
>
> 2009/4/20 kungfuslippers <[email protected]>
>>
>> Hi,
>>
>> I understand that if you want to display UI stuff from other than the
>> main thread i.e. a separate thread  - you can use some form of Handler
>> classes post() method(s) to put a Dialog display (for example) in the
>> event queue?
>>
>> Can anyone point me to some sample code or to where the usage is
>> defined in the API docs?
>>
>> thanks
>>
>>
>>
>>
>
>
> >
>



-- 
Romain Guy
Android framework engineer
[email protected]

Note: please don't send private questions to me, as I don't have time
to provide private support.  All such questions should be posted on
public forums, where I and others can see and answer them

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" 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-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to