【问题标题】:Cannot convert google maps poly line values into string to decode it无法将谷歌地图折线值转换为字符串以对其进行解码
【发布时间】:2016-03-15 19:19:20
【问题描述】:

我可以从谷歌方向 api 以编码形式检索折线 values(JSONObject values) 以解码此值我需要将折线 JSONObject 值转换为字符串。

当我使用 .toString() 方法时,它会抛出类似的异常,

03-16 00:22:08.014 21481-22032/com.ats.routemapdemo W/System.err: org.json.JSONException: Value czmbAml`uM@\?F@B@B@@ at points of type java.lang.String cannot be converted to JSONObject
03-16 00:22:08.014 21481-22032/com.ats.routemapdemo W/System.err:     at org.json.JSON.typeMismatch(JSON.java:100)
03-16 00:22:08.014 21481-22032/com.ats.routemapdemo W/System.err:     at org.json.JSONObject.getJSONObject(JSONObject.java:578)
03-16 00:22:08.014 21481-22032/com.ats.routemapdemo W/System.err:     at com.ats.routemapdemo.DirectionsJSONParser.parse(DirectionsJSONParser.java:36)
03-16 00:22:08.014 21481-22032/com.ats.routemapdemo W/System.err:     at com.ats.routemapdemo.MapsActivity$JSONtask.doInBackground(MapsActivity.java:146)
03-16 00:22:08.014 21481-22032/com.ats.routemapdemo W/System.err:     at com.ats.routemapdemo.MapsActivity$JSONtask.doInBackground(MapsActivity.java:117)
03-16 00:22:08.014 21481-22032/com.ats.routemapdemo W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-16 00:22:08.014 21481-22032/com.ats.routemapdemo W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-16 00:22:08.024 21481-22032/com.ats.routemapdemo W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-16 00:22:08.024 21481-22032/com.ats.routemapdemo W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-16 00:22:08.024 21481-22032/com.ats.routemapdemo W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-16 00:22:08.024 21481-22032/com.ats.routemapdemo W/System.err:     at java.lang.Thread.run(Thread.java:841)

请帮助将其转换为字符串值。

【问题讨论】:

标签: google-maps google-directory-api


【解决方案1】:

将这些添加到您的项目中

private class draw_ConnectAsyncTask extends AsyncTask<Void, Void, String>
{
    String url;

    draw_ConnectAsyncTask(String urlPass) {
        url = urlPass;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(Void... params) {
        JSONParser jParser = new JSONParser();
        String json = jParser.getJSONFromUrl(url);
        return json;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        if (result != null) {
            drawPath(result);
        }
    }

JSONParser.java

import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    // constructor
    public JSONParser() {
    }
    public String getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            //decoding from bytes to char using "iso-8859-1"
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            json = sb.toString();
            is.close();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        return json;

    }
}

并添加这些方法

public String makeURL(double sourcelat, double sourcelog, double destlat, double destlog)
{
    //make the url that will be passed to the JSONParser
    StringBuilder urlString = new StringBuilder();
    urlString.append("http://maps.googleapis.com/maps/api/directions/json");
    urlString.append("?origin=");// from
    urlString.append(Double.toString(sourcelat));
    urlString.append(",");
    urlString.append(Double.toString(sourcelog));
    urlString.append("&destination=");// to
    urlString.append(Double.toString(destlat));
    urlString.append(",");
    urlString.append(Double.toString(destlog));
    urlString.append("&sensor=false&mode=driving&alternatives=false");
    return urlString.toString();
}


public void drawPath(String result)
    {
        try {
            //Transform the string into a json object
            final JSONObject json = new JSONObject(result);
            //retrieving and decoding the polylineInitial
            JSONArray routeArray = json.getJSONArray("routes");
            JSONObject routes = routeArray.getJSONObject(0);
            JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
            String encodedString = overviewPolylines.getString("points");
            List<LatLng> list = decodePoly(encodedString);

            rectLine = new PolylineOptions().width(8).color(
                    Color.BLUE);


            //drawing the polyline by traversing all the LatLang the the list
            for (int z = 0; z < list.size(); z++) {
                rectLine.add(list.get(z));
            }

            polyline = map.addPolyline(rectLine);


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private List<LatLng> decodePoly(String encoded)
    {

        //translating the encoded string to a list we can understand in android map
        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;
    }

然后像这样调用 AsynTask

draw_ConnectAsyncTask(makeURL(double,double,double,double));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多