Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android HttpUrlConnection does POST instead of GET

I've got an Android app that's trying to do a GET request to my server using HttpUrlConnection. When I test the code in a separate testing desktop application, everything works fine. However, when I run it on my android device, my server registers a POST request instead of a GET.

Here's the code for my get method:

public static String get(String url) throws IOException {
    HttpURLConnection conn = connFromUrlString(url);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.connect();

    String out = IOUtils.toString(conn.getInputStream(), "UTF-8");
    conn.disconnect();
    return out;
}
like image 443
joshlf Avatar asked Dec 20 '22 19:12

joshlf


1 Answers

This line is the culprit.

conn.setDoOutput(true);

Remove that and give it a try.

By the way, you should read this excellent piece: https://stackoverflow.com/a/2793153/415412

like image 53
Suchintya Avatar answered Dec 27 '22 07:12

Suchintya