Sending an HTTP POST request through the android emulator doesn't work

Posted by Sotirios Delimanolis on Stack Overflow See other posts from Stack Overflow or by Sotirios Delimanolis
Published on 2012-09-24T03:35:34Z Indexed on 2012/09/24 3:37 UTC
Read the original article Hit count: 157

Filed under:
|
|
|
|

I'm running a tomcat servlet on my local machine and an Android emulator with an app that makes a post request to the servlet. The code for the POST is below (without exceptions and the like):

        String strUrl = "http://10.0.2.2:8080/DeviceDiscoveryServer/server/devices/";
        Device device = Device.getUniqueInstance();

        urlParameters += URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(device.getUser(), "UTF-8");
        urlParameters += "&" + URLEncoder.encode("port", "UTF-8") + "=" + URLEncoder.encode(new Integer(Device.PORT).toString(), "UTF-8");
        urlParameters += "&" + URLEncoder.encode("address", "UTF-8") + "=" + URLEncoder.encode(device.getAddress().getHostAddress(), "UTF-8");

        URL url = new URL(strUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");

        OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
        wr.write(urlParameters);
        wr.flush();
        wr.close();

Whenever this code is executed, the servlet isn't called. However if I change the type of the request to 'GET' and don't write anything to the outputstream, the servlet gets called and everything works fine. Am I just not making the POST correctly or is there some other error?

© Stack Overflow or respective owner

Related posts about java

Related posts about android