2012-08-08 14 views
0

這是我的代碼: 我用我的系統的IP地址包含listofitems.txt文件:如何從遠程系統讀取.text文件,然後將其寫入Android數組字符串中?

try { 
     Create a URL for the desired page 
String ipadd=ip.getText().toString(); 
     URL url = new URL("http://192.168.1.2/listofitems.txt"); 


     // Read all the text returned by the server 
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
     String str = in.readLine(); 

     String[] items = new String[]{ 

     items = str.split(","); 

    in.close(); 

    } catch (MalformedURLException e) { 

    } catch (IOException e) { 

    } 

我必須連接到無線路由器(WLAN)系統的平板電腦。 請幫忙!!!謝謝!!!

回答

0

試試這個:

try { 
      // Create a URL for the desired page 
      URL url = new URL("http://192.168.1.2/listofitems.txt"); 

      // Read all the text returned by the server 
      BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
      String all=""; 
      String str; 
      while ((str = in.readLine()) != null) { 
       all+=str; 
      } 
      in.close(); 

      items = all.split(","); 


     } catch (MalformedURLException e) { 
     } catch (IOException e) { 
     } 

並添加此體現:

<uses-permission android:name="android.permission.INTERNET" > 
</uses-permission> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" > 
</uses-permission> 
+0

嘿感謝名單了很多!這就像魔術!!!!! – amg 2012-08-09 10:48:13

+0

你的意思是:'String [] items = all.split(「,」);'on line 14? – 2014-08-01 08:01:12

相關問題