Question

I recently came across strange problem when sending / recieving data thru http POST request on Android.

I had difficulties with setting Fiddler to monitor the traffic between my Android app and server so I created simple web form to simulate the POST request.

<form action="http://www.my.server.org/my_script.php" method="POST">
  <input name="deviceID" type="text" width=30> Device ID <br>
  <input name="lang" type="text" width=30> Language (en / cs) <br>
  <input name="lastUpdated" type="text" width=30> Last Updated (yyyy-MM-dd hh:mm) <br>
  <button type="submit">Send</button>
</form>

When I send the request using this form, a response is delivered back with 200 OK status code and desired XML file.

I thought it would be equivalent to Java code I have in my Android app.

private static final String POST_PARAM_LAST_UPDATED = "lastUpdated";
private static final String POST_PARAM_DEVICE_ID = "deviceId";
private static final String POST_PARAM_LANG = "lang";

...

// Create a POST Header and add POST Parameters
HttpPost httpPost = new HttpPost(URL_ARTICLES);
List<NameValuePair> postParameters = new ArrayList<NameValuePair>(3);
postParameters.add(new BasicNameValuePair(POST_PARAM_DEVICE_ID, deviceId));
postParameters.add(new BasicNameValuePair(POST_PARAM_LANG, lang));
postParameters.add(new BasicNameValuePair(POST_PARAM_LAST_UPDATED, lastUpdated));
httpPost.setEntity(new UrlEncodedFormEntity(postParameters));

// Create new HttpClient and execute HTTP Post Request
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpPost);

// Get and parse the response
List<Article> parsedArticles = new ArrayList<Article>();
HttpEntity entity = response.getEntity();
if (entity != null) {
  parsedArticles = Parser.parseArticles(entity.getContent());
}

However even when I put the same parameter values (as those I put in the web form), the response in this case is 204 NO CONTENT and no XML file obviously.

Can somebody here please tell me how come these two methods are not equivalent and the responses are different? Is it something with encoding or what am I missing?

I unfortunately don't have access to the server and I'm not able to debug Android outgoing and incoming data because Fiddler and my emulator / device connected to PC refused to cooperate.

And I also wondered if I should use AndroidHttpClient instead of DefaultHttpClient but I think it's not going to change anything in this case.

Thanks in advance!

Was it helpful?

Solution

Due to Maxims comment I found out what's wrong.

It was one stupid lower case letter in the POST_PARAM_DEVICE_ID constant. It's value was "deviceId" (and should be "deviceID" as in web form).

Well, my fellow developers, pay attention when defining String keys - it's case sensitive!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top