2014-04-02 137 views
0

以前,我使用assets文件夹中的XML文件。该应用可以很好地阅读。下一步我想把这个XML放在Web服务器上。但在这个阶段,应用程序无法识别任何数据。它让我困惑了几天。尝试从URL获取XML

AssetManager asset = getAssets(); 
InputStream input = asset.open("student.xml"); 
List<Student> list = ParserByPULL.getStudents(input); 

一切工作正常,如果资产文件夹中的文件。 然后我试图从URL中获取它。

String path = "http://fthgyj.tup632.cnaaa11.com/student.xml"; 
URL url = new URL(path); 
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
conn.setConnectTimeout(5000); 
conn.setRequestMethod("GET"); 
InputStream input = url.openConnection().getInputStream(); 
List<Student> list = ParserByPULL.getStudents(input); 

我已经添加了在清单文件中连接INTERNET的权限。 有没有人有这个想法?

+0

那又怎么了?你得到异常或什么,只是空的回应? – vilpe89

+0

NetworkOnMainThreadException也许? – laalto

+0

告诉我们您问题的确切症状。让我们猜测是不好的,除此之外,你不会得到你的答案 – kiruwka

回答

0

我认为你需要调用

conn.connect(); 
InputStream input = conn.getInputStream(); 

,然后检查你得到了XML牵强:

BufferedReader reader = new BufferedReader(new InputStreamReader(input)); 
StringBuilder builder = new StringBuilder(); 
String line; 

while ((line = reader.readLine()) != null) { 
    builder.append(line); 
} 
reader.close(); 
Log.d("tag", "output: " + builder.toString()); 
+0

我试过了,但还是不行。无论如何,非常感谢。 – user3421634

+0

你确定你没有从输入流中获取任何东西吗?检查我更新的答案 – vilpe89