Httphostconnectionexception - Android

Asked

Viewed 238 times

1

I did in the Visual Studio Community 2013 a simple Web API to register a person and receive a person, and I’m trying to consume this service with an android app, I’m emulating the application by emulator Genymotion.

When he does the HttpResponse response = httpClient.execute(method); he brings me an exception HttpHostConnectionException saying that the connection to localhost was refused.

The code snippet is all ok, but if you want:

        protected String doInBackground(String... params)
        {
            HttpClient httpClient = new DefaultHttpClient();
            HttpUriRequest method = null;
            Gson gson = new Gson();

            try {
                if (metodo.toUpperCase() == "POST")
                {
                    HttpPost post = new HttpPost(url);
                    post.setEntity(new ByteArrayEntity(gson.toJson(object).getBytes()));

                    method = post;
                    method.setHeader("Content-Type", "application/json");
                }

                method.setHeader("Accept", "application/json");
                method.setHeader("Accept-Charset", "utf-8");

                HttpResponse response = httpClient.execute(method);

                status = response.getStatusLine().getStatusCode();

                return EntityUtils.toString(response.getEntity());
            }

I already went through the paperwork and checked the part Emulator Networking, and even changing from localhost for 10.0.2.2 it still doesn’t work, but when I change to 10.0.2.2 he brings me that the connection exceeded the time limit and not that it was refused.

How to proceed?

  • Have you tested directly from your device? It may be a problem with the emulator. Another thing: if this 10.0.2.2 is the IP of your machine where the server is running?

  • to running the server on my local machine, and using genymotion as emulator for android

  • 10.0.2.2 is the IP of your machine on the local network?

  • No, but this is the address that an android emulator recognizes as localhost according to the documentation Emulator Networking

  • You need to put the IP’s your machine on the local network where the server is running. It looks like you are trying to access the emulator’s localhost.

  • the server is the localhost

  • The difference is that the localhost of emulator is different from your host localhost which is server. That’s why you have to access her IP on the network and not localhost.

  • That’s what the 10.0.0.2 but it still doesn’t work.

  • Access by android browser that same address and see what the result.

  • neither with my ip nor with the 10.0.0.2 it works

  • Then you better check that the server is running correctly on your machine. You have already tested on a real device?

  • Enzo, searching for access to a device’s localhost, staff generally recommend using ip 10.0.3.2.

  • I’ve tried this one too, but it’s also not possible

  • Testing outside of Genymotion, the Web Service is accessible?

  • yes, functioning normally

Show 11 more comments

2 answers

1


If you are referring to a localhost on your device use the http://10.0.2.2/ instead of the http://127.0.0.1/ or http://localhost / .

Because your Android emulator is running on a virtual machine (QEMU) and you cannot connect to a server that runs directly on your PC.

Therefore, your url should look like this:

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

Another thing, check if in your xml there is this permission:

<uses-permission android:name="android.permission.INTERNET" />

1

Try opening the api from the genymotion browser using the computer’s network ip index (something like 192.168.x. x)

  • I did it and also nothing, I used the IP of VB, 192.168.56.1 and still nothing

  • the api can be accessed from other network computer...?

  • I didn’t do this test until because I just want the emulator to access it, but it’s working perfectly

  • Just to check, have some times that I need to -'publish' the website api depending on the platform... unfortunately I never had this kind of inconvenience with genymotion... tamben pode ser una configuracao de subnet no Vbox (genymotion usa Vbox), if that’s the same not ten Acceso the network...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.