Android中的ConnectException访问远程服务器?

时间:2016-02-22 05:55:27

标签: javascript android httpurlconnection socketexception socket-timeout-exception

我正在尝试使用HTTP网址连接访问我的远程服务器。我遇到了以下问题。

当我第一次尝试连接时,它未连接。之后我重新启动设备,它已连接。 IP地址每天都会动态更改。

java.net.ConnectException: failed to connect to 127.0.0.1(port 80) after 20000ms: isConnected failed: EHOSTUNREACH (No route to host) 

public static boolean isPortalNetworkAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        HttpRequest.doCheck();
        if (netInfo != null && netInfo.isConnected()) {
            try {
                String url1="http://127.0.0.01/";
                System.out.println("url1"+url1);
                URL url = new URL(url1);
                HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
                urlc.setConnectTimeout(20000);
                urlc.connect();
                System.out.println("code" + urlc.getResponseCode());
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return false;

    }

如何解决此问题?

0 个答案:

没有答案