【发布时间】: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