![](/img/trans.png)
[英]getting net::ERR_CONNECTION_REFUSED (http://localhost:8080) on android 4.4.2 version
[英]Connection to http://localhost:8080 refused in Android
我試圖在android中使用REST服務,但出現以下錯誤:
org.apache.http.conn.HttpHostConnectException: Connection to http://localhost:8080 refused
我已經介紹了權限:uses-permission android:name =“ android.permission.INTERNET”
我正在模擬器中運行代碼,並且可以運行,但是當我嘗試在智能手機中運行時,它會失敗。
PD:在模擬器中運行時,我使用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;
}
Localhost在您的手機上不起作用,因為它是指手機本身。
您需要獲取本地IP地址,然后改用它。 192.168.something。通常的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.