不要在Android上使用HTTURLCONNECTION通过POST发送数据

时间:2015-07-20 16:37:09

标签: php android httpurlconnection

(抱歉我的英文不好) 我有下一个代码通过POST发送数据到服务器。此代码正在另一个应用程序正常工作。但这现在不起作用。它是一个返回响应数据的函数:

BufferedReader reader = null;

    try {
        URL url = new URL(path);

        HttpURLConnection conecc = (HttpURLConnection) url.openConnection();

        conecc.setReadTimeout(5000);
        conecc.setConnectTimeout(5000);
        conecc.setDoOutput(true);
        conecc.setDoInput(true);
        conecc.setChunkedStreamingMode(0);
        conecc.connect();

        Uri.Builder builder = new Uri.Builder()
                .appendQueryParameter("name", name)
                .appendQueryParameter("birthday", bithday)
                .appendQueryParameter("sex", sex);

        String query = builder.build().getEncodedQuery();

        OutputStream outputstream = conecc.getOutputStream();

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputstream, "UTF-8"));

        writer.write(query);

        outputstream.close();

        StringBuilder sbuilder = new StringBuilder();
        reader = new BufferedReader(new InputStreamReader(conecc.getInputStream()));

        String line;
        while((line = reader.readLine()) != null) {
            sbuilder.append(line + "\n");
        }
        //writer.flush();
        //writer.close();
        return sbuilder.toString();

    } catch (Exception e) {
        e.printStackTrace();
        return  null;
    } finally {

        if (reader != null) {
            try {
                reader.close();

            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    } 

在服务器上我只有这个(PHP):

print_r($_POST)

但我得到一个空数组。然后连接到服务器工作,但不发送数据。

Array()

我在conecc.connect()之前添加了这些行;失败:

conecc.setRequestProperty("Connection", "Keep-Alive");
System.setProperty("http.keepAlive", "false");
conecc.setRequestProperty("User-Agent", "Mozilla/5.0 ( compatible ) ");
conecc.setRequestProperty("Accept", "*/*");
conecc.setRequestMethod("POST");

1 个答案:

答案 0 :(得分:0)

您是否尝试过conecc.setRequestMethod("POST");

从你班上的其他人那里,似乎没有错别字。但是要确定,检查String query = builder.build().getEncodedQuery();是否为非空结果(只记录它,或显示为Toast)。

检查this 以获取有关问题的其他帮助。

修改

检查this as well以了解解决问题的其他方法。

由于你似乎是brazzilian,如果你不能写出你想要/需要的东西,请在葡萄牙语中发帖,我们会尽力帮助你。 我会尽量简单,from a similar question

URL url = new URL("http://YOUR_URL.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conecc.setReadTimeout(10000);
conecc.setConnectTimeout(15000);
conecc.setRequestMethod("POST");
conecc.setRequestProperty("Accept-Charset", "UTF-8");
conecc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
conecc.setDoInput(true);
conecc.setDoOutput(true);

OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
    new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();

conecc.connect();

在同一个班级:

private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
StringBuilder result = new StringBuilder();
boolean first = true;

for (NameValuePair pair : params)
{
    if (first)
        first = false;
    else
        result.append("&");

    result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
    result.append("=");
    result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}

return result.toString();
}