Java 基于Android的web XML解析
您好,我试图从网站上读取xml文件,但我有一个问题 当我按下按钮时,我创建了类“DOMCreate”的对象,下一个应用程序崩溃 下面我将logcat中的错误放在下面:Java 基于Android的web XML解析,java,android,xml-parsing,domparser,Java,Android,Xml Parsing,Domparser,您好,我试图从网站上读取xml文件,但我有一个问题 当我按下按钮时,我创建了类“DOMCreate”的对象,下一个应用程序崩溃 下面我将logcat中的错误放在下面: 03-19 17:34:10.027 17003-17003/com.example.currencyconverter.app E/Trace﹕ error opening trace file: No such file or directory (2) 03-19 17:34:10.037 17003-17003/com
03-19 17:34:10.027 17003-17003/com.example.currencyconverter.app E/Trace﹕ error opening trace file: No such file or directory (2)
03-19 17:34:10.037 17003-17003/com.example.currencyconverter.app W/ActivityThread﹕ Application com.example.currencyconverter.app can be debugged on port 8100...
03-19 17:34:10.347 17003-17003/com.example.currencyconverter.app I/Adreno200-EGL﹕ <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.081_msm7627a_JB_REL_2.0.3_CL2820657_release_AU (CL2820657)
Build Date: 01/22/13 Tue
Local Branch:
Remote Branch: quic/jb_rel_2.0.3
Local Patches: NONE
Reconstruct Branch: AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.081 + NOTHING
03-19 17:34:10.397 17003-17003/com.example.currencyconverter.app E/copybit﹕ Error opening frame buffer errno=13 (Permission denied)
03-19 17:34:10.397 17003-17003/com.example.currencyconverter.app W/Adreno200-EGLSUB﹕ <updater_create_surface_state:342>: updater_create_surface_state failed to open copybit, error: -13
03-19 17:34:10.397 17003-17003/com.example.currencyconverter.app D/memalloc﹕ ion: Mapped buffer base:0x53bee000 size:1536000 offset:0 fd:61
03-19 17:34:10.397 17003-17003/com.example.currencyconverter.app D/memalloc﹕ ion: Mapped buffer base:0x40045000 size:4096 offset:0 fd:63
03-19 17:34:10.557 17003-17003/com.example.currencyconverter.app D/memalloc﹕ ion: Mapped buffer base:0x546a3000 size:1536000 offset:0 fd:66
03-19 17:34:10.557 17003-17003/com.example.currencyconverter.app D/memalloc﹕ ion: Mapped buffer base:0x40072000 size:4096 offset:0 fd:68
03-19 17:34:10.957 17003-17003/com.example.currencyconverter.app D/memalloc﹕ ion: Mapped buffer base:0x5491a000 size:1536000 offset:0 fd:70
03-19 17:34:10.957 17003-17003/com.example.currencyconverter.app D/memalloc﹕ ion: Mapped buffer base:0x4008b000 size:4096 offset:0 fd:72
03-19 17:34:11.457 17003-17003/com.example.currencyconverter.app D/memalloc﹕ ion: Mapped buffer base:0x54a91000 size:1536000 offset:0 fd:74
03-19 17:34:11.457 17003-17003/com.example.currencyconverter.app D/memalloc﹕ ion: Mapped buffer base:0x40091000 size:4096 offset:0 fd:76
03-19 17:34:19.727 17003-17003/com.example.currencyconverter.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41051540)
03-19 17:34:19.767 17003-17003/com.example.currencyconverter.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3609)
at android.view.View.performClick(View.java:4102)
at android.view.View$PerformClick.run(View.java:17085)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5520)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3604)
at android.view.View.performClick(View.java:4102)
at android.view.View$PerformClick.run(View.java:17085)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5520)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1208)
at java.net.InetAddress.lookupHostByName(InetAddress.java:388)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:239)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
at com.example.currencyconverter.app.DOMCreator.<init>(DOMCreator.java:34)
at com.example.currencyconverter.app.MainActivity.exchangeRate(MainActivity.java:69)
at com.example.currencyconverter.app.MainActivity.convert(MainActivity.java:58)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3604)
at android.view.View.performClick(View.java:4102)
at android.view.View$PerformClick.run(View.java:17085)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5520)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)
03-19 17:34:10.027 17003-17003/com.example.currencyconverter.app E/Trace﹕ 打开跟踪文件时出错:没有此类文件或目录(2)
03-19 17:34:10.037 17003-17003/com.example.currencyconverter.app W/ActivityThread﹕ 可以在端口8100上调试应用程序com.example.currencyconverter.app。。。
03-19 17:34:10.347 17003-17003/com.example.currencyconverter.app I/Adreno200 EGL﹕ : EGL 1.4高通公司构建:AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.081_msm7627a_JB_REL_2.0.3_CL2820657_发布(CL2820657)
建造日期:2013年1月22日星期二
本地分行:
远程分支:quic/jb_rel_2.0.3
本地修补程序:无
重建分支:AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.081+无
03-19 17:34:10.397 17003-17003/com.example.currencyconverter.app E/copybit﹕ 打开帧缓冲区errno=13时出错(权限被拒绝)
03-19 17:34:10.397 17003-17003/com.example.currencyconverter.app W/Adreno200 EGLSUB﹕ : 更新程序\u创建\u曲面\u状态无法打开copybit,错误:-13
03-19 17:34:10.397 17003-17003/com.example.currencyconverter.app D/memalloc﹕ 离子:映射的缓冲区基数:0x53000大小:1536000偏移量:0 fd:61
03-19 17:34:10.397 17003-17003/com.example.currencyconverter.app D/memalloc﹕ 离子:映射的缓冲区基:0x40045000大小:4096偏移量:0 fd:63
03-19 17:34:10.557 17003-17003/com.example.currencyconverter.app D/memalloc﹕ 离子:映射的缓冲区基数:0x546a3000大小:1536000偏移量:0 fd:66
03-19 17:34:10.557 17003-17003/com.example.currencyconverter.app D/memalloc﹕ 离子:映射的缓冲区基数:0x40072000大小:4096偏移量:0 fd:68
03-19 17:34:10.957 17003-17003/com.example.currencyconverter.app D/memalloc﹕ 离子:映射的缓冲区基数:0x5491a000大小:1536000偏移量:0 fd:70
03-19 17:34:10.957 17003-17003/com.example.currencyconverter.app D/memalloc﹕ 离子:映射的缓冲区基数:0x4008b000大小:4096偏移量:0 fd:72
03-19 17:34:11.457 17003-17003/com.example.currencyconverter.app D/memalloc﹕ 离子:映射的缓冲区基数:0x54a91000大小:1536000偏移量:0 fd:74
03-19 17:34:11.457 17003-17003/com.example.currencyconverter.app D/memalloc﹕ 离子:映射的缓冲区基数:0x40091000大小:4096偏移量:0 fd:76
03-19 17:34:19.727 17003-17003/com.example.currencyconverter.app W/dalvikvm﹕ threadid=1:线程以未捕获异常退出(组=0x41051540)
03-19 17:34:19.767 17003-17003/com.example.currencyconverter.app E/AndroidRuntime﹕ 致命异常:主
java.lang.IllegalStateException:无法执行活动的方法
在android.view.view$1.onClick上(view.java:3609)
在android.view.view.performClick上(view.java:4102)
在android.view.view$PerformClick.run(view.java:17085)
位于android.os.Handler.handleCallback(Handler.java:615)
位于android.os.Handler.dispatchMessage(Handler.java:92)
位于android.os.Looper.loop(Looper.java:155)
位于android.app.ActivityThread.main(ActivityThread.java:5520)
位于java.lang.reflect.Method.Invokenactive(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:511)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run上(ZygoteInit.java:1029)
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
在dalvik.system.NativeStart.main(本机方法)
原因:java.lang.reflect.InvocationTargetException
位于java.lang.reflect.Method.Invokenactive(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:511)
在android.view.view$1.onClick上(view.java:3604)
在android.view.view.performClick上(view.java:4102)
在android.view.view$PerformClick.run(view.java:17085)
位于android.os.Handler.handleCallback(Handler.java:615)
位于android.os.Handler.dispatchMessage(Handler.java:92)
位于android.os.Looper.loop(Looper.java:155)
位于android.app.ActivityThread.main(ActivityThread.java:5520)
位于java.lang.reflect.Method.Invokenactive(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:511)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run上(ZygoteInit.java:1029)
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
在dalvik.system.NativeStart.main(本机方法)
原因:android.os.NetworkOnMainThreadException
在android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork上(StrictMode.java:1208)
位于java.net.InetAddress.lookupHostByName(InetAddress.java:388)
位于java.net.InetAddress.getAllByNameImpl(InetAddress.java:239)
位于java.net.InetAddress.getAllByName(InetAddress.java:214)
位于libcore.net.http.HttpConnection。(HttpConnection.java:70)
在libcore.net.http.HttpConnection上。(HttpConnection.java:50)
位于libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
位于libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
位于libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
位于libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
位于libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
位于libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
位于libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
位于libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
位于libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
在com.example.currencyconverter.app.DOMCreator.(DOMC
public class DOMCreator {
Document doc;
public DOMCreator(){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try{
URL nbp = new URL("http://www.nbp.pl/kursy/xml/a054z140319.xml");
URLConnection urlConn = nbp.openConnection();
builder = factory.newDocumentBuilder();
InputStream is = urlConn.getInputStream();
doc = builder.parse(is);
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}catch(ParserConfigurationException e){
e.printStackTrace();
}catch(SAXException e){
e.printStackTrace();
}
}
public Double getRate(String scn){
if (doc!=null){
NodeList nodeList = doc.getElementsByTagName("pozycja");
for(int i = 0;i<nodeList.getLength();i++){
Element pos = (Element) nodeList.item(i);
NodeList childNodes = pos.getElementsByTagName("*");
String vName = "";
String vValue = "";
for(int j = 0; j<childNodes.getLength();j++){
Element posChild = (Element) nodeList.item(j);
if(posChild.getNodeName().equals("kod_waluty")){
vName = posChild.getTextContent();
}else if(posChild.getNodeName().equals("kurs_sredni")){
vValue = posChild.getTextContent();
}
}
if(vName.equals(scn)){
return Double.parseDouble(vValue);
}
}
}
return -1.0;
}
}
Caused by: android.os.NetworkOnMainThreadException