0

我想将文本数据上传到 Http。我有一个带有按钮和 EditText 的布局。当我单击按钮时,我在 edittext 上的文本数据将上传到我的网站。

sendButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            // 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(
                        mywebsite...);

                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));
                    HttpResponse response = 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();
            }
        }
    });

在 AndroidManifest 上

<uses-permission android:name="android.permission.INTERNET" />

但是当我运行应用程序时。我有一些问题:

05-11 20:43:04.270: E/AndroidRuntime(4294): FATAL EXCEPTION: main
05-11 20:43:04.270: E/AndroidRuntime(4294): android.os.NetworkOnMainThreadException
05-11 20:43:04.270: E/AndroidRuntime(4294):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1100)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at java.net.InetAddress.lookupHostByName(InetAddress.java:428)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:279)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at java.net.InetAddress.getAllByName(InetAddress.java:252)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at com.example.postdatatohttp.MainActivity$1.onClick(MainActivity.java:67)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at android.view.View.performClick(View.java:3528)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at android.view.View$PerformClick.run(View.java:14217)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at android.os.Handler.handleCallback(Handler.java:605)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at android.os.Looper.loop(Looper.java:137)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at android.app.ActivityThread.main(ActivityThread.java:4482)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at java.lang.reflect.Method.invokeNative(Native Method)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at java.lang.reflect.Method.invoke(Method.java:511)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
05-11 20:43:04.270: E/AndroidRuntime(4294):     at dalvik.system.NativeStart.main(Native Method)

你能指导我解决这个问题吗?

4

1 回答 1

0

尝试使用 AsyncTask 并将所有请求代码放在 doInBackground() 方法中。请不要在 UiMainTread 上加载。

于 2013-05-11T15:54:57.583 回答