繁体   English   中英

在Android中拒绝连接到http:// localhost:8080

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM