4

I need to send a http post request from my android device to a web server that is running on my localhost:8080 and is being hosted by the google app engine.

the current code is this:

    try {

                HttpPost httppost = new HttpPost("http://192.168.2.220:8080");
                httppost.setHeader("Content-type", "application/json");
                ResponseHandler <String> responseHandler = new BasicResponseHandler();
                StringEntity se = new StringEntity(object.toString()); 
           se.setContentEncoding(newBasicHeader(HTTP.CONTENT_TYPE,"application/json"));
                httppost.setEntity(se);
                System.out.println("Request Sent");
                String response = httpclient.execute(httppost, responseHandler);

                String temp = EntityUtils.toString(response.getEntity());



            } catch (ClientProtocolException e) {}
            catch (IOException e) {
            }
        }

I also tried setting:

    HttpPost httppost = new HttpPost("http://10.0.2.2:8080");

In all cases, the response is null and the program force closes. am i sending the request correctly? can anyone please guide me here?

Thanks

Goofy
  • 6,098
  • 17
  • 90
  • 156
Ajay
  • 1,796
  • 3
  • 18
  • 22

2 Answers2

7

'Send a request to localhost' means to send it to the local machine. In your case that would be the Android device. You want to send the request to your desktop machine, which is a remote host. The problem is that the Appengine dev_sever by default only binds to the local address, so it can't be accessed remotely (i.e., from your Android device). You need to pass the --address option to make accessible from the outside. Check your computer's IP and pass it as the address. Something like:

dev_appserver.cmd --address=192.168.2.220

Details here: http://code.google.com/appengine/docs/java/tools/devserver.html

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
0

The response of the httpclient.execute method needs to be return on a big HttpResponse variable, so changing "String" to "HttpResponse" will be ok.

HttpResponse response = (HttpResponse) httpclient.execute(httppost,responseHandler);
francordie
  • 608
  • 5
  • 11