-1

我正在编写的应用程序有问题,该应用程序可在三星 S5300(Android OS 2.3.3)上运行,但不能在我的平板电脑(Prestigio Multipad 2 Ultra Duo 8.0 3G)上运行。它正在运行(Android OS 4.1)

该应用程序从 IOIO 板上获取一些信息,例如时间戳和 ADC 值,并将其发布到服务器。这适用于三星手机。使用平板电脑,它会在发送数据部分时大放异彩。

这是我的代码片段:

        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

我得到的 logcat 错误如下:

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

我相信新操作系统的权限甚至可能是一些安全问题?我已经在清单中设置了适当的互联网权限。

任何意见,将不胜感激。问候

4

1 回答 1

2

您正在尝试在主 UI 线程上访问 Internet。任何类型的网络访问都可能被阻止,这将使您的 UI 无响应。Android 现在强制执行网络操作不在主 UI 线程上运行的策略。

使用 AsyncTask、IntentService、Java 线程构造或其他方式从主线程中删除该工作。

于 2013-08-13T17:42:11.410 回答