i have an android application that makes an HTTP post request to a server containing some NameValuePairs, and It works just fine over any wifi network, but when i use the same http post over 3g, the server gets a http request with an empty body. Here is the code for the request
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
3);
nameValuePairs.add(new BasicNameValuePair("Name", params[0]));
nameValuePairs.add(new BasicNameValuePair("DNI", params[1]));
nameValuePairs.add(new BasicNameValuePair("Token", params[2]));
URL url = new URL(URL_SERVER);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setReadTimeout(30000);
conn.setConnectTimeout(50000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(nameValuePairs));
writer.flush();
writer.close();
os.close();
conn.connect();
int responseCode = conn.getResponseCode();
BufferedReader in;
if (responseCode == 404)
in = new BufferedReader(new InputStreamReader(
conn.getErrorStream()));
else
in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
Here is the code for the getQuery method
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();
}
Any idea why this happens?
I have new info. I made a form to send the http post over the web browser. that form works great and sends perfectly the body over 3g on Windows phone and over wifi. But when I try to use the chrome of the android phone over 3g to send the http post, it arrives empty, and also if I try to send the http post from one computer connected to a hotspot of my android phone it fails. When i try with the same computer connected to a wifi network, no problem at all. This is so weird. Ideas?