我正在使用适用于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] 我检查了不同来源和目的地的应用程序。这次,收到的字符不同。但是,使用不同缓冲区大小的多次尝试表明响应完全停止在同一位置,就像之前一样。