从Android应用程序发布数据到网站的android.os.NetworkOnMainThreadException

时间:2014-07-18 18:40:41

标签: java android

您好我开发了一个应用程序,用于将数据从Android发布到网站。现在,当我运行应用程序时,我收到消息不幸的是应用程序停止了然后ap关闭从Logcat我发现它的android.os.NetworkOnMainThreadException。我搜索并研究这些东西来克服它http://developer.android.com/reference/android/os/AsyncTask.html

我尝试了很多修改代码但是遇到了很多错误。请通过修改删除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();
        }

    }

}

2 个答案:

答案 0 :(得分: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;
}
发送()方法中的

使用此

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

答案 1 :(得分:1)

您需要将HttpPost操作移动到asyncTask。

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
     }

    }

}

}

}
相关问题