Hello-World
Hello-World

Reputation: 9555

HttpGet on localhost connect failed

I am trying to build a android application that gets data from a REST service as JSON.

I am using a PC , windows, eclipse and a android emulator.

In this function here below. If I use a URL that is live on the internet then it works perfectly.

HttpGet httpGet = new HttpGet("http://www.cheesejedi.com/rest_services/get_big_cheese.php?puzzle=1");

but if I use a url that is hosted locally ( using visual studio ) on my own pc I get a error saying "Conection refused."

HttpGet httpGet = new HttpGet("http://" + "localhost:50851/Default.aspx");

How do I connect to my local host? Thnaks


protected String doInBackground(Void... params) {
             HttpClient httpClient = new DefaultHttpClient();
             HttpContext localContext = new BasicHttpContext();
             //HttpGet httpGet = new HttpGet("http://www.cheesejedi.com/rest_services/get_big_cheese.php?puzzle=1");
            HttpGet httpGet = new HttpGet("http://" + "localhost:50851/Default.aspx");
             //
             String text = null;
             try {
                   HttpResponse response = httpClient.execute(httpGet, localContext);
                   HttpEntity entity = response.getEntity();
                   text = getASCIIContentFromEntity(entity);
             } catch (Exception e) {
                 return e.getLocalizedMessage();
             }
             return text;
        }

Upvotes: 0

Views: 2575

Answers (3)

Luc
Luc

Reputation: 2805

In your computer, open command line, enter ipconfig in windows(linux ifconfig) you can see your IP address.

Example: 192.168.0.111

Change **localhost** by **192.168.0.111**

Localhost just in computer, It is not in your mobile

Upvotes: 1

Nathua
Nathua

Reputation: 8826

use 10.0.0.2 as localhost,

refer to here : Refer this one

Upvotes: 0

Armen
Armen

Reputation: 255

You can't use localhost when running on Device/Emulator. In this case request will be forwarded to the devices's loopback interface. Just change localhost with ip of you PC

HttpGet httpGet = new HttpGet("http://" + "your.computer.ip:50851/Default.aspx");

Upvotes: 0

Related Questions