Hi all,

I am using HttpUrlConnection for network communication. It works fine for 
Android 2.3 and its lower version but on android 4.0+ devices it is not 
working.
I am making "post connection" in "AsyncTask".

After googling, I came to know  Android 4.0+ turns GET request into POST. 
We can turn off this by adding HttpUrlConnection .setDoOutPut(false).
But in my case, I made POST request but its not working, please help.
I can't use HttpPost, my need is to use HttpUrlConnection.

The piece of code is


URL connectURL = new URL(urlstring.trim());
HttpURLConnection hc = (HttpURLConnection) connectURL
.openConnection();
try {
if (requeststring != null) {
hc.setDoInput(true);
hc.setDoOutput(true);
hc.setUseCaches(true);
 hc.setRequestMethod("POST");
String basicAuth = "Basic "
+ Base64.encodeToString((username+":"+password).getBytes(),
Base64.NO_WRAP);
hc.setRequestProperty("content-type",
"application/x-www-form-urlencoded");
hc.setRequestProperty("User-Agent", "Android");
hc.setRequestProperty("Authorization", basicAuth);
hc.setRequestProperty("Connection", "close");
hc.setRequestProperty("Content-Length",
"" + requeststring.length());
// hc.setConnectTimeout(10000);
OutputStream dos = hc.getOutputStream();
try {
byte[] request_body = requeststring.getBytes();

dos.write(request_body);

dos.close();

} finally {
try {
dos.close();

} catch (IOException ioe) {

}
}
} else {
 hc.setRequestMethod("GET");
                                        hc.setDoOutput(false); 
String basicAuth = "Basic "
+ Base64.encodeToString("admin:admin".getBytes(),
Base64.NO_WRAP);
hc.setRequestProperty("content-type",
"application/x-www-form-urlencoded");
hc.setRequestProperty("User-Agent", "Android");
hc.setRequestProperty("Authorization", basicAuth);
 }

Any help is appreciated.

Thanks.
Test Test.


-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to