rainai
rainai

Reputation: 468

Android 4.0 Turns POST into GET

I have a Gingerbread Android application that I'm porting to ICS. This application communicates with a web server sending HTTP POST. My application runs fine on Gingerbread. However, I have been experiencing problems after porting it to ICS. I found out that the POST requests my application is sending are actually changed to GET.

The funny thing is, Android actually reports that POST is indeed used.

URL oURL = new URL(sURL);

HttpURLConnection oHTTPConnection = (HttpURLConnection)(oURL.openConnection());
oHTTPConnection.setDoInput(true);
oHTTPConnection.setDoOutput(true);
oHTTPConnection.setRequestMethod("POST");

// set headers...
int nResponse = oHTTPConnection.getResponseCode();

String sMethod = oHTTPConnection.getRequestMethod();  // Returns "POST"

However, the server would say otherwise. I modified the web server application to check the request method it receives and then put this value in the response body it sends back to my Android application. And what I receive on my Android application is "GET".

I have tried using HttpClient with HttpPost but I get the same issue.

As I mentioned, I didn't have this problem in Gingerbread. Also, I've read from another thread here a similar (but opposite) problem that also only happens in ICS: Android 4.0 ICS turning HttpURLConnection GET requests into POST requests.

Has anyone else experienced this? Can anyone help me solve this?

Thanks in advance!

Rai

Upvotes: 2

Views: 663

Answers (2)

DSLima90
DSLima90

Reputation: 2838

Don't know if you already found a fix for this, but i was having the same problem and just found a work around. In my case it was an issue on server's side with Apache's redirection. I was doing:

Url url = new Url("http://aaaa.bbbb.com/");

Changed to:

Url url = new Url("http://aaaa.bbbb.com/index.php");

Somehow the redirection was turning my POST into a GET with no parameters.

Upvotes: 0

Pedro Loureiro
Pedro Loureiro

Reputation: 11514

Try follow this answer: https://stackoverflow.com/a/8799198/372076

I've found that pre-ICS one could get away with making a body-less POST without providing a Content-Length value, however post-ICS you must set Content-Length: 0.

Upvotes: 1

Related Questions