面对从web到Android获取xml数据的困难

时间:2014-01-31 09:53:29

标签: android xml parsing xml-parsing

我需要一些帮助。

我在http://t3.netne.net/test.xml托管了一个xml文件 xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<game>
    <stat>
      <varx>5</varx>
      <vary>10</vary>
    </stat>
</game>

现在我需要varx的值,并且在我的Android应用程序中有所不同。

这是我到目前为止所做的:

public class MultiPlayerGameMenuActivity extends Activity {
    String url = "http://t3.netne.net/test.xml";
    ProgressDialog pDialog;
    String msgx = "x";
    String msgy = "y";

    public void HostNewGameButtonClicked(View view){


        Toast t = new Toast(this);
        t.setText(msgx+"    "+msgy);
        t.setDuration(1000);
        t.show();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_player_game_menu);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
             new GetXml(url).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new String[]{null});
        else
            new GetXml(url).execute(new String[]{null});

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.multi_player_game_menu, menu);
        return true;
    }

    public class GetXml extends AsyncTask<String, Void, String>{
        String urls;

        public GetXml(String urls){
            this.urls = urls;
        }

        @Override
        protected void onPreExecute(){
            pDialog = ProgressDialog.show(MultiPlayerGameMenuActivity.this, "Fetching Details... ", "Please Wait...", true);

        }

        @Override
        protected String doInBackground(String... params){
            URL url;
            try{
                url = new URL(urls);
                DocumentBuilderFactory mDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder mDocumentBuilder = mDocumentBuilderFactory.newDocumentBuilder();
                Document mDocument = mDocumentBuilder.parse(new InputSource(url.openStream()));

                mDocument.getDocumentElement().normalize();

                NodeList mNodeList = mDocument.getElementsByTagName("stat");

                for(int i=0;i<mNodeList.getLength();i++){
                    Node mNode = mNodeList.item(i);

                    Element mElement = (Element) mNode;
                    NodeList mStringList = mElement.getElementsByTagName("varx");
                    Element strElement = (Element) mStringList.item(0);
                    mStringList = strElement.getChildNodes();

                    msgx = mStringList.item(0).getNodeValue();

                    NodeList mStringList2 = mElement.getElementsByTagName("vary");
                    Element strElement2 = (Element) mStringList2.item(0);
                    mStringList2 = strElement2.getChildNodes();

                    msgy = mStringList.item(0).getNodeValue();
                }
            }catch(MalformedURLException e){
                e.printStackTrace();
            }catch(ParserConfigurationException e){
                e.printStackTrace();
            }catch(SAXException e){
                e.printStackTrace();
            }catch(IOException e){
                e.printStackTrace();
            }
                return null;
        }

        @Override
        protected void onPostExecute(String result){
            if(pDialog.isShowing())
                pDialog.dismiss();
        }

    }

}

这是LOGCAT

01-31 04:48:30.760: D/dalvikvm(1139): GC_FOR_ALLOC freed 39K, 4% free 2888K/3008K, paused 46ms, total 50ms
01-31 04:48:30.790: I/dalvikvm-heap(1139): Grow heap (frag case) to 5.041MB for 2254016-byte allocation
01-31 04:48:30.840: D/dalvikvm(1139): GC_FOR_ALLOC freed 2K, 3% free 5087K/5212K, paused 45ms, total 45ms
01-31 04:48:31.570: D/gralloc_goldfish(1139): Emulator without GPU emulation detected.
01-31 04:48:35.290: D/dalvikvm(1139): GC_FOR_ALLOC freed 58K, 3% free 5346K/5480K, paused 138ms, total 140ms
01-31 04:48:36.010: I/Choreographer(1139): Skipped 41 frames!  The application may be doing too much work on its main thread.
01-31 04:48:37.410: I/Choreographer(1139): Skipped 334 frames!  The application may be doing too much work on its main thread.
01-31 04:48:38.430: I/Choreographer(1139): Skipped 214 frames!  The application may be doing too much work on its main thread.
01-31 04:48:39.310: I/Choreographer(1139): Skipped 132 frames!  The application may be doing too much work on its main thread.
01-31 04:48:39.480: I/Choreographer(1139): Skipped 39 frames!  The application may be doing too much work on its main thread.
01-31 04:48:45.920: D/dalvikvm(1139): GC_FOR_ALLOC freed 93K, 3% free 6116K/6284K, paused 145ms, total 145ms
01-31 04:48:45.980: I/Choreographer(1139): Skipped 52 frames!  The application may be doing too much work on its main thread.
01-31 04:48:46.970: W/dalvikvm(1139): threadid=11: thread exiting with uncaught exception (group=0xb3af9b90)
01-31 04:48:47.000: E/AndroidRuntime(1139): FATAL EXCEPTION: AsyncTask #1
01-31 04:48:47.000: E/AndroidRuntime(1139): Process: com.ariondev.tictactoeonline, PID: 1139
01-31 04:48:47.000: E/AndroidRuntime(1139): java.lang.RuntimeException: An error occured while executing doInBackground()
01-31 04:48:47.000: E/AndroidRuntime(1139):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at java.lang.Thread.run(Thread.java:841)
01-31 04:48:47.000: E/AndroidRuntime(1139): Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at java.net.URL.openStream(URL.java:470)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at com.ariondev.tictactoeonline.MultiPlayerGameMenuActivity$GetXml.doInBackground(MultiPlayerGameMenuActivity.java:83)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at com.ariondev.tictactoeonline.MultiPlayerGameMenuActivity$GetXml.doInBackground(MultiPlayerGameMenuActivity.java:1)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-31 04:48:47.000: E/AndroidRuntime(1139):     ... 3 more
01-31 04:48:47.000: E/AndroidRuntime(1139): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at libcore.io.Posix.getaddrinfo(Native Method)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
01-31 04:48:47.000: E/AndroidRuntime(1139):     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
01-31 04:48:47.000: E/AndroidRuntime(1139):     ... 19 more
01-31 04:48:47.000: E/AndroidRuntime(1139): Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
01-31 04:48:47.000: E/AndroidRuntime(1139):     ... 22 more
01-31 04:48:47.050: I/Choreographer(1139): Skipped 31 frames!  The application may be doing too much work on its main thread.
01-31 04:48:48.240: I/Choreographer(1139): Skipped 34 frames!  The application may be doing too much work on its main thread.
01-31 04:48:48.530: I/Choreographer(1139): Skipped 75 frames!  The application may be doing too much work on its main thread.
01-31 04:48:48.670: I/Choreographer(1139): Skipped 30 frames!  The application may be doing too much work on its main thread.
01-31 04:48:49.110: I/Choreographer(1139): Skipped 114 frames!  The application may be doing too much work on its main thread.
01-31 04:48:50.100: I/Choreographer(1139): Skipped 254 frames!  The application may be doing too much work on its main thread.
01-31 04:48:50.350: I/Choreographer(1139): Skipped 63 frames!  The application may be doing too much work on its main thread.
01-31 04:48:50.540: I/Choreographer(1139): Skipped 48 frames!  The application may be doing too much work on its main thread.
01-31 04:48:51.450: E/WindowManager(1139): android.view.WindowLeaked: Activity com.ariondev.tictactoeonline.MultiPlayerGameMenuActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b402c970 V.E..... R.....ID 0,0-480,243} that was originally added here
01-31 04:48:51.450: E/WindowManager(1139):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:346)
01-31 04:48:51.450: E/WindowManager(1139):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
01-31 04:48:51.450: E/WindowManager(1139):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
01-31 04:48:51.450: E/WindowManager(1139):  at android.app.Dialog.show(Dialog.java:286)
01-31 04:48:51.450: E/WindowManager(1139):  at android.app.ProgressDialog.show(ProgressDialog.java:116)
01-31 04:48:51.450: E/WindowManager(1139):  at android.app.ProgressDialog.show(ProgressDialog.java:99)
01-31 04:48:51.450: E/WindowManager(1139):  at com.ariondev.tictactoeonline.MultiPlayerGameMenuActivity$GetXml.onPreExecute(MultiPlayerGameMenuActivity.java:72)
01-31 04:48:51.450: E/WindowManager(1139):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
01-31 04:48:51.450: E/WindowManager(1139):  at com.ariondev.tictactoeonline.MultiPlayerGameMenuActivity.onCreate(MultiPlayerGameMenuActivity.java:50)
01-31 04:48:51.450: E/WindowManager(1139):  at android.app.Activity.performCreate(Activity.java:5243)
01-31 04:48:51.450: E/WindowManager(1139):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-31 04:48:51.450: E/WindowManager(1139):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
01-31 04:48:51.450: E/WindowManager(1139):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
01-31 04:48:51.450: E/WindowManager(1139):  at android.app.ActivityThread.access$700(ActivityThread.java:135)
01-31 04:48:51.450: E/WindowManager(1139):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
01-31 04:48:51.450: E/WindowManager(1139):  at android.os.Handler.dispatchMessage(Handler.java:102)
01-31 04:48:51.450: E/WindowManager(1139):  at android.os.Looper.loop(Looper.java:137)
01-31 04:48:51.450: E/WindowManager(1139):  at android.app.ActivityThread.main(ActivityThread.java:4998)
01-31 04:48:51.450: E/WindowManager(1139):  at java.lang.reflect.Method.invokeNative(Native Method)
01-31 04:48:51.450: E/WindowManager(1139):  at java.lang.reflect.Method.invoke(Method.java:515)
01-31 04:48:51.450: E/WindowManager(1139):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-31 04:48:51.450: E/WindowManager(1139):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-31 04:48:51.450: E/WindowManager(1139):  at dalvik.system.NativeStart.main(Native Method)
01-31 04:48:51.980: I/Process(1139): Sending signal. PID: 1139 SIG: 9

它正在粉碎。请告诉我我做错了什么。我需要尽快。 提前致谢。 问候。

1 个答案:

答案 0 :(得分:1)

Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)

您未能在清单

中添加互联网权限
<uses-permission android:name="android.permission.INTERNET"/>