2

(sorry for my bad english) I have the next code to send data via POST to Server. This code is working in another application correctly. But this does not work now. It's a function that return the response data:

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;
            }
        }
    } 

On the server i just have this (PHP):

print_r($_POST)

But i get an empty array. Then connection to server works but data is not sent.

Array()

I have added these lines before conecc.connect(); unsuccessfully:

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 Answers1

0

Have you tried the conecc.setRequestMethod("POST"); ?

From the rest of your class, there seems to be no typos. But to be sure, check that String query = builder.build().getEncodedQuery(); yelds non-null results (just log it, or show as a Toast).

Check this for additional aid in your problem.

EDIT

Check this as well for additional ways to fix your issue.

Since you seem to be brazzilian, post in portuguese in the comments if you cannot writte what you want/need, and we will try to help with that. I will try to put it simple, 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();

And in that same class:

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();
}
Community
  • 1
  • 1
Bonatti
  • 2,732
  • 5
  • 23
  • 38
  • I logged it: name=PAPANTLA&birthday=1967-07-30&sexo=M – Marco Antonio Jul 20 '15 at 17:39
  • http://stackoverflow.com/questions/9767952/how-to-add-parameters-to-httpurlconnection-using-post seems like a duplicate. But seeing that's older and more relevant, it looks like your answer should've just pointed to the original answer. – kevinl Oct 27 '15 at 19:56