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?
– André Ribeiro
to running the server on my local machine, and using genymotion as emulator for android
– Enzo Tiezzi
10.0.2.2 is the IP of your machine on the local network?
– André Ribeiro
No, but this is the address that an android emulator recognizes as
localhost
according to the documentationEmulator Networking
– Enzo Tiezzi
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.
– André Ribeiro
the server is the localhost
– Enzo Tiezzi
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.
– André Ribeiro
That’s what the
10.0.0.2
but it still doesn’t work.– Enzo Tiezzi
Access by android browser that same address and see what the result.
– André Ribeiro
neither with my ip nor with the
10.0.0.2
it works– Enzo Tiezzi
Then you better check that the server is running correctly on your machine. You have already tested on a real device?
– André Ribeiro
Let’s go continue this discussion in chat.
– André Ribeiro
Enzo, searching for access to a device’s localhost, staff generally recommend using ip
10.0.3.2
.– Wakim
I’ve tried this one too, but it’s also not possible
– Enzo Tiezzi
Testing outside of Genymotion, the Web Service is accessible?
– Wakim
yes, functioning normally
– Enzo Tiezzi