[英]Error in getting XML and parsing it
public String getMetaData() {
String errors = "";
try {
URL url = new URL("http://in2streaming.com:9999/stats?sid=1.xml");
URLConnection conn = url.openConnection();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// Error Here:
Document doc = db.parse(conn.getInputStream().toString());
// get the root node
NodeList nodeList = doc.getElementsByTagName("SHOUTCASTSERVER");
Node node=nodeList.item(0);
// the node has three child nodes
for (int i = 0; i < node.getChildNodes().getLength(); i++) {
Node temp=node.getChildNodes().item(i);
if(temp.getNodeName().equalsIgnoreCase("SONGTITLE")){
return temp.getTextContent();
}
}
return "Couldn't reach XML";
}
catch (Exception e) {
return "Exception ";
}
}
Calling this function via Runnable, Got Exception android.os.NetworkOnMainThreadException I might change the link to http://in2streaming.com:9999/7.html and use HTMl parser instead 通过Runnable调用此函数,出现异常android.os.NetworkOnMainThreadException我可能会将链接更改为http://in2streaming.com:9999/7.html并改用HTMl解析器
// Refresh meta data
private final Runnable refresh_meta = new Runnable() {
@Override
public void run() {
Toast.makeText(m_context, getMetaData(), Toast.LENGTH_SHORT).show();
m_handler.postDelayed(this, 5000);
}
};
For NetworkOnMainThreadException(You can also use AsyncTask): 对于NetworkOnMainThreadException(您也可以使用AsyncTask):
Executors.newSingleThreadExecutor().submit(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, getMetaData(), Toast.LENGTH_SHORT).show();
}
});
If you want to Schedule every 5 sec. 如果您想每5秒安排一次。
You can use ScheduledExecutorService 您可以使用ScheduledExecutorService
ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
worker.scheduleAtFixedRate(refresh_meta,
1, //initial delay
5, //run every 5 seconds
TimeUnit.SECONDS);
And Update your Runnable as 并将您的Runnable更新为
private Runnable refresh_meta = new Runnable() {
@Override
public void run() {
final String text = getMetaData();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(m_context, text, Toast.LENGTH_SHORT).show();
}
}
);
}
};
Also, 也,
Change Document doc = db.parse(conn.getInputStream().toString());
更改
Document doc = db.parse(conn.getInputStream().toString());
to 至
Document doc = db.parse(conn.getInputStream());
First some remarks: 首先说一下:
a) Do not muffle the exceptions like you do here : a)不要像在这里那样使异常消失:
catch (Exception e) {
return "Exception ";
}
This way you will never know what was the exception that was thrown. 这样,您将永远不会知道抛出了什么异常。 It is better to log/print the exception's stack trace, for example:
最好记录/打印异常的堆栈跟踪,例如:
catch (Exception e) {
Log.e("TAG", "Error", e);
return "Exception";
}
b) conn.getInputStream().toString()
doesn't do what you suppose it does (convert the InputStream
to String
). b)
conn.getInputStream().toString()
并没有做您想做的事情(将InputStream
转换为String
)。 DocumentBuilder
's parse method takes an InputStream
as parameter no need to convert it to String
. DocumentBuilder
的parse方法将InputStream
作为参数,而无需将其转换为String
。
Having the above in mind here is your method: 记住以上几点是您的方法:
public String getMetaData() {
String errors = "";
try {
URL url = new URL("http://in2streaming.com:9999/stats?sid=1.xml");
URLConnection conn = url.openConnection();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// Error Here:
Document doc = db.parse(conn.getInputStream());
// get the root node
NodeList nodeList = doc.getElementsByTagName("SHOUTCASTSERVER");
Node node=nodeList.item(0);
// the node has three child nodes
for (int i = 0; i < node.getChildNodes().getLength(); i++) {
Node temp=node.getChildNodes().item(i);
if(temp.getNodeName().equalsIgnoreCase("SONGTITLE")){
return temp.getTextContent();
}
}
return "Couldn't reach XML";
}
catch (Exception e) {
Log.e("TAG", "Error in getMetaData()", e);
return "Exception ";
}
}
Try running your app again and if an error arises from this method it will be printed in your logcat with the message "Error in getMetaData()". 尝试再次运行您的应用程序,如果此方法出现错误,它将在日志中显示,消息为“ Error in getMetaData()”。 Update your question accordingly with the error to let other members help you.
根据错误更新您的问题,让其他成员帮助您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.