Android:从Mysql数据库中检索折线

时间:2014-12-15 06:36:29

标签: mysql google-maps decode polyline

我想从数据库中检索折线路径,但是当我运行应用程序时,log cat总是说它是StringIndexOutOfbound。我看到解码折线过程中的错误。这是我的代码段:

private class RetriveTask extends AsyncTask<Void, Void, String>{

        @Override
        protected String doInBackground(Void... params) {
            // TODO Auto-generated method stub
            String strUrl = "http://my IP Adress/ta/1104505001/android/saved/retrieve.php";
            URL url = null;
            StringBuffer sb = new StringBuffer();
            try{
                url = new URL(strUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.connect();
                InputStream iStream = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));
                String line="";
                while((line=reader.readLine())!=null){
                    sb.append(line);
                }
                reader.close();
                iStream.close();
            }catch(MalformedURLException e){
                e.printStackTrace();
            }catch(IOException e){
                e.printStackTrace();
            }
            return sb.toString();
        }

        protected void onPostExecute(String result){
            super.onPostExecute(result);
            new ParserTask().execute(result);
        }
    }

    private class ParserTask extends AsyncTask<String, Void, List<HashMap<String,String>>>{

        @Override
        protected List<HashMap<String, String>> doInBackground(String... jsonData) {
            // TODO Auto-generated method stub
            PolylineJSONParser polyParser = new PolylineJSONParser();
            JSONObject json=null;

            try{
                json = new JSONObject(jsonData[0]);


            }catch(JSONException e){
                e.printStackTrace();
            }
            List<HashMap<String,String>>routes = polyParser.parse(json);
            return routes;
        }

        @Override
        protected void onPostExecute(List<HashMap<String,String>>routes){
            ArrayList<String> d = new ArrayList<String>();
            List<LatLng> poly;
            List<List<LatLng>> f = new ArrayList<List<LatLng>>();
            for(int i=0;i<routes.size();i++){
                String j = routes.get(i).get("path");
                d.add(j);
            }

            for(int j=0;j<d.size();j++){
                poly = new ArrayList<LatLng>();
                String k = d.get(j);
                poly = decodePoly(k);
                f.add(poly);
                Toast.makeText(MainActivity.this, "berhasil", Toast.LENGTH_SHORT).show();
            }
            /*ArrayList<LatLng> point1 = null;
            PolylineOptions polyLineOptions = null;
            for(int i = 0; i < routes.size(); i++){
                point1=new ArrayList<LatLng>();
                polyLineOptions = new PolylineOptions();
                List<HashMap<String,String>> path = routes.get(i);

                for(int a=0;a<path.size();a++){
                    HashMap<String,String> point = path.get(a);

                    double lat = Double.parseDouble(point.get("lat"));
                    double lng = Double.parseDouble(point.get("lng"));
                    LatLng posisi = new LatLng(lat,lng);

                    point1.add(posisi);
                }
                polyLineOptions.addAll(point1);
                polyLineOptions.width(2);
                polyLineOptions.color(Color.BLUE);
            }
            map.addPolyline(polyLineOptions);*/
        }
    }

    private List<LatLng> decodePoly(String encoded) {

        List<LatLng> poly = new ArrayList<LatLng>();
        int index = 0, len = encoded.length();
        int lat = 0, lng = 0;

        while (index < len) {
            int b, shift = 0, result = 0;
            do {
                b = encoded.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 = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng;

            LatLng p = new LatLng((((double) lat / 1E5)),
                    (((double) lng / 1E5)));
            poly.add(p);
        }
        return poly;
    }

这是JSONParser类

public class PolylineJSONParser {
public List<HashMap<String,String>> parse(JSONObject jObject){
    JSONArray jMarkers = null;
    try{
        jMarkers = jObject.getJSONArray("polyline");
    }catch(JSONException e){
        e.printStackTrace();
    }
    return getMarkers(jMarkers);

}

private List<HashMap<String, String>> getMarkers(JSONArray jMarkers){
    int markersCount = jMarkers.length();
    List<HashMap<String,String>> markersList = new ArrayList<HashMap<String,String>>();
    HashMap<String, String> marker = null;

    for(int i=0; i<markersCount;i++){
        try{
            marker = getMarker((JSONObject)jMarkers.get(i));
            markersList.add(marker);
        }catch(JSONException e){
            e.printStackTrace();
        }
    }
    return markersList;

}

private HashMap<String,String> getMarker(JSONObject jMarker){
    HashMap<String,String> marker = new HashMap<String,String>();
    String path = "-NA-";

    try{
        if(!jMarker.isNull("path")){
            path=jMarker.getString("path");
        }

        marker.put("path", path);
    }catch(JSONException e){
        e.printStackTrace();
    }
    return marker;

}

}

这是日志猫说的:

12-15 13:51:58.270: E/AndroidRuntime(26844): FATAL EXCEPTION: main
12-15 13:51:58.270: E/AndroidRuntime(26844): java.lang.StringIndexOutOfBoundsException: length=23; index=23
12-15 13:51:58.270: E/AndroidRuntime(26844):    at com.evy.MainActivity.decodePoly(MainActivity.java:262)
12-15 13:51:58.270: E/AndroidRuntime(26844):    at com.evy.MainActivity.access$0(MainActivity.java:243)
12-15 13:51:58.270: E/AndroidRuntime(26844):    at com.evy.MainActivity$ParserTask.onPostExecute(MainActivity.java:215)
12-15 13:51:58.270: E/AndroidRuntime(26844):    at com.evy.MainActivity$ParserTask.onPostExecute(MainActivity.java:1)
12-15 13:51:58.270: E/AndroidRuntime(26844):    at android.os.AsyncTask.finish(AsyncTask.java:631)
12-15 13:51:58.270: E/AndroidRuntime(26844):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-15 13:51:58.270: E/AndroidRuntime(26844):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-15 13:51:58.270: E/AndroidRuntime(26844):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-15 13:51:58.270: E/AndroidRuntime(26844):    at android.os.Looper.loop(Looper.java:176)
12-15 13:51:58.270: E/AndroidRuntime(26844):    at android.app.ActivityThread.main(ActivityThread.java:5419)
12-15 13:51:58.270: E/AndroidRuntime(26844):    at java.lang.reflect.Method.invokeNative(Native Method)
12-15 13:51:58.270: E/AndroidRuntime(26844):    at java.lang.reflect.Method.invoke(Method.java:525)
12-15 13:51:58.270: E/AndroidRuntime(26844):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
12-15 13:51:58.270: E/AndroidRuntime(26844):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
12-15 13:51:58.270: E/AndroidRuntime(26844):    at dalvik.system.NativeStart.main(Native Method)

我该怎么办?

1 个答案:

答案 0 :(得分:0)

在你的while循环中尝试更改索引&lt; len到索引&lt; LEN + 1。 希望能解决String out of bound问题。