我正在尝试从Internet解析XML并在简单的TextView中显示其一些数据。这是我的代码:
public class MainActivity extends AppCompatActivity {
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView)findViewById(R.id.textView1);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
String UTF8 = "utf-8";
URL url = new URL("http://pollenapps.com/AllergyAlertWebSVC/api/1.0/Forecast/ForecastForZipCode?Zipcode=02145&Affiliateid=9642&AppID=2.1.0&uid=6693636764.xml");
URLConnection urlConnection = url.openConnection();
//InputStream in = new BufferedInputStream(urlConnection.getInputStream());
//BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
//InputStream in = new URL("http://pollenapps.com/AllergyAlertWebSVC/api/1.0/Forecast/ForecastForZipCode?Zipcode=02145&Affiliateid=9642&AppID=2.1.0&uid=6693636764.xml").openStream();
//getAssets().open("file.xml");
InputStream in = url.openStream();
//InputStream is2 = new FileInputStream(file);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
//Document doc = dBuilder.parse(new InputSource(new InputStreamReader(in, "UTF-8")));
Document doc = dBuilder.parse(in, "UTF-8");
//Document doc = dBuilder.parse(in);
Element element = doc.getDocumentElement();
element.normalize();
NodeList nList = doc.getElementsByTagName("allergyForecast");
for (int i=0; i<nList.getLength(); i++) {
Node node = nList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element2 = (Element) node;
tv1.setText(getValue("Day0", element2));
}
}
} catch (Exception e) {e.printStackTrace();}
}
private static String getValue(String tag, Element element) {
NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = nodeList.item(0);
return node.getNodeValue();
}
}
当我在本地文件(其中一个被推荐的InputStreams用于本地文件)上运行时,此代码按预期工作但是当我尝试在Internet上的XML文件上运行它时,我收到以下错误:
07-04 12:58:06.550 23289-23289/mystikos.pollentest W/System.err: org.xml.sax.SAXParseException: Unexpected token (position:TEXT {"allergyForecas...@1:990 in java.io.InputStreamReader@2ff0997)
根据一些研究,似乎解析器无法分辨XML文件的编码类型,因此我需要对其进行定义。但是,我不确定如何在这里定义...我在定义“UTF-8”编码的尝试在注释掉的行中是可见的,但那些不起作用。有什么建议吗?
谢谢!
答案 0 :(得分:0)
你得到的数据是json,而不是xml。当您从UrlOpenConnection获取inputStream时,您的网址中的前缀.xml
不会获取xml中的数据,并且可以删除。
要查看您获得的数据:
String data = "";
while (in.available() > 0)
data += (char)in.read();
System.out.println(data);
结果是:{“allergyForecast”:{“Day0”:4.1,“Day1”:3.7,“Day2”:4.6,“Day3”:1.4,“Day4”:2.9,“...}
要解析日期,您可以使用Gson作为示例。要解析元素的代码段:
JsonElement jelement = new JsonParser().parse(new InputStreamReader(in));
// or from readed from inputStream string:
// JsonElement jelement = new JsonParser().parse(data);
JsonObject jobject = jelement.getAsJsonObject();
jobject = jobject.getAsJsonObject("allergyForecast");
并且您可以从jsonObject获取所需的值。 或者,如果只需要xml,则可以检查API如何在xml中获得响应。