2012-02-12 5 views
1

Google지도에 대한 URL 요청에 2 개의 우편 번호가 주어진 후 json 응답을 구문 분석하는 앱을 사용했습니다. 응답에서 overviewpolyline 노드를 가져 와서 문자열로 캐스팅했습니다. overviewpolyline 노드가 폴리 라인으로 전체 경로라고 가정합니다.StringIndexOutOfBoundsException을주는 폴리 라인 디코더

아래 코드는 폴리 라인을 문자열로 전달할 때 해당 문자열을 GeoPoints 목록으로 변환하는 코드입니다. 폴리 라인 문자열이 비어 있지 않은지 확인하고 2 개의 포스트 코드에 문자열에 700+ 문자가 있는지 확인했습니다. 그래서 아무런 문제가 없습니다.

아래의 코드 소스에서 예외가 발생한 곳을 표시했습니다. 인덱스 오류가있는 이유는 무엇입니까? 루핑은 'while'문으로 제어되고 폴리 라인의 길이보다 작을 때만 반복됩니다.

@SuppressWarnings("unchecked") 
    private List decodePolyLine(final String poly) { 



     int len = poly.length(); 
     Log.e(TAG, "poly string length = "+poly.length()); 
     int index = 0; 
     List decoded = new ArrayList(); 
     int lat = 0; 
     int lng = 0; 


     while (index < len) { 

     int b; 
     int shift = 0; 
     int result = 0; 

     do { 

     b = poly.charAt(index++) - 63; 

     result |= (b & 0x1f) << shift; 

     shift += 5; 



    } while (b >= 0x20); 



     int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
     lat += dlat; 
     shift = 0; 
     result = 0; 

     do { 

     b = poly.charAt(index++) - 63;  <--------****error here**** 
     result |= (b & 0x1f) << shift; 
     shift += 5; 

     } while (b >= 0x20); 



     int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
     lng += dlng; 

     decoded.add(new GeoPoint(

       (int)(lat*1e6) , (int)(lon * 1e6))); 

     } 

     return decoded; 

     }//end of decodePolyLine 
+0

overviewpolyline에 중첩 오브젝트 "포인트"가 있음을 발견했습니다. 이제 그걸 보게 될거야. – turtleboy

답변

0

해결. 문제는 위의 코드와 관련이 없으며 오히려 json 응답을 구문 분석하는 방법이었습니다. overviewpolyline 객체 내에서 point 객체를 가져와야했습니다. 예 :

JSONObject results = null; 

     try { 

      results = new JSONObject(jsonOutput); 


      routes = results.getJSONArray("routes"); 

      anonObject = routes.getJSONObject(0); 
      bounds = anonObject.getJSONObject("bounds"); 
      overViewPolyline = anonObject.getJSONObject("overview_polyline"); 
      polyPoints = overViewPolyline.getString("points"); 
      Log.e(TAG,"overview_polyline = " + overViewPolyline); 
      Log.e(TAG,"points = " + polyPoints); 


      northeast = bounds.getJSONObject("northeast"); 


      lat = (Double) northeast.get("lat"); 


      lon = (Double) northeast.get("lng"); 


     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
관련 문제