0

所以,我在这里有这个代码:这真的不起作用,我的应用程序已经停止,问题是,在我在家尝试这个之前它工作得很好......现在它没有!我不知道这里出了什么问题,有人可以看看并告诉我吗?谢谢..

public class MainActivity extends Activity {

    EditText msgTextField;
    Button sendButton;

    @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 button object
            sendButton = (Button) findViewById(R.id.sendButton);
        }

        public void send(View v)
        {
            //get message from message box
            String  msg = msgTextField.getText().toString();

            //check whether the msg empty or not
            if(msg.length()>0) {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://192.168.10.28/app/app1.php");

                try {
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                       nameValuePairs.add(new BasicNameValuePair("id", "01"));
                       nameValuePairs.add(new BasicNameValuePair("message", msg));
                       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                       httpclient.execute(httppost);
                        msgTextField.setText(""); //reset the message text field
                        Toast.makeText(getBaseContext(),"Sent",Toast.LENGTH_SHORT).show();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                //display message if text field is empty
                Toast.makeText(getBaseContext(),"All fields are required",Toast.LENGTH_SHORT).show();
            }
        }

    }

这里我有 Manifes.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.form"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.form.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
4

2 回答 2

0

每个阻塞调用都必须在不同于UI Thread. 您正在获取NetworkOnMainThreadException. 使用 aThreadAsyncTask. 在这里阅读

于 2013-05-14T08:58:15.733 回答
0

你正在做一些不能从 UI 线程调用的东西。您可以创建一个 AsyncTask 来执行此类操作。有关更多信息,请参阅此内容。

于 2013-05-14T09:00:01.770 回答