在我的應用程序中,我使用Google Place Picker來獲取用戶將要前往的位置座標以及Google API方向我在KM中獲取距離,但有時我在日誌中得到此錯誤,沒有得到的距離和時間:Google API Directions在閱讀時發生錯誤JSONObejct
10-21 20:30:23.326 4920-4920/com.ddz.assistenteauto W/System.err: org.json.JSONException: Index 0 out of range [0..0)
10-21 20:30:23.326 4920-4920/com.ddz.assistenteauto W/System.err: at org.json.JSONArray.get(JSONArray.java:293)
10-21 20:30:23.326 4920-4920/com.ddz.assistenteauto W/System.err: at org.json.JSONArray.getJSONObject(JSONArray.java:521)
10-21 20:30:23.326 4920-4920/com.ddz.assistenteauto W/System.err: at com.ddz.assistenteauto.Fragments.TravelFragment$4.onResponse(TravelFragment.java:249)
10-21 20:30:23.326 4920-4920/com.ddz.assistenteauto W/System.err: at com.ddz.assistenteauto.Fragments.TravelFragment$4.onResponse(TravelFragment.java:236)
10-21 20:30:23.326 4920-4920/com.ddz.assistenteauto W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
10-21 20:30:23.326 4920-4920/com.ddz.assistenteauto W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
10-21 20:30:23.326 4920-4920/com.ddz.assistenteauto W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
10-21 20:30:23.326 4920-4920/com.ddz.assistenteauto W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
10-21 20:30:23.326 4920-4920/com.ddz.assistenteauto W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
10-21 20:30:23.326 4920-4920/com.ddz.assistenteauto W/System.err: at android.os.Looper.loop(Looper.java:148)
10-21 20:30:23.326 4920-4920/com.ddz.assistenteauto W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7325)
10-21 20:30:23.326 4920-4920/com.ddz.assistenteauto W/System.err: at java.lang.reflect.Method.invoke(Native Method)
10-21 20:30:23.326 4920-4920/com.ddz.assistenteauto W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
10-21 20:30:23.326 4920-4920/com.ddz.assistenteauto W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
在這裏,我如何發送請求,谷歌與HTTP請求:
private void getDistanceTime(TravelInfo obj){
Log.d(TAG, "getDistanceTime()");
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(getActivity());
String url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + obj.getStartLat() + "," + obj.getStartLng() + "&destination=" + obj.getEndLat() + "," + obj.getEndLng() + "&mode=driving&sensor=false";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
//mTextView.setText("Response is: "+ response.substring(0,500));
//Log.i(TAG,"Response is: "+ response);
try {
JSONObject jsonObject = new JSONObject(response);
// routesArray contains ALL routes
JSONArray routesArray = jsonObject.getJSONArray("routes");
// Grab the first route
JSONObject route = routesArray.getJSONObject(0);
// Take all legs from the route
JSONArray legs = route.getJSONArray("legs");
// Grab first leg
JSONObject leg = legs.getJSONObject(0);
//take travel distance
JSONObject distanceObject = leg.getJSONObject("distance");
String distance = distanceObject.getString("text");
//Take travel duration
JSONObject durationObject = leg.getJSONObject("duration");
String duration = durationObject.getString("text");
//Set distance and duration to the class infoTravel that contain coordinates
setDistanceTime(distance, duration);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//mTextView.setText("That didn't work!");
Log.i(TAG,"That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
有人能幫忙嗎?
我可以從PlacePicker獲取用戶位置嗎? 從現在我用這個獲取用戶的位置:
private void getUserLocation() {
Log.i(TAG, "getUserLocation()");
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
String stringStartLat, stringStartLng;
travelInfo.setStartLat(location.getLatitude());
travelInfo.setStartLng(location.getLongitude());
stringStartLat = Double.toString(travelInfo.getStartLat());
stringStartLng = Double.toString(travelInfo.getStartLng());
//Set the TextView in the fragment with start coordinates
startLatView.setText(stringStartLat);
startLngView.setText(stringStartLng);
if (travelInfo.getEndLat() != 0 && travelInfo.getEndLng() != 0) {
Log.i(TAG, "Dentro if userlocation");
getDistanceTime(travelInfo);
}
}
}
});
}
編輯
當我得到的JSON的錯誤,我收到來自谷歌API這個JSON
10-21 20:54:18.965 22814-22814/com.ddz.assistenteauto I/TravelFragment: Response is: {
"error_message" : "You have exceeded your daily request quota for this API. We recommend registering for a key at the Google Developers Console: https://console.developers.google.com/apis/credentials?project=_",
"routes" : [],
"status" : "OVER_QUERY_LIMIT"
}
編輯
我添加網址的關鍵但仍然出現錯誤:
String url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + obj.getStartLat() + "," + obj.getStartLng() + "&destination=" + obj.getEndLat() + "," + obj.getEndLng() + "&mode=driving&AIza*****************FI";
EDIT 2現在的關鍵我得到這個錯誤:
10-22 15:24:35.540 27362-27362/com.ddz.assistenteauto I/TravelFragment: Response is: {
"error_message" : "Requests to this API must be over SSL. Load the API with \"https://\" instead of \"http://\".",
"routes" : [],
"status" : "REQUEST_DENIED"
所以我的HTTP更改爲https ......,讓我們看看 }
在上次更新中,您看起來像缺少一個'&key =',它應該是'&mode = driving&key = AIza ***************** FI'而不是'&mode =駕駛&AIza ***************** FI' – xomena
你是對的!謝謝....看看現在是否工作:) – Dario