0

このコードを使用してxmlファイルを解析しています。URLアドレスがありますが、この文のメインスレッド例外でandroidosnetworkというエラーが発生します。URLConnectionhttpConnection=url.openConnection(); inputStream = httpConnection.getInputStream(); これが私のコードです:

package com.example.mainp;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Vector;

import org.apache.http.HttpConnection;
import org.kxml.Xml;
import org.kxml.parser.XmlParser;
import org.kxml.parser.ParseEvent;
import org.kxml.parser.XmlParser;

import android.app.Activity;
import android.widget.Toast;






public class parse {

     public static Vector places;
     Activity ac;
    Place place;
      private ParseEvent event = null;
    public void init(Activity activity) {
        this.ac=activity;
        XmlParser parser = null;
        InputStreamReader isR = null;
        HttpConnection connection = null;
        try {

            InputStream inputStream = null;
            System.out.println("0");
            Toast.makeText(ac, "24", Toast.LENGTH_LONG).show();
            //URL url = new URL("http://217.52.114.3/Mobinil%20Islamic%20WebService/MobinilIslamicService.asmx/GetIslamicDirectory");
            URL url= new URL("http://217.52.114.3/CinemaGuid/CinemaGuid.asmx/getTVSchedule");
            System.err.println("2");
            URLConnection httpConnection = url.openConnection();
            inputStream = httpConnection.getInputStream();
//          inputStream = connection.openInputStream();
            Toast.makeText(ac, "4", Toast.LENGTH_LONG).show();  
            isR = new InputStreamReader(inputStream, "UTF-8");
            System.out.println("3");
            parser = new XmlParser(isR);
            System.out.println("4");
            Toast.makeText(activity, "5", Toast.LENGTH_LONG).show();    
            places = new Vector<Place>();
            traverse(parser);
            isR.close();
            inputStream.close();
            inputStream = null;
            isR = null;
            // System.gc();
        } catch (Exception ex) {
            Toast.makeText(activity, ex.toString(), Toast.LENGTH_LONG).show();  
        }
    }
    private void traverse(XmlParser parser) {
        // TODO Auto-generated method stub
        boolean leave = false;
        do {
            try {
                event = parser.read();
                ParseEvent pe;
                switch (event.getType()) {
                    case Xml.START_TAG:
                        // see API doc of StartTag for more access methods
                        // Pick up Title for display
                        if("places".equalsIgnoreCase(event.getName())){
                            places = new Vector();
                            //Toast.makeText(ac, "6", Toast.LENGTH_LONG).show();    
                        }
                        if("places".equalsIgnoreCase(event.getName())){
                            place = new Place();
                            place.setName(event.getValue("name"));
                            place.seturl(event.getValue("url"));
                            place.setaddress(event.getValue("adress"));
                            //Toast.makeText(MainPActivity.this, "7", Toast.LENGTH_LONG).show();    

                        }
                        if("description".equalsIgnoreCase(event.getName())){
                             pe = parser.read();
                            if(pe.getText()!=null){
                                place.setdescription(pe.getText().toString());
                            }
                        }
                        traverse(parser); // recursion call for each <tag></tag>
                        break;
                    case Xml.END_TAG:
                        if ("Place".equalsIgnoreCase(event.getName())) {
                            places.addElement(place);
                         // Toast.makeText(MainPActivity.this, "8", Toast.LENGTH_LONG).show();  
                        }
                        leave = true;
                        break;
                    case Xml.END_DOCUMENT:
                        leave = true;
                        break;
                    case Xml.TEXT:
                        break;
                    case Xml.WHITESPACE:
                        break;
                    default:
                }
            } catch (Exception ex) {
                Toast.makeText(ac, ex.getMessage(), Toast.LENGTH_LONG).show();  

            }
        } while (!leave);

    }
}
4

1 に答える 1

0

ネットワーク呼び出しは別のスレッドで行う必要があります。UI (デフォルト) のメイン スレッドでネットワーク呼び出しを行っており、Android 4.0 以降では例外がスローされます。AsyncTask またはワーカー スレッドを調べて、実行時間の長いタスクを別のプロセスで実行する

http://developer.android.com/guide/components/processes-and-threads.html

于 2012-08-29T10:24:25.957 に答える