不幸的是<application>已经停止</application>

时间:2013-04-19 11:12:36

标签: android http post

我有一个应用程序,用户以表格形式提交一些数据,然后发送到服务器。我正在平板电脑和Android智能手机(Galaxy S2)上进行测试。在平板电脑上,只要我点击“提交”,应用程序就会停止处理“遗憾已停止工作”的消息。在手机或模拟器上都看不到这个问题,这让我很难过。

应用中还有另一个屏幕,用户可以选择重新提交相同的凭据。在那里,也遇到了同样的问题。该应用程序的其余部分工作正常。这使我得出结论,问题可能在于我将数据发送到服务器的方式。该代码段如下:

//code to send to server should begin here.
        HttpClient hc = new DefaultHttpClient();
        HttpPost hp = new HttpPost("http://www.mywebsite.com/takeDetails.php");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            String val = "new";
            nameValuePairs.add(new BasicNameValuePair("mode", val));
            nameValuePairs.add(new BasicNameValuePair("name", name));
            nameValuePairs.add(new BasicNameValuePair("number", number));
            nameValuePairs.add(new BasicNameValuePair("email", emailID));
            Log.v(this.toString(), "Email = " + emailID);
            hp.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            // Execute HTTP Post Request
            HttpResponse response = hc.execute(hp);

            //Toast.makeText(getApplicationContext(), "Attempting to register.", Toast.LENGTH_LONG).show();

            String responseBody = EntityUtils.toString(response.getEntity());
            if(responseBody.contains("Success")) {
                Toast.makeText(getApplicationContext(), "Thank you for registering! You will receive an email with your username and password shortly.", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "Attempt to register failed.", Toast.LENGTH_LONG).show();
            }
            Log.v(this.toString(), "HTTP Response = " + responseBody);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }

Logcat输出:

V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): Line read = Name: jguyjfhf
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): Line read = Number: 668895898
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): Line read = Email ID:jvjhfhc@ccf.mkj
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): User details gleaned = Name = jguyjfhf
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): 668895898
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): jvjhfhc@ccf.mkj
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): Email = jvjhfhc@ccf.mkj
D/AndroidRuntime( 3766): Shutting down VM
W/dalvikvm( 3766): threadid=1: thread exiting with uncaught exception (group=0x409f11f8)
E/AndroidRuntime( 3766): FATAL EXCEPTION: main
E/AndroidRuntime( 3766): android.os.NetworkOnMainThreadException
E/AndroidRuntime( 3766):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
E/AndroidRuntime( 3766):    at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
E/AndroidRuntime( 3766):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
E/AndroidRuntime( 3766):    at java.net.InetAddress.getAllByName(InetAddress.java:220)
E/AndroidRuntime( 3766):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
E/AndroidRuntime( 3766):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
E/AndroidRuntime( 3766):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
E/AndroidRuntime( 3766):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
E/AndroidRuntime( 3766):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
E/AndroidRuntime( 3766):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
E/AndroidRuntime( 3766):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
E/AndroidRuntime( 3766):    at com.sriram.htmldisplay.htmlDisplay.writeSendDetails(htmlDisplay.java:200)
E/AndroidRuntime( 3766):    at com.sriram.htmldisplay.htmlDisplay.access$10(htmlDisplay.java:127)
E/AndroidRuntime( 3766):    at com.sriram.htmldisplay.htmlDisplay$1.onClick(htmlDisplay.java:110)
E/AndroidRuntime( 3766):    at android.view.View.performClick(View.java:3511)
E/AndroidRuntime( 3766):    at android.view.View$PerformClick.run(View.java:14105)
E/AndroidRuntime( 3766):    at android.os.Handler.handleCallback(Handler.java:605)
E/AndroidRuntime( 3766):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 3766):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 3766):    at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime( 3766):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3766):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 3766):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime( 3766):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime( 3766):    at dalvik.system.NativeStart.main(Native Method)
D/dalvikvm( 3766): GC_CONCURRENT freed 290K, 7% free 6697K/7175K, paused 4ms+6ms
W/ActivityManager( 1268):   Force finishing activity com.sriram.htmldisplay/.htmlDisplay
D/TabletStatusBar( 1340): hiding the MENU button
W/ActivityManager( 1268): Activity pause timeout for ActivityRecord{41406c60 com.sriram.htmldisplay/.htmlDisplay

我的问题:
1.有没有更好的方法来处理来自HTTPClient的错误?
2.关于可能导致平板电脑失败的任何想法都是最受欢迎的。

4 个答案:

答案 0 :(得分:3)

您正尝试在主UI线程上运行网络请求。自3.0以来,Android不允许你这样做(我相信)。这样做会导致UI锁定,直到请求完成,在请求执行期间使您的应用程序无效。

您必须在新的ThreadASyncTask中运行您的请求,才能加载UI线程。您可以找到有关如何使用多个线程here.

的更多信息

答案 1 :(得分:2)

你正在从主线程进行网络操作,这是一个非常糟糕的主意。您至少应该在AsyncTask内完成此操作。

答案 2 :(得分:1)

这个问题正在发生,你没有使用无痛线程。所有高于3.0的android os版本都不会支持任何互联网访问王的作品(http连接)在oncreate ..所以你应该使用ASYNCTask。请查看此链接以了解相关信息。

http://samir-mangroliya.blogspot.in/p/android-asynctask-example.html

http://developer.android.com/reference/android/os/AsyncTask.html

答案 3 :(得分:0)

NetworkOnMainThreadException:当应用程序尝试在其主线程上执行网络操作时引发的异常。

在onCreate

中添加此代码
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy); 

或将HTTP调用代码添加到AsyncTask

class RetreiveFeedTask extends AsyncTask<String, Void, RSSFeed> {

        private Exception exception;

        protected RSSFeed doInBackground(String... urls) {
            try {

                // add your all code here

            } catch (Exception e) {
                this.exception = e;
                return null;
            }
        }

        protected void onPostExecute(RSSFeed feed) {
            // TODO: check this.exception 
            // TODO: do something with the feed
        }
     }

执行AsyncTask:

new RetreiveFeedTask().execute(urlToRssFeed);

希望您在android清单文件中添加了以下权限

<uses-permission android:name="android.permission.INTERNET"/>