0

I need to collect gps point from a csv and create a list of geopoints so I'll be able to add them as overlays on my map. So I've come up with this piece of code that collects all the relevant points according to the type of garbage to recycle. I'm trying to load this up and errors come up (or if I'm wrong and the errors are not related to it, please tell me):

Please consider recycleType = 3 (batteries).

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;

}

The error messages I get are:

    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

Any ideas of why this happens?

Thanks so much

devdc
  • 161
  • 1
  • 4
  • 13

1 Answers1

0

You can't do network IO on the UI thread, it takes too long and would make the phone unresponsive. You need to do it in another thread, generally an AsyncTask

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I checked AsyncTask which seems to be helpful but what about the fact that the code is not even running? – devdc Nov 14 '12 at 22:34
  • It won't. You can't call any of the http functions that actually send data tot he service on the main thread. You *have* to do it on another thread or it will throw an exception. – Gabe Sechan Nov 15 '12 at 01:47