【问题标题】:XML-Parsing from web on Android在 Android 上从 Web 解析 XML
【发布时间】:2014-03-19 16:49:36
【问题描述】:

您好,我尝试从网站读取 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.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)

还有我的代码:

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;
    }
}

我不知道这段代码有什么问题。

【问题讨论】:

    标签: java android xml-parsing domparser


    【解决方案1】:

    这是最常见的问题。您无法在 UI 线程中处理任何网络操作。这意味着您无法直接从您的活动中访问、处理或从 Internet 获取任何内容。这在 android 中默认受到限制,以保持应用程序的响应能力。 logcat 通过

    清楚地显示它
    Caused by: android.os.NetworkOnMainThreadException
    

    解决方案: 您必须使用 AsyncTask 类进行任何网络操作。只需将所有耗时的cpu过程或长时间的数据下载放在doInBackground()方法中即可。获取数据后以onPostExcecute()方法显示。

    安卓参考:

    http://developer.android.com/reference/android/os/AsyncTask.html

    完整教程:

    http://www.androidhive.info/2012/04/android-downloading-file-by-showing-progress-bar/

    http://samir-mangroliya.blogspot.com/p/android-asynctask-example.html

    【讨论】:

      猜你喜欢
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多