1

私はアンドロイドが初めてです。URLからXMLを取得したい。私のアプリケーションはとてもシンプルです。MainActivity には、次の画面に移動して XML データを表示するボタンがあります。

以下は、オンライン ソースから入手したサンプル コードですが、うまくいきません。何も表示されません。

private class LongOperation extends AsyncTask<String, Void, LinearLayout> {

    @Override
    protected LinearLayout doInBackground(String... string) {
        LinearLayout layout = new LinearLayout(DevicesActivity.this);
        layout.setOrientation(1);
        /** Create a new textview array to display the results */ 
        TextView name[];
        TextView website[];
        TextView category[];
        try {
            URL url = new URL(
                    "http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getElementsByTagName("item");
            /** Assign textview array lenght by arraylist size */
            name = new TextView[nodeList.getLength()];
            website = new TextView[nodeList.getLength()];
            category = new TextView[nodeList.getLength()];
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                name[i] = new TextView(DevicesActivity.this);
                website[i] = new TextView(DevicesActivity.this);
                category[i] = new TextView(DevicesActivity.this);
                Element fstElmnt = (Element) node;
                NodeList nameList = fstElmnt.getElementsByTagName("name");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();
                name[i].setText("Name = "
                        + ((Node) nameList.item(0)).getNodeValue());
                NodeList websiteList = fstElmnt.getElementsByTagName("website");
                Element websiteElement = (Element) websiteList.item(0);
                websiteList = websiteElement.getChildNodes();
                website[i].setText("Website = "
                        + ((Node) websiteList.item(0)).getNodeValue());
                category[i].setText("Website Category = "
                        + websiteElement.getAttribute("category"));
                layout.addView(name[i]);
                layout.addView(website[i]);
                layout.addView(category[i]);
            }
        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }
        /** Set the layout view to display */
        return layout;
    }      

    protected void onPostExecute(LinearLayout result) {
        setContentView(result);
        // here you will get the result
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /** Create a new layout to display the view */
     //setContentView(R.layout.activity_devices);
     new LongOperation().execute("");

}

LogCat からのログ

05-29 16:00:50.972: I/Choreographer(6780): Skipped 33 frames!  The application may be doing too much work on its main thread.
05-29 16:00:51.122: D/gralloc_goldfish(6780): Emulator without GPU emulation detected.
05-29 16:00:54.252: I/System.out(6780): XML Pasing Excpetion = android.os.NetworkOnMainThreadException
05-29 16:00:55.663: I/Choreographer(6780): Skipped 71 frames!  The application may be doing too much work on its main thread.

更新しました

05-29 19:55:38.952: E/Trace(7739): error opening trace file: No such file or directory (2)
05-29 19:55:41.022: D/gralloc_goldfish(7739): Emulator without GPU emulation detected.
05-29 19:55:48.472: I/Choreographer(7739): Skipped 31 frames!  The application may be doing too much work on its main thread.
05-29 19:55:49.182: I/Choreographer(7739): Skipped 68 frames!  The application may be doing too much work on its main thread.
05-29 19:55:50.662: I/System.out(7739): XML Pasing Excpetion = java.lang.SecurityException: Permission denied (missing INTERNET permission?)
4

2 に答える 2

2

Android は、ネットワーク操作がメイン スレッドで実行されているときにこの例外を返します。asynctask クラスを試す必要があります。コードの残りの部分は問題ないようです。asynctask のドキュメントを読んでください。お役に立てれば!

于 2013-05-29T16:30:05.083 に答える