繁体   English   中英

如何将Android应用程序连接到静态IP地址

[英]How to connect Android app to an static ip address

我正在尝试开发一个需要通过wifi连接到另一台设备的应用程序。 我知道另一台设备的IP地址,但是当我尝试连接到它时,却无法。

private static String getUrl(String direccion) throws ConnectException{

    try {
        URL u = new URL("http://192.168.1.216");

        HttpURLConnection co = (HttpURLConnection) u.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(co.getInputStream()));
        String line;

        while ((line = reader.readLine()) != null) 
            //buffer.append(line);
            System.out.println(line);

         reader.close();
         co.disconnect();
         System.out.println("######InputStream CORRECTA... "+u);

         return line;
    } catch (MalformedURLException e) {
        throw new ConnectException();

    } catch (java.net.ConnectException e){
        throw e;

    } catch (IOException e) {
        throw new ConnectException();
    } 
}

您还需要指定以下内容:

      co.setRequestMethod("GET");
      co.setDoOutput(true);
      co.connect();

因此,请尝试以下操作:

private static String getUrl(String direccion) throws ConnectException {
    try {
        URL u = new URL("http://192.168.1.216");

        HttpURLConnection co = (HttpURLConnection) u.openConnection();
        co.setRequestMethod("GET");
        co.setDoOutput(true);
        co.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(co.getInputStream()));
        String line;

        while ((line = reader.readLine()) != null) 
            //buffer.append(line);
            System.out.println(line);

            reader.close();
            co.disconnect();
            System.out.println("######InputStream CORRECTA... "+u);

            return line;
    } catch (MalformedURLException e) {
        throw new ConnectException();

    } catch (java.net.ConnectException e){
        throw e;

    } catch (IOException e) {
        throw new ConnectException();
    } 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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