2013-02-05 88 views
0

我想開發一個應用程序,需要通過WiFi連接到另一個設備。我知道其他設備的IP地址,但是當我嘗試連接到它時,我無法使用。如何將Android應用程序連接到靜態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(); 
    } 
} 
+1

後你所面對的問題logcat的?? –

回答

0

您還需要指定這些:

 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(); 
    } 
} 
相關問題