1

I would like to do a HTTP post request from my virtual android device on the hostmachine. Below you'll see an image on how I post, by using the old WebFetch tool.

  1. I don't know what URL to use for calling the hostmachine?
  2. I got no idea how my body string can be used an input?

enter image description here

Does anybody have an idea on how to solve this?

Josh
  • 11,980
  • 9
  • 70
  • 116

1 Answers1

2

If you want to connect to the computer which is running the Android simulator, use the IP address 10.0.2.2. You can read more about it here.

Also check out the accepted answer in following question to see how json can be send as post data:

How to send POST request in JSON using HTTPClient?

you can use following code to make HTTP get request:

try {
        HttpClient client = new DefaultHttpClient();  
        String getURL = "http://10.0.2.2:port/your_path_with_parameter";
        HttpGet get = new HttpGet(getURL);
        HttpResponse responseGet = client.execute(get);  
        HttpEntity resEntityGet = responseGet.getEntity();  
        if (resEntityGet != null) {  
                    //do something with the response
                    Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
                }
} catch (Exception e) {
    e.printStackTrace();
}
Community
  • 1
  • 1
Praful Bhatnagar
  • 7,390
  • 2
  • 34
  • 44
  • Thanks for your response. I wasn't able to try anything, because I simply miss the experience here. The only thing I'm really sure of, is that my Webfetch is posting and provides the objects. Do you possibly have a code snippet? – user1760330 Oct 19 '12 at 20:22
  • please explain what are you trying to do? Do you need to make http get request or http post request? are you getting json in the response? – Praful Bhatnagar Oct 19 '12 at 20:25
  • My goal is to put some input parameters inside and send it to the servicebus, and receive an object containing all results. I guess it's json which I get back. – user1760330 Oct 19 '12 at 20:45
  • Can I simply put my body text, which you can see in the webfetch screenshot, in a string inclusive curly brackets? – user1760330 Oct 20 '12 at 11:13
  • please check link in my answer for making post request with json data.. posting it again.. http://stackoverflow.com/questions/6218143/android-post-json-using-http – Praful Bhatnagar Oct 20 '12 at 13:50