2012-11-14 10 views
0

CSV에서 GPS 포인트를 수집하고 지오 포인트 목록을 만들어서지도에 오버레이로 추가 할 수있게해야합니다. 따라서 재활용 할 쓰레기 유형에 따라 모든 관련 사항을 수집하는 코드 조각을 생각해 냈습니다. 오류가 발생하여 오류가 발생했거나 오류가 발생하지 않았다면 알려주세요.csvToGeoPoints를로드 할 때 오류가 발생했습니다.

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 

어떤 아이디어? 당신은 UI 스레드에서 네트워크 IO 할 수 없습니다

답변

0

너무 많은

덕분에, 그것은 너무 오래 걸리고 전화가 응답 할 것입니다. 다른 스레드, 일반적으로 AsyncTask에서 수행해야합니다.

+0

좀 더 구체적으로 설명 할 수 있습니까? – devdc

+0

도움이 될 것 같은 AsyncTask를 선택했지만 코드가 실행되지 않는다는 사실은 무엇입니까? – devdc

+0

그렇지 않습니다. 주 스레드에서 서비스하는 데이터를 실제로 보내는 모든 http 함수를 호출 할 수 없습니다. 다른 스레드에서이 작업을 수행해야하거나 예외가 발생합니다. –

관련 문제