Question

Hello i try to read xml file from website but i have a problem.

When i press a button i create object of class "DOMCreate" and next application is crashed.

Below i put errors from 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)

And me code:

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

I dont know what's wrong with this code.

Was it helpful?

Solution

This is most common issue. You cant process any network operation in UI thread. That means you cant access, process, or get anything from internet directly from your activity. This is restricted by default in android to maintain application responsiveness. The logcat clearly shows it by

Caused by: android.os.NetworkOnMainThreadException

Solution : you have to use AsyncTask class for any network operation. just put everything cpu time consuming process, or long data download in doInBackground() method. after getting the data show it in onPostExcecute() method.

android reference:

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

Complete Turtorials:

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top