【发布时间】:2017-01-19 08:36:57
【问题描述】:
我正在尝试从打开的天气应用程序中解析数据文档。我成功地阅读了整个文件。我可以把整个文件放到一个文本视图中。我只需要解析这些数据。尝试解析时出现此错误:
org.xml.sax.SAXParseException:文档意外结束
这是我用于解析和读取文档的代码。
public void Weather(View view){
InputStream data;
final String OPEN_WEATHER_MAP_API =
"http://api.openweathermap.org/data/2.5/weather?q=";
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
URL url = new URL(String.format(OPEN_WEATHER_MAP_API + City + "&mode=xml&appid=40f9dad632ecd4d87b55cb512d538b75"));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// connection.addRequestProperty("x-api-key", this.getString(R.string.open_weather_maps_app_id));
data = connection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(data);
BufferedReader Reader = new BufferedReader(inputStreamReader);
StringBuffer Weatherdata = new StringBuffer();
String storage;
while ((storage = Reader.readLine()) != null) {
Weatherdata.append(storage + "\n");
}
cityField.setText(Weatherdata.toString());
}
catch(Exception e){
e.printStackTrace();
cityField.setText("Fail");
return;
}
try {
DocumentBuilderFactory documetBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documetBuilderFactory.newDocumentBuilder();
Document xmlDocument = documentBuilder.parse(data);
Element rootElement = xmlDocument.getDocumentElement();
}
catch (Exception e){
e.printStackTrace();
}
}
我做了一个快速的谷歌搜索,当他将文件存储在计算机/手机上时,另一个出现此错误的人遇到了此错误。
【问题讨论】:
标签: android xml parsing android-studio xml-parsing