Hi Objectist Software Inc,

First of all, your code isn't working because "myThing" variable is
updated by a separate thread (you delegated the response handling to a
Handler) than the one that is calling getMyThing() method.

So it's pretty obvious that "myThing" variable may be null when the
method returns.

Since you are using AsyncTask to call getMyThing() which runs on a
separate thread, there is no need to for you to delegate the http
response handling on yet another thread, which in your case is using
the UI thread.

A quick fix to the problem in your situation is to perform the http
request and response in the same thread that called getMyThing().
Which in your case is the thread in AsyncTask.doInBackground().

Makas

On Mar 17, 7:36 am, Objectist Software Inc <object...@gmail.com>
wrote:
> Hi,
>
> I am trying to use a certain wrapper of HttpClient, called HTTPRequestHelper
> with an associated responseHandler as the basic class for my wrapper to a web
> service which is implemented as a singleton.
>
> HTTPRequestHelper and samples of usage are described in chapter 6 of
> "Unlocking Android" published by Manning.
>
> My problem is that invoking both the helper and the handler from a thread
> started on the main UI thread (either from an AsyncTask or directly) works
> only sporadically.
>
> Here is the basic setup.
>
> public class APIService
> {
>   private ArrayList<Something> myThing;
>
>   public ArrayList<Something> getMyThing(params)
>   {
>       getTheThing();
>       return myThing;
>   }
>
>   private void getTheThing()
>   {
>     final org.apache.http.client.ResponseHandler<String> responseHandler =
> HTTPRequestHelper
>         .getResponseHandlerInstance(this.theThingHandler);
>     // prepare params etc...
>     HTTPRequestHelper helper = new HTTPRequestHelper(responseHandler);
>     helper.performPost(...);
>   }
>
>   private final android.os.Handler theThingHandler = new Handler()
>   {
>     @Override
>     public void handleMessage(final Message msg)
>     {
>       String bundleResult = msg.getData().getString("RESPONSE");
>       myThing = parseXMLResultForMyThing(bundleResult);
>       Log.d(myThing); // --------> LOGS PROPERLY myThing!
>     }
>   };
>
> }
>
> public class MyActiviy extends Activity
> {
>   private AsyncTask<?, ?, ?> task;
>   @Override
>   public void onCreate(Bundle savedInstanceState)
>   {
>     task = new getMyThing().execute():
>   }
>
>   private class getMyThing extends AsyncTask<Void, Void, ArrayList<Something>>
>   {
>     @Override
>     protected ArrayList<Something> doInBackground(Void... params)
>     {
>       ArrayList<Something> myThing = apiService.getMyThing(other_params);
>       return myThing;
>     }
>
>     @Override
>     protected void onPostExecute(ArrayList<Something> result)
>     {
>       Log.d(result); // --------> UNFORTUNATELY THE RESULT IS SOMETIMES null
>       // process(result);
>       task = null;
>     }
>   }
>
> }
>
> So it seems to me that somehow consuming the result of doInBackground begins
> before my theThingHandler was able to update the value of myThing in the
> APIService.
>
> Any insight of why this is happening and possibly how to fix it would be very
> much appreciated.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to