Sending a post request to localhost

I need to send an HTTP request from my Android device to a web server that is running on my localhost: 8080 and hosted by Google engine.

current code:

    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 to install:

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

      

In all cases, the answer is zero, and the software force is closed. Am I sending the request correctly? can anyone guide me here?

thank

+3


source to share


2 answers


"Send request to localhost" means to send it to the local computer. In your case, this will be an Android device. You want to send a request to your desktop computer, which is the remote host. The problem is that appengine dev_sever by default only binds to the local address, so it cannot be retrieved remotely (i.e. from your Android device). You need to pass a parameter --address

to make it available externally. Check the IP address of the computer and pass it as an address. Something like:

dev_appserver.cmd --address=192.168.2.220

      



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

+7


source


The response from the httpclient.execute method should be returned in a large HttpResponse variable , so changing "String" to "HttpResponse" will be fine.



HttpResponse response = (HttpResponse) httpclient.execute(httppost,responseHandler);

      

0


source







All Articles