Broadcast Async Http json
<p>I'm trying to check the web using the timeAlarm every hour when the application is off and send a notification if necessary. It was working fine for target=android-8 and minSdkVersion="3" but get an error NetworkOnMainThreadException for above versions.
So i tryed to use the Async doinbackground function and thing goes wrong now. After few days, I give up and ask for the wise advice of the community.</p>

<pre><code>07-08 00:50:41.813: W/dalvikvm(460): threadid=7: thread exiting with uncaught exception  (group=0x4001d800)
07-08 00:50:41.823: E/AndroidRuntime(460): FATAL EXCEPTION: AsyncTask #1
07-08 00:50:41.823: E/AndroidRuntime(460): java.lang.RuntimeException: An error occured while executing doInBackground()
07-08 00:50:41.823: E/AndroidRuntime(460):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-08 00:50:41.823: E/AndroidRuntime(460):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-08 00:50:41.823: E/AndroidRuntime(460):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-08 00:50:41.823: E/AndroidRuntime(460):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-08 00:50:41.823: E/AndroidRuntime(460):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-08 00:50:41.823: E/AndroidRuntime(460):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-08 00:50:41.823: E/AndroidRuntime(460):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-08 00:50:41.823: E/AndroidRuntime(460):  at java.lang.Thread.run(Thread.java:1096)
07-08 00:50:41.823: E/AndroidRuntime(460): Caused by: java.lang.NullPointerException
07-08 00:50:41.823: E/AndroidRuntime(460):  at com.appli.ui.phone.TimeAlarm.isOnline(Unknown Source)
07-08 00:50:41.823: E/AndroidRuntime(460):  at com.appli.ui.phone.TimeAlarm.connect(Unknown Source)
07-08 00:50:41.823: E/AndroidRuntime(460):  at com.appli.ui.phone.TimeAlarm.access$0(Unknown Source)
07-08 00:50:41.823: E/AndroidRuntime(460):  at com.appli.ui.phone.TimeAlarm$Connection.doInBackground(Unknown Source)
07-08 00:50:41.823: E/AndroidRuntime(460):  at  com.appli.ui.phone.TimeAlarm$Connection.doInBackground(Unknown Source)
07-08 00:50:41.823: E/AndroidRuntime(460):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-08 00:50:41.823: E/AndroidRuntime(460):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-08 00:50:41.823: E/AndroidRuntime(460):  
... 4 more
</code></pre>

<p>TimeAlarm</p>

<pre><code>public class TimeAlarm extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {

    new Connection().execute();         
}
 private class Connection extends AsyncTask&lt;Context, Object, Object&gt; {

         @Override
         protected Context doInBackground(Context... arg0) {
             connect();
             return null;
          }
 }

 private void connect() {

     Context context = null;
     if (isOnline(context)==true) {          

        SharedPreferences preferences =    context.getSharedPreferences(context.getString(R.string.com_pack), Context.MODE_PRIVATE);
dateshared = preferences.getString("madate", "");       
HttpClient client = new DefaultHttpClient();
HttpUriRequest request = new HttpGet("http://wwww......../json");
            HttpResponse response = null;         
            try {
                response = client.execute(request);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            String jsonString = null;
            try {
                jsonString = StreamUtils.convertToString(response.getEntity().getContent());

                //DO JSON STUFF

         nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                          //NOTIFY if necessary                                                          
            }               
 }  

 }

//Test the connection
public boolean isOnline(Context context) {
ConnectivityManager cm = (ConnectivityManager)    context.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null &amp;&amp; 
cm.getActiveNetworkInfo().isConnectedOrConnecting();
    }
}
</code></pre>

<p>Manifest</p>

<pre><code>  &lt;receiver android:name=".ui.phone.TimeAlarm"  
 android:exported="true"
 android:permission="android.permission.ACCESS_NETWORK_STATE"/&gt;    
</code></pre>

<p>What a mess just to check one word. Anyway...thanks for your help.</p>
