Boober Bunz Boober Bunz - 5 months ago 25
Android Question

POST params empty, what am I doing wrong? HttpURLConnection / Android / Java

The code below shows a method, downloadUrl(), that takes a String, "myurl," its parameter. There are only two possible urls that I ever send to it, and the behavior of the method is different for each.

when myurl = URL1, it uses a GET request and everything works fine.

when myurl = URL2, however, it uses a POST request, and the response from the php page indicates that the post parameters sent with the request were empty. You can see the line where I set the POST params, so I don't understand why it's sending no params?!

Thanks for any help!
-Adam.

private String downloadUrl(String myurl) throws IOException {

InputStream is = null;

String response = "";

try {
URL urlObject = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) urlObject.openConnection();

// find out if there's a way to incorporate these timeouts into the progress bar
// and what they mean for shitty network situations


conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setDoInput(true);

// INSERTED QUICK CHECK TO SEE WHICH URL WE ARE LOADING FROM
// it's important because one is GET, and one is POST


if (myurl.equals(url2)){

Log.i(TAG, "dlurl() in async recognizes we are doing pre-call");



conn.setRequestMethod("POST");
conn.setDoOutput(true);

OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));

String postParams = "?phone=" + phone;

writer.write(postParams);

Log.i(TAG, "we're adding " + postParams + "to " + urlObject);

writer.flush();
writer.close();
os.close();


}

else {

conn.setRequestMethod("GET");
conn.connect();
}



// Starts the query

int responseCode = conn.getResponseCode();
Log.i(TAG, "from " + myurl + ", The response code from SERVER is: " + responseCode);

is = conn.getInputStream();

// Convert the InputStream into a string
// i guess we look up how to do this





if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
response += line;
}
} else {
response = "from downloadUrl, php page response was not OK: " + responseCode;

}

// it's good to close these things?
is.close();
conn.disconnect();


Log.i(TAG, "response is " + response);

return response;

// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (is != null) {
is.close();
}
}
}

Answer

try with following code block to send parameters of the POST request.

Map<String,String> params = new LinkedHashMap<>();
params.put("phone", "phone");

StringBuilder postPraamString = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
     if (postPraamString.length() != 0) postPraamString.append('&');
         postPraamString.append(URLEncoder.encode(param.getKey(), "UTF-8"));
         postPraamString.append('=');
         postPraamString.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
     }
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
writer.write(postDataBytes);
Comments