1

我正在使用适用于 Android 的 Google Directions API 并获得 JSON 响应。唯一的问题是响应恰好在 15422 个字符之后停止接收(即响应恰好在第 4 步中间停止)。我尝试改变缓冲区大小,但它仍然是相同的。我永远无法收到完整的回复。知道为什么会这样吗?

[编辑] 这是请求方向的代码。

Location currentLocation;
String urlShowDirections = ""; 
if(currentLocation != null){
    urlShowDirections = "http://maps.googleapis.com/maps/api/directions/json?"+
                        "origin="+currentLocation.getLatitude()+","+currentLocation.getLongitude()+
                        "&destination="+nearestAtmLL.latitude+","+nearestAtmLL.longitude+
                        "&sensor=false";
}
URL url = new URL(urlShowDirections);
new LocateDirections().execute(url);

这是在后台处理请求的 AsyncTask。

class LocateDirections extends AsyncTask<URL, Void, Void>{

    @Override
    protected Void doInBackground(URL... url) {
    HttpURLConnection conn = null;
        final StringBuilder json = new StringBuilder();

        try{
            //connect to web service
            conn = (HttpURLConnection) url[0].openConnection();
            Log.i(TAG, "Connection - "+conn.toString());

            InputStreamReader in = new InputStreamReader(conn.getInputStream());

            //read json data into string builder
            int read;
            char[] buff = new char[4096];
            while ((read = in.read(buff)) != -1) {
                json.append(buff, 0, read);
                Log.i(TAG, "Response char total - "+json.length());
                Log.i(TAG, "Chars read - "+read);
            }

            Log.i(TAG, "Directions Response - "+json.toString());

        } catch (IOException e) {
            Log.e(TAG, "IO Error - Error in trying to establish a connnection");
            e.printStackTrace();
        } catch (JSONException e) {
            Log.e(TAG, "JSON Error - Error in parsing json response");
            e.printStackTrace();
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
        return null;
    }
}

[编辑 2] 我检查了具有不同来源和目的地的应用程序。这一次,收到的字符没有不同。但是不同缓冲区大小的多次尝试表明响应完全停止在同一位置,就像以前一样。

4

0 回答 0