尝试加载csvToGeoPoints时出错

时间:2012-11-14 21:08:20

标签: android csv mapactivity geopoints

我需要从csv收集gps点并创建一个地理位置列表,这样我就可以在地图上添加它们作为叠加层。所以我想出了这段代码,根据要回收的垃圾类型收集所有相关点。 我正试图加载它并出现错误(或者如果我错了,错误与它无关,请告诉我):

请考虑recycleType = 3(电池)。

public ArrayList<GeoPoint> csvToGeoPoints(int recycleType) {
    String csvString = null;
    URL myurl = null;
    HttpURLConnection urlConn;

    InputStreamReader inStream = null;
    BufferedReader buff = null;

    try {
        myurl = new URL("http://www.tel-aviv.gov.il/OnlineServices/DataTLV/Documents/%D7%A2%D7%99%D7%A8%D7%99%D7%99%D7%AA%20%D7%AA%D7%9C-%D7%90%D7%91%D7%99%D7%91-%D7%99%D7%A4%D7%95%20-%20%D7%A4%D7%A8%D7%99%D7%A1%D7%AA%20%D7%9E%D7%99%D7%9B%D7%9C%D7%99%20%D7%9E%D7%97%D7%96%D7%95%D7%A8.csv");
        //urlConn = myurl.openConnection();
        urlConn = (HttpURLConnection) myurl.openConnection();
        inStream = new InputStreamReader(urlConn.getInputStream());
        buff = new BufferedReader(inStream);
        while ((csvString = buff.readLine()) != null) {
            String[] elements = csvString.split(","); // getting the objects out of the csv's line
            int lineLen = elements.length; // for later checking that line is not corrupted
            if (lineLen == 14) { // making sure that line actually contains 14 objects. otherwise, skipping it
                for (int i=0;i < lineLen;i++) { // for each line in csv file
                    int currentRecycleType = Integer.getInteger(elements[6]); // checking the type of recycling chosen by user
                    if (recycleType == currentRecycleType) { // only if this line has the user's choice, collecting the item
                        int lon = Integer.getInteger(elements[10])*1000000;
                        int lat = Integer.getInteger(elements[11])*1000000;
                        GeoPoint currentPoint = new GeoPoint(lon,lat);
                        myPoints.add(currentPoint); // adding all the points together :)
                    }
                }
            }   
        }

    } catch (MalformedURLException e) {
        //System.out.println("Please check the spelling of the url: "+ e.toString());
        // pop up a window with error
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Reset...");
        alertDialog.setMessage("Error: " + e.toString());
        //alertDialog.setButton("OK", null);
        //alertDialog.setIcon(R.drawable.icon);
        alertDialog.show();
    } catch (IOException e1) {
        //System.out.println("Can't read from the Internet: " + e1.toString());
        // pop up a window with error
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Reset...");
        alertDialog.setMessage("Error: " + e1.toString());
        //alertDialog.setButton("OK", null);
        //alertDialog.setIcon(R.drawable.icon);
        alertDialog.show();
    }
    finally {
        try {
            inStream.close();
            buff.close();
        } catch (Exception e) {
            AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("error");
            alertDialog.setMessage("Error: " + e.toString());
            //alertDialog.setButton("OK", null);
            //alertDialog.setIcon(R.drawable.icon);
            alertDialog.show();
            //e.printStackTrace();
        }
    }

    return myPoints;

}

我收到的错误消息是:

    11-14 22:54:25.700: E/Trace(733): error opening trace file: No such file or directory (2)
11-14 22:54:26.620: D/dalvikvm(733): GC_CONCURRENT freed 178K, 3% free 8268K/8519K, paused 32ms+15ms, total 136ms
11-14 22:54:26.620: D/dalvikvm(733): WAIT_FOR_CONCURRENT_GC blocked 53ms
11-14 22:54:26.630: W/CursorWrapperInner(733): Cursor finalized without prior close()
11-14 22:54:26.630: W/CursorWrapperInner(733): Cursor finalized without prior close()
11-14 22:54:26.959: D/dalvikvm(733): GC_CONCURRENT freed 27K, 2% free 8675K/8839K, paused 28ms+29ms, total 122ms
11-14 22:54:26.980: D/AndroidRuntime(733): Shutting down VM
11-14 22:54:26.990: W/dalvikvm(733): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-14 22:54:27.010: E/AndroidRuntime(733): FATAL EXCEPTION: main
11-14 22:54:27.010: E/AndroidRuntime(733): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.recycletelaviv/com.example.recycletelaviv.ChooseRecycle}: android.os.NetworkOnMainThreadException
11-14 22:54:27.010: E/AndroidRuntime(733):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-14 22:54:27.010: E/AndroidRuntime(733):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-14 22:54:27.010: E/AndroidRuntime(733):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-14 22:54:27.010: E/AndroidRuntime(733):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-14 22:54:27.010: E/AndroidRuntime(733):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 22:54:27.010: E/AndroidRuntime(733):  at android.os.Looper.loop(Looper.java:137)
11-14 22:54:27.010: E/AndroidRuntime(733):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-14 22:54:27.010: E/AndroidRuntime(733):  at java.lang.reflect.Method.invokeNative(Native Method)
11-14 22:54:27.010: E/AndroidRuntime(733):  at java.lang.reflect.Method.invoke(Method.java:511)
11-14 22:54:27.010: E/AndroidRuntime(733):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-14 22:54:27.010: E/AndroidRuntime(733):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-14 22:54:27.010: E/AndroidRuntime(733):  at dalvik.system.NativeStart.main(Native Method)
11-14 22:54:27.010: E/AndroidRuntime(733): Caused by: android.os.NetworkOnMainThreadException
11-14 22:54:27.010: E/AndroidRuntime(733):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
11-14 22:54:27.010: E/AndroidRuntime(733):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-14 22:54:27.010: E/AndroidRuntime(733):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-14 22:54:27.010: E/AndroidRuntime(733):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-14 22:54:27.010: E/AndroidRuntime(733):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
11-14 22:54:27.010: E/AndroidRuntime(733):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
11-14 22:54:27.010: E/AndroidRuntime(733):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
11-14 22:54:27.010: E/AndroidRuntime(733):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
11-14 22:54:27.010: E/AndroidRuntime(733):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11-14 22:54:27.010: E/AndroidRuntime(733):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
11-14 22:54:27.010: E/AndroidRuntime(733):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
11-14 22:54:27.010: E/AndroidRuntime(733):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
11-14 22:54:27.010: E/AndroidRuntime(733):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
11-14 22:54:27.010: E/AndroidRuntime(733):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
11-14 22:54:27.010: E/AndroidRuntime(733):  at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
11-14 22:54:27.010: E/AndroidRuntime(733):  at com.example.recycletelaviv.ChooseRecycle.csvToGeoPoints(ChooseRecycle.java:126)
11-14 22:54:27.010: E/AndroidRuntime(733):  at com.example.recycletelaviv.ChooseRecycle.onCreate(ChooseRecycle.java:81)
11-14 22:54:27.010: E/AndroidRuntime(733):  at android.app.Activity.performCreate(Activity.java:5008)
11-14 22:54:27.010: E/AndroidRuntime(733):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-14 22:54:27.010: E/AndroidRuntime(733):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-14 22:54:27.010: E/AndroidRuntime(733):  ... 11 more
11-14 22:54:46.440: I/Process(733): Sending signal. PID: 733 SIG: 9

为何会出现这种情况的想法?

非常感谢

1 个答案:

答案 0 :(得分:0)

您无法在UI线程上执行网络IO,这需要太长时间并且会使手机无响应。您需要在另一个线程中执行此操作,通常是AsyncTask