如何从远程系统读取.text文件,然后将其写入Android中的字符串数组?

时间:2012-08-08 11:24:20

标签: android

这是我的代码: 我正在使用我的系统的IP地址,其中包含listofitems.txt文件:

    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)的系统。 请帮忙!!!谢谢!!!

1 个答案:

答案 0 :(得分: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>