0

I am working on android app. I need to get data from my server.I was making the server call from acitivity and got java.lang.reflect.invocationtargetexception because I was doing it on main android thread. To over come this ,I simply create a new thread and called thread.join for it to complete.I want to know is there any side effect of using such code? I tried using handler and async executor but the code became messy. Would Appreciate help. Thanks!

   Runnable runnable=new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            nearCars=getNearCars();
        }
    };
    Thread t=new Thread(runnable);
    t.start();
    try {
        t.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
jetty
  • 859
  • 2
  • 17
  • 37
  • check out the Android Volley async network library! it is very powerful! – Matt May 30 '15 at 14:15
  • Make use of libraries like OkHttp or Volley instead. – Bidhan May 30 '15 at 14:17
  • loopJ is another option which I personally like. These libraries will execut network calls on worker thread and return response on main thread. You can easily right your logic with interfaces and event management. – Karan May 30 '15 at 14:24
  • 2
    `join()` will hold your main UI thread and it's not a good way. Rather use `AsyncTask` – priyank May 30 '15 at 14:28
  • take a look at this [SO question](http://stackoverflow.com/questions/30538672/use-more-than-httpurlconnection-and-asyntask-to-send-requests-to-the-server/30539226#30539226) – TommySM May 30 '15 at 14:33

1 Answers1

2

You may use AsyncTask in Android SDK to accomplish such operations. For example:

private class GetNearCarsAsyncTask extends AsyncTask<Void, Void, NearCarReturnType> {
     protected Long doInBackground(Void... arg) 
     {
          nearCars=getNearCars();
          return nearCars;
     }

     protected void onPostExecute(NearCarReturnType result) {
         // DO something on UI Thread
     }
 }

However there are pitfalls and gotchas associated with AsyncTask, if you google something like "Problems with AsyncTask" etc. So developers now prefer using a HTTP library like loopj Async Library or Retrofit to avoid common pitfalls.

Adil
  • 3,248
  • 1
  • 22
  • 27
  • Thanks for the the reply Adil. Did not know about these libraries.But is there any drawback with this approach . Running a bit short on time. Refactoring with these libraires may take time – jetty May 30 '15 at 14:29
  • You can start with AsyncTask for now definitely. However keep in mind that you may come across lifecyle issues especially on rotation. For instance, you may want to keep an activity level instance of AsyncTask which you can cancel out when you activity ends or decide what to do on rotation if you activity is recreated on rotation. – Adil May 30 '15 at 14:33
  • Just in case you may want to read out this blog post http://blog.danlew.net/2014/06/21/the-hidden-pitfalls-of-asynctask/ – Adil May 30 '15 at 14:35