I am trying to consume a REST service in android and I am getting the following error:
org.apache.http.conn.HttpHostConnectException: Connection to http://localhost:8080 refused
I have introduced the permission: uses-permission android:name="android.permission.INTERNET"
I am running the code in a emulator and it works but when I try to run in my smartphone it fails.
PD: When I run in the emulator I use http://10.0.2.2:8080.
public boolean checkConnection(){
String res = "";
boolean serverUp = false;
try {
HttpClient client = new DefaultHttpClient();
//HttpGet request = new HttpGet("http://10.0.2.2:8080/prueba_conexion");
HttpGet request = new HttpGet("http://localhost:8080/prueba_conexion");
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String linea = "";
while ((linea = rd.readLine()) != null) {
res += linea;
}
serverUp = Boolean.parseBoolean(res);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return serverUp;
}
答案 0 :(得分:0)
Localhost doesnt work on your phone, because it refers to the phone itself.
You need to get your local IP address and use it instead ie. 192.168.something.something usually.