0

我尝试创建一个程序,允许用户使用以下代码将 HTTP POST 发送到 PHP 脚本。当我单击发送按钮时,我得到一个 android.os.NetworkOnMainThreadException。(btn发送)。

package com.naveed.post;

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

import org.apache.http.HttpResponse;
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.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

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

        // Creating HTTP client
        final HttpClient httpClient = new DefaultHttpClient();
        // Creating HTTP Post
        final HttpPost httpPost = new HttpPost("http://10.0.2.2/android_post/test.php");
        Button send = (Button)findViewById(R.id.btnSend);
        send.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                EditText message = (EditText)findViewById(R.id.textMessage);

                String msg = message.getText().toString();

                   // Building post parameters
                // key and value pair
                List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
                nameValuePair.add(new BasicNameValuePair("message", msg));

                // Url Encoding the POST parameters
                try {
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
                } catch (UnsupportedEncodingException e) {
                    // writing error to Log
                    e.printStackTrace();
                }

                // Making HTTP Request
                try {
                    HttpResponse response = httpClient.execute(httpPost);

                    // writing response to log
                    Log.d("Http Response:", response.toString());
                } catch (ClientProtocolException e) {
                    // writing exception to log
                    e.printStackTrace();
                } catch (IOException e) {
                    // writing exception to log
                    e.printStackTrace();

                }
            }
        });

    }
}

我知道我必须使用 AsyncTask,所以我尝试了。但我知道我做错了。有什么建议么。我的代码如下:

package com.naveed.post;

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

import org.apache.http.HttpResponse;
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.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {


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


        Button send = (Button)findViewById(R.id.btnSend);
        send.setOnClickListener(new View.OnClickListener() {

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

                 ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
                 EditText message = (EditText)findViewById(R.id.textMessage);

                    String msg = message.getText().toString();

                    nameValuePair.add(new BasicNameValuePair("message", msg));


            }
        });

    }

   class doThePost extends AsyncTask<Void, Void, Void>{

        protected Void doInBackground(ArrayList<NameValuePair>... nameValuePair) {
            // TODO Auto-generated method stub
            ArrayList<NameValuePair> nvPairs = nameValuePair[0];
             HttpClient httpClient = new DefaultHttpClient();
                // Creating HTTP Post
             HttpPost httpPost = new HttpPost("http://10.0.2.2/android_post/test.php");
                // Url Encoding the POST parameters
                try {
                    httpPost.setEntity(new UrlEncodedFormEntity(nvPairs));
                } catch (UnsupportedEncodingException e) {
                    // writing error to Log
                    e.printStackTrace();
                }

                // Making HTTP Request
                try {
                    HttpResponse response = httpClient.execute(httpPost);

                    // writing response to log
                    Log.d("hello", "done");
                    Log.d("Http Response:", response.toString());
                } catch (ClientProtocolException e) {
                    // writing exception to log
                    e.printStackTrace();
                } catch (IOException e) {
                    // writing exception to log
                    e.printStackTrace();

                }
                Log.d("hello", "done");
            return null;
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            return null;
        }




        }

}
4

2 回答 2

0

将您的 doInBackground 方法的代码移动到受保护的 Void doInBackground(Void... params)方法,它将起作用。你必须使用 AsyncTask 类。这是AsyncTask类的链接。它还具有如何使用示例代码。

于 2013-10-05T16:53:12.397 回答
0

Android 查杀运行 5 秒以上的进程。您必须使用 AsyncTask。

于 2013-10-05T13:45:50.503 回答