HTTPClient不发送HttpPost请求

时间:2014-09-26 14:23:09

标签: java android httpclient

我的Android HTTPClient确实存在问题。

我想将POST数据发送到网站并在此之后解析内容。 我的问题是,POST数据没有发送到网络服务器。 我不知道为什么。

这是我的HTTP Util Class

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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 android.os.AsyncTask;

public class HTTPHelperUtil {

    private static HTTPHelperUtil instance;

    private String url;

    private List<NameValuePair> nameValuePairs;

    private String result;

    private HTTPHelperUtil() {
        // nothing to do
    }

    public synchronized static HTTPHelperUtil getInstance() {
        if (HTTPHelperUtil.instance == null) {
            HTTPHelperUtil.instance = new HTTPHelperUtil();
        }
        return HTTPHelperUtil.instance;
    }

    public HTTPHelperUtil init(String url, List<NameValuePair> nameValuePairs) {
        this.url = url;
        this.nameValuePairs = nameValuePairs;
        return HTTPHelperUtil.instance;
    }

    public HTTPHelperUtil start() throws ClientProtocolException, IOException {
        SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
        AsyncTask<List<NameValuePair>, Void, String> execute = sendPostReqAsyncTask.execute(nameValuePairs);
        return HTTPHelperUtil.instance;
    }

    class SendPostReqAsyncTask extends AsyncTask<List<NameValuePair>, Void, String> {

        @Override
        protected String doInBackground(List<NameValuePair>... params) {
            String res = "";
            List<NameValuePair> postPairs = params[0];
            System.out.println(postPairs.get(0));
            System.out.println(postPairs.get(1));
            if (postPairs != null && !postPairs.isEmpty()) {
                // Create a new HttpClient and Post Header
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);
                try {
                    httppost.setEntity(new UrlEncodedFormEntity(postPairs));
                    HttpResponse response;
                    response = httpclient.execute(httppost);

                    if (response != null && response.getEntity() != null) {
                        InputStream content = response.getEntity().getContent();
                        if (content != null) {
                            BufferedReader reader = new BufferedReader(
                                    new InputStreamReader(content, "UTF-8"));
                            while (true) {
                                String addingSource = reader.readLine();
                                if (addingSource != null) {
                                    res = addingSource;
                                    break;
                                }
                            }
                        }
                    }
                } catch (IllegalStateException e) {
                    return "-";
                } catch (IOException e) {
                    return "-";
                }
            }
            System.out.println(res);
            return res;
        }
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            HTTPHelperUtil.this.result = result;
        }
    }

    public String getResult() {
        System.out.println("Result = " + result);
        return result;
    }
}

来自我的活动的电话:

        final String url = "http://example.com/app/mysite.php";

        List<NameValuePair> postParams = new ArrayList<NameValuePair>(2);
        postParams.add(new BasicNameValuePair("username", username));
        postParams.add(new BasicNameValuePair("password", password));
        HTTPHelperUtil.getInstance().init(url, postParams);
        String result = "";
        try {
            HTTPHelperUtil.getInstance().start();
            result = HTTPHelperUtil.getInstance().getResult();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

我的测试页面没有通过POST获取用户名和密码。

有没有人在我的代码中看到错误?

由于

1 个答案:

答案 0 :(得分:0)

根据您的代码:

列出postParams = new ArrayList(2);

这里你在构造函数中传递2,在声明具有名称值对的列表时不需要。 这可能是因为休息所有代码似乎都很好。