Search code examples
androidhttp-postcompatibility

HttpPost (Android ≤ 4.0) in Broadcast Receiver (AsyncTask?)


I'm building an app that is passing variables to my server through an HttpPost. Everything was working well until I updated my Android 2.3 device to Android 4.0 yesterday. After the update the app that I'm working on crashes each time the HttpPost is run. I figured that since the same code was working in Android 2.3 the issue is that it's just not compatible with the updated system.

What is the best way that I can pass the same variables and still be compatible with Android 2.3 - 4.0? What is the best practice here? I'm fairly new to mobile development. How is my methodology?

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("MY URL");
 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  nameValuePairs.add(new BasicNameValuePair("receiver", "BLAH"));
   try {
    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
   } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
   }
   try {
    httpClient.execute(httpPost);
   } catch (ClientProtocolException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }

Update

Here's my LogCat

07-06 10:41:25.385: D/DroidGap(6856): DroidGap.onCreate()
07-06 10:42:09.205: I/dalvikvm(7330): Turning on JNI app bug workarounds for target SDK version 10...
07-06 10:42:09.275: D/AndroidRuntime(7330): Shutting down VM
07-06 10:42:09.275: W/dalvikvm(7330): threadid=1: thread exiting with uncaught exception (group=0x40acd210)
07-06 10:42:09.275: E/AndroidRuntime(7330): FATAL EXCEPTION: main
07-06 10:42:09.275: E/AndroidRuntime(7330): java.lang.RuntimeException: Unable to start receiver tellatext.sms.app.SmsReceiver: android.os.NetworkOnMainThreadException
07-06 10:42:09.275: E/AndroidRuntime(7330):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2229)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at android.app.ActivityThread.access$1500(ActivityThread.java:134)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at android.os.Looper.loop(Looper.java:137)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at android.app.ActivityThread.main(ActivityThread.java:4697)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at java.lang.reflect.Method.invokeNative(Native Method)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at java.lang.reflect.Method.invoke(Method.java:511)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at dalvik.system.NativeStart.main(Native Method)
07-06 10:42:09.275: E/AndroidRuntime(7330): Caused by: android.os.NetworkOnMainThreadException
07-06 10:42:09.275: E/AndroidRuntime(7330):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1119)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at java.net.InetAddress.lookupHostByName(InetAddress.java:441)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:243)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at tellatext.sms.app.SmsReceiver.onReceive(SmsReceiver.java:114)
07-06 10:42:09.275: E/AndroidRuntime(7330):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2221)
07-06 10:42:09.275: E/AndroidRuntime(7330): ... 10 more

Solution

  • You need to run your URL request in background process.

    Here is sample code

    new TestTask().execute();
    

    and

    public class TestTask extends AsyncTask<Void, Void, Void>{
    
        @Override
        protected Void doInBackground(Void... params) {
    
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("MY URL");
             List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
              nameValuePairs.add(new BasicNameValuePair("receiver", "BLAH"));
               try {
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
               } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
               }
               try {
                httpClient.execute(httpPost);
               } catch (ClientProtocolException e) {
                e.printStackTrace();
               } catch (IOException e) {
                e.printStackTrace();
               }
            return null;
        }
    
        }