2012-04-15 142 views
-1

我有以下的代碼,假設使用使用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) 

是否有任何人能幫助,告訴我什麼,我做錯了什麼,知道的事實ŧ帽子我沒有錯誤或警告,一旦我調試我的應用程序

問候

+0

你登錄貓: - )....然後張貼在這裏 – 2012-04-15 08:52:13

+0

將您貼上您的logcat – 2012-04-15 08:57:41

+0

BasicNameValuePair – sschrass 2012-04-15 09:59:31

回答

0

根據我的問題,我上面的貼,我有種發現了這個問題,並從根本上解決它,並沒有幫助,也不會發生我從我的問題中評論過的布倫德爾那裏得到的,這裏是問題和答案。

在以前的代碼中的問題,雖然它看起來很好,但它正在預先形成一些代碼,它將在主UI線程中持續5-10秒,在android的早期實際上這是確定的,但在android的最新實際中它是不。所以我說這followig代碼的主要活動:

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

這段代碼只建議在開發階段實施,但對於生產代碼,我推薦使用另一個線程是工作者線程來執行文件處理和網絡東西或任何其他繁重的處理代碼,並提供更新的主UI線程。這裏是一個很好的起點tutorile你可以檢查Android Threads, Handlers and AsyncTask - Tutorial

相關問題