2014-07-18 88 views
0

您好我已經開發了一個應用程序來發布數據從android到網站。現在,當我運行的應用程序,我得到了消息不幸的應用程序已經停止然後封閉。從Logcat我發現它的android.os.NetworkOnMainThreadException。我搜索它,並研究這些東西來克服它http://developer.android.com/reference/android/os/AsyncTask.htmlandroid.os.NetworkOnMainThreadException從Android應用程序發佈數據到網站

我試了很多修改代碼,但得到了很多錯誤。請通過修改它來移除android.os.NetworkOnMainThreadException。 謝謝

android.os.NetworkOnMainThreadException的實際java代碼是。

package com.latlongapp; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 

import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
import android.app.Activity; 

public class MainActivity extends Activity { 

Button sendButton; 

EditText msgTextField; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // make message text field object 
    msgTextField = (EditText) findViewById(R.id.msgTextField); 
    // make send button object 
    sendButton = (Button) findViewById(R.id.sendButton); 
} 

public void send(View v) 
    { 
     // get the message from the message text box 
     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://http://tayyab001.base.pk/kami.php"); 
     try { 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
      nameValuePairs.add(new BasicNameValuePair("message", msg)); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      httpclient.execute(httppost); 
      msgTextField.setText(""); // clear text box 
     } 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(); 
     } 

    } 

} 

回答

3

例外是因爲您嘗試執行聯網操作,即在其主線程上發送數據。使用AsyncTask發送數據。

class SendTask extends AsyncTask<String, Void, String> { 
@Override 
    protected String doInBackground(String... params) { 
      // get the message from the message text box 
     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://http://tayyab001.base.pk/kami.php"); 
     try { 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
      nameValuePairs.add(new BasicNameValuePair("message", msg)); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      httpclient.execute(httppost); 
      msgTextField.setText(""); // clear text box 
     } 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(); 
     } 
    return null; 
} 

在你send()方法使用該

public void send(View v){ 
    new SendTask.execute(); 
} 
+0

謝謝你的迴應。我知道它。但我無法使用Async TAsk。我嘗試了很多修改代碼,但得到了很多錯誤。 你可以請修改此代碼使用異步任務 – user2590541

+0

檢查我編輯答案。 – Kunu

+0

非常感謝。 它適合我... – user2590541

1

您需要在HttpPost動作移動到acyncTask。

public void send(View v){ 
    String msg = msgTextField.getText().toString(); 
    new SendTask.execute(msg); 
    msgTextField.setText(""); 

} 

private class Send extends AsyncTask<URL, Integer, String> 
{ 
    protected Long doInBackground(String... txtboxTextValue) { 

    // get the message from the message text box 
    String msg = txtboxTextValue[0]; 

    // make sure the fields are not empty 
    if (msg.length()>0) 
    { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://http://tayyab001.base.pk/kami.php"); 
    try { 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
     nameValuePairs.add(new BasicNameValuePair("message", msg)); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     httpclient.execute(httppost); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } 

    } 

} 

} 

} 
相關問題