【问题标题】:Connection to web server and reading XML files(Android)连接到 Web 服务器并读取 XML 文件(Android)
【发布时间】:2015-07-26 11:18:52
【问题描述】:

我想要做的是连接到 Web 服务器并可以读取 XML 文件 例外是打开失败的 erofs(只读文件系统)。 我在清单中添加了:

uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"

我做错了什么?

public void getHTML() throws IOException {

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    try {
        URL url = new URL("http:/...");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        BufferedReader rd;
        String line;
        String name = "myfile.xml";
        conn.setRequestMethod("GET");
        rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        File file = new File(name);
        if(file.exists()) {
            file.delete();
            file = new File(name);
        }
        FileWriter fw = new FileWriter(file.getName(), true);
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter out = new PrintWriter(bw);
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
            fw.write(line);
            fw.write("\n");
            //out.println(line);
            //out.flush();
        }
        fw.close();
        out.close();
        rd.close();
    }        
    catch(Exception e){
        Toast.makeText(getApplicationContext()," "+e,Toast.LENGTH_SHORT).show();
    }
}

}

【问题讨论】:

  • 我们可以看看 Logcat 吗?
  • 首先,完全不鼓励使用StrictMode。您应该在单独的线程中进行网络操作,..而不是在主线程中..

标签: android xml web-services


【解决方案1】:

我做了修改,我解决了。我会发布正确的代码。

public void getHTML() 抛出 IOException {

    FileOutputStream outputStream=null;
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    String fullPath = "/mnt/sdcard/";
    try {
        URL url = new URL("http:..");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        BufferedReader rd;
        File dir = new File(fullPath.toString());
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(fullPath, "file.xml");
        if(file.exists())
            file.delete();
        file.createNewFile();
        String line;
        outputStream = new FileOutputStream(file);
        conn.setRequestMethod("GET");
        rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String newline="\n";
        while ((line = rd.readLine()) != null){
            outputStream.write(line.getBytes());
            outputStream.write(newline.getBytes());
            outputStream.flush();
        }
        outputStream.close();
        rd.close();
    } 
    catch(Exception e){
        Toast.makeText(getApplicationContext()," "+e,Toast.LENGTH_SHORT).show();
    }
}

【讨论】:

    猜你喜欢
    • 2014-06-22
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多