得到不幸的应用程序已停止错误

时间:2012-04-15 08:29:25

标签: android networking send

我有以下代码,假设用于使用Android应用程序向服务器发送数据,但我一直收到错误状态"不幸的是我的应用程序名称已被停止"

我的活动代码是:

public class SendDataToServerActivity extends Activity {
    /** Called when the activity is first created. */
    Button sendButton;
    EditText msgTextField;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // make message text field object
        msgTextField = (EditText) findViewById(R.id.msgTextField);
        // make send button object
        sendButton = (Button) findViewById(R.id.sendButton);
        sendButton.setOnClickListener(new Button.OnClickListener(){
            //perform your action here            
            public void onClick(View v){
                String msg = msgTextField.getText().toString();  

                // make sure the fields are not empty
                if (msg.length()>0)
                {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://98.131.137.4/husam.php");
                 try {
                   List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                   nameValuePairs.add(new BasicNameValuePair("id", "12345"));
                   nameValuePairs.add(new BasicNameValuePair("message", msg));
                   httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                   httpclient.execute(httppost);
                   msgTextField.setText("");
                 } catch (ClientProtocolException e) {
                     // TODO Auto-generated catch block
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                 }
                }
                else
                {
                    // display message if text fields are empty
                    Toast.makeText(getBaseContext(),"All field are   required",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

我的日志如下:

04-19 01:17:02.168: E/AndroidRuntime(583): FATAL EXCEPTION: main
04-19 01:17:02.168: E/AndroidRuntime(583): android.os.NetworkOnMainThreadException
04-19 01:17:02.168: E/AndroidRuntime(583):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
04-19 01:17:02.168: E/AndroidRuntime(583):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
04-19 01:17:02.168: E/AndroidRuntime(583):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
04-19 01:17:02.168: E/AndroidRuntime(583):  at libcore.io.IoBridge.connect(IoBridge.java:112)
04-19 01:17:02.168: E/AndroidRuntime(583):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-19 01:17:02.168: E/AndroidRuntime(583):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
04-19 01:17:02.168: E/AndroidRuntime(583):  at java.net.Socket.connect(Socket.java:842)
04-19 01:17:02.168: E/AndroidRuntime(583):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
04-19 01:17:02.168: E/AndroidRuntime(583):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
04-19 01:17:02.168: E/AndroidRuntime(583):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-19 01:17:02.168: E/AndroidRuntime(583):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-19 01:17:02.168: E/AndroidRuntime(583):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-19 01:17:02.168: E/AndroidRuntime(583):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-19 01:17:02.168: E/AndroidRuntime(583):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-19 01:17:02.168: E/AndroidRuntime(583):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-19 01:17:02.168: E/AndroidRuntime(583):  at com.husamalahmadi.send.SendDataToServerActivity$1.onClick(SendDataToServerActivity.java:55)
04-19 01:17:02.168: E/AndroidRuntime(583):  at android.view.View.performClick(View.java:3511)
04-19 01:17:02.168: E/AndroidRuntime(583):  at android.view.View$PerformClick.run(View.java:14105)
04-19 01:17:02.168: E/AndroidRuntime(583):  at android.os.Handler.handleCallback(Handler.java:605)

有没有人能帮忙告诉我这里做错了什么,知道我调试应用程序后没有错误或警告

问候

1 个答案:

答案 0 :(得分:0)

根据我在上面发布的问题,我发现了问题,并从根本上解决了问题,如果没有我从Blundell那里得到的帮助,我就不会发生这种情况,我在问题中发表了评论,这就是问题和答案

以前的代码中的问题虽然它看起来很好,但它是预先形成一些代码,它们将在主UI线程中持续5-10秒,在android的早期版本中这是可以的但是在Android的最新版本中它不是。所以我将这个关注代码添加到主要活动中:

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()  
                    .detectDiskReads()  
                    .detectDiskWrites()  
                    .detectNetwork()   
                                    .problems  
                    .penaltyLog()  
                    .build());  
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()  
                    .detectLeakedSqlLiteObjects()  
                    .detectLeakedClosableObjects()  
                    .penaltyLog()  
                    .penaltyDeath()  
                    .build());  

此代码仅建议在开发阶段实现,但对于生产代码,我建议使用另一个线程,即工作线程来执行文件处理和网络设备或任何其他繁重的处理代码,并为主UI线程提供更新。这是一个很好的入门教程,你可以查看Android Threads, Handlers and AsyncTask - Tutorial