-1

I have a problem with an app that I am writing which works on the Samsung S5300 (Android OS 2.3.3) but not on my tablet (Prestigio Multipad 2 Ultra Duo 8.0 3G). It is running (Android OS 4.1)

The app gets some info, like a timestamp and ADC values from a IOIO Board, and Posts it to a server. This works great with the samsung phone. With the tablet it bombs out on the sending of data part.

Here is my code snippet that bombs out:

        public void postData() 
    {
        String urlS = "https://posttestserver.com/post.php?dump&html&dir=oan";
        //String urlS =  "https://placidblue.no-ip.biz/cgi-bin/data_input.py";
        //String urlS =  "http://placidblue.no-ip.biz/cgi-bin/data_input.py";

        HttpClient httpclient = new DefaultHttpClient(); // Create a new HttpClient and Post Header
        HttpPost httppost = new HttpPost(urlS);

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("LAT", TransferLat));
            nameValuePairs.add(new BasicNameValuePair("LON", TransferLon));
            nameValuePairs.add(new BasicNameValuePair("TIM", TransferTim));
            nameValuePairs.add(new BasicNameValuePair("POL", TransferPol));
            nameValuePairs.add(new BasicNameValuePair("CON", TransferCon));
            try {
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            } catch (UnsupportedEncodingException e) {
                Log.d("General","UnsupportedEncodingException.xxx");
                e.printStackTrace();// writing error to Log
            }

            Log.d("General Log","Sending_data.xxx");
            HttpResponse response = httpclient.execute(httppost); // Execute HTTP Post Request
            Log.d("HttpResponse:", response.toString()+"xxx"); // writing response to log
            Log.d("General","Data_send_complete.xxx");

        }catch (IllegalStateException e) {
            Log.d("General","Illegal_State1.xxx");
            e.printStackTrace();
            Log.d("General","Illegal_State2.xxx");
        }
        catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // writing error to Log
            e.printStackTrace();
        }
    }//end postData

My logcat error that I get is as follow:

08-13 19:02:50.797: D/General Log(6672): Sending_data.xxx
08-13 19:02:50.816: D/AndroidRuntime(6672): Shutting down VM
08-13 19:02:50.816: W/dalvikvm(6672): threadid=1: thread exiting with uncaught exception (group=0x40dce908)
08-13 19:02:50.824: E/AndroidRuntime(6672): FATAL EXCEPTION: main
08-13 19:02:50.824: E/AndroidRuntime(6672): java.lang.IllegalStateException: Could not execute method of the activity
08-13 19:02:50.824: E/AndroidRuntime(6672):     at android.view.View$1.onClick(View.java:3598)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at android.view.View.performClick(View.java:4091)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at android.view.View$PerformClick.run(View.java:17072)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at android.os.Handler.handleCallback(Handler.java:615)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at android.os.Looper.loop(Looper.java:153)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at android.app.ActivityThread.main(ActivityThread.java:4987)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at java.lang.reflect.Method.invokeNative(Native Method)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at java.lang.reflect.Method.invoke(Method.java:511)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at dalvik.system.NativeStart.main(Native Method)
08-13 19:02:50.824: E/AndroidRuntime(6672): Caused by: java.lang.reflect.InvocationTargetException
08-13 19:02:50.824: E/AndroidRuntime(6672):     at java.lang.reflect.Method.invokeNative(Native Method)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at java.lang.reflect.Method.invoke(Method.java:511)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at android.view.View$1.onClick(View.java:3593)
08-13 19:02:50.824: E/AndroidRuntime(6672):     ... 11 more
08-13 19:02:50.824: E/AndroidRuntime(6672): Caused by: android.os.NetworkOnMainThreadException
08-13 19:02:50.824: E/AndroidRuntime(6672):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1125)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at com.example.airsensor1.MainActivity.postData(MainActivity.java:194)
08-13 19:02:50.824: E/AndroidRuntime(6672):     at com.example.airsensor1.MainActivity.sendData(MainActivity.java:158)
08-13 19:02:50.824: E/AndroidRuntime(6672):     ... 14 more

I beleive that it might even be some security problem with permissions on the new OS? I have set the appropriate internet permisions in the manifest.

Any advice would be appreciated. Regards

Ghost
  • 11
  • 2
  • 1
    Google for `NetworkOnMainThreadException`. – nhaarman Aug 13 '13 at 17:40
  • **Always** check for the `Caused by` part (and line number). No need to guess. Computers are smart. – keyser Aug 13 '13 at 17:42
  • please see this answer: [http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception][1] [1]: http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – kritzikratzi Aug 13 '13 at 17:42
  • http://stackoverflow.com/questions/9413625/android-android-os-networkonmainthreadexception – rocketboy Aug 13 '13 at 17:42
  • @Keyser Particularly OP should Google (or Bing) the last `caused by` part. In this case "android.os.NetworkOnMainThreadException" and if that doesn't help, try the next line "android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork" – Lee Meador Aug 13 '13 at 17:43

1 Answers1

2

You are trying to access the internet while on the main UI thread. Any type of network access can block, which will make your UI unresponsive. Android now enforces a policy that network operations not be run on the main UI thread.

Use AsyncTask, IntentService, Java threading constructs, or some other way to remove that work from the main thread.

Karakuri
  • 38,365
  • 12
  • 84
  • 104