我使用谷歌的距離矩陣API來獲取locations.So之間的距離我用這個查詢:httpclient.execute(httppost)拋出異常
String distanceofloc="https://maps.googleapis.com/maps/api/distancematrix/json?origins="+mylat+","+mylng+"&destinations="+placeloc+"&key=MY_BROWSER_KEY";
那時,我便用這個字符串爲獲得距離的函數:
String dis=setDistance(distanceofloc);
這裏是我的setDistance功能:
public String setDistance(String url) throws URISyntaxException{
String dis=null;
StringBuilder placesBuilder = new StringBuilder();
//execute search
HttpClient placesClient = new DefaultHttpClient();
HttpResponse placesResponse=null;
//try to fetch the data
try{
HttpPost placesGet = new HttpPost(url);
try{
placesResponse = placesClient.execute(placesGet);
}catch(Exception e){
e.printStackTrace();
Log.v("PLACES", "exception thrown");
}
StatusLine placeSearchStatus = placesResponse.getStatusLine();
if (placeSearchStatus.getStatusCode() == 200) {
//we have an OK response
HttpEntity placesEntity = placesResponse.getEntity();
InputStream placesContent = placesEntity.getContent();
//create reader
InputStreamReader placesInput = new InputStreamReader(placesContent);
//use buffered reader to process
BufferedReader placesReader = new BufferedReader(placesInput);
//read a line at a time, append to string builder
String lineIn;
while ((lineIn = placesReader.readLine()) != null) {
placesBuilder.append(lineIn);
}}}
catch(Exception e){
e.printStackTrace();
Log.v("PLACES", "missing value");
}
try {
//parse JSON
//create JSONObject, pass stinrg returned from doInBackground
JSONObject distanceJSONObject = new JSONObject(placesBuilder.toString());
JSONArray rowArray = distanceJSONObject.getJSONArray("rows");
JSONObject rowsObject = rowArray.getJSONObject(0);//only one element in this array
JSONArray elementsArray = rowsObject.getJSONArray("elements");
JSONObject elementsObject = elementsArray.getJSONObject(0);//only one element in this array
if(elementsObject.isNull("distance")){
}
else{
JSONObject distanceObject = elementsObject.getJSONObject("distance");
if(!distanceObject.isNull("text"))
{
dis=distanceObject.getString("text"
}
}
}catch (JSONException e) {
e.printStackTrace();
}
return dis;
}
這裏,異常是在
HttpPost placesGet = new HttpPost(url);
try{
placesResponse = placesClient.execute(placesGet);
}catch(Exception e){
e.printStackTrace();
Log.v("PLACES", "exception thrown");
}
拋出誰能告訴我,如果事情是錯的網址是什麼?任何幫助表示讚賞。
請發佈您的logcat – DroidDev 2014-09-24 13:03:28