我正在尝试从服务器应用程序调用位置回调,但出现此错误:
尝试调用虚拟方法'java.lang.String java.lang.String.toString()',我的toString返回null。无法解析Json响应字符串以获取消息:崩溃没有值。
我正在使用JSONObject,而我的错误则显示在此行中
JSONObject jsonObject = new JSONObject(response.body().toString());
Tracking.java
public class Tracking {
private GoogleMap mMap;
FusedLocationProviderClient fusedLocationProviderClient;
LocationCallback locationCallback;
LocationRequest locationRequest;
Location mLastLocation;
Marker mCurrentMarker;
Polyline polyline;
IGeoCoordinates mService;
FButton btn_call, btn_shipped;
private void drawRoute(final LatLng yourLocation, Request request) {
if (polyline != null)
polyline.remove();
if (request.getAddress() != null && !request.getAddress().isEmpty())
{
mService.getGeoCode(request.getAddress()).enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
try {
//JSONObject jsonObject = new JSONObject(response.body().toString());
JSONObject jsonObject = new JSONObject(response.body().toString());
String lat = ((JSONArray)jsonObject.get("results"))
.getJSONObject(0)
.getJSONObject("geometry")
.getJSONObject("location")
.get("lat").toString();
String lng = ((JSONArray)jsonObject.get("results"))
.getJSONObject(0)
.getJSONObject("geometry")
.getJSONObject("location")
.get("lng").toString();
LatLng orderLocation = new LatLng(Double.parseDouble(lat),Double.parseDouble(lng));
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.box);
bitmap = Common.scaleBitmap(bitmap,70,70);
MarkerOptions marker = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(bitmap))
.title("Orden de "+Common.currentRequest.getPhone())
.position(orderLocation);
mMap.addMarker(marker);
//Drwa Route
mService.getDirections(yourLocation.latitude+","+yourLocation.longitude,
orderLocation.latitude+","+orderLocation.longitude)
.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
new ParseTask().execute(response.body().toString());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
}
else
{
if (request.getLatLng() != null && !request.getLatLng().isEmpty())
{
String[] latLng = request.getLatLng().split(",");
LatLng orderLocation = new LatLng(Double.parseDouble(latLng[0]),Double.parseDouble(latLng[1]));
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.box);
bitmap = Common.scaleBitmap(bitmap,70,70);
MarkerOptions marker = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(bitmap))
.title("Orden de "+Common.currentRequest.getPhone())
.position(orderLocation);
mMap.addMarker(marker);
mService.getDirections(mLastLocation.getLatitude()+","+mLastLocation.getLongitude(),
orderLocation.latitude+","+orderLocation.longitude)
.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
new ParseTask().execute(response.body().toString());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
}
}
}
}
DirectionsJSONParser.java
public class DirectionJSONParser {
//This class you can copy code from link
//Because this is public class to decode poly lines on Internet
/**
* Receives a JSONObject and returns a list of lists containing latitude and longitude
*/
public List<List<HashMap<String, String>>> parse(JSONObject jObject) {
List<List<HashMap<String, String>>> routes = new ArrayList<List<HashMap<String, String>>>();
JSONArray jRoutes = null;
JSONArray jLegs = null;
JSONArray jSteps = null;
try {
jRoutes = jObject.getJSONArray("routes");
/** Traversing all routes */
for (int i = 0; i < jRoutes.length(); i++) {
jLegs = ((JSONObject) jRoutes.get(i)).getJSONArray("legs");
List path = new ArrayList<HashMap<String, String>>();
/** Traversing all legs */
for (int j = 0; j < jLegs.length(); j++) {
jSteps = ((JSONObject) jLegs.get(j)).getJSONArray("steps");
/** Traversing all steps */
for (int k = 0; k < jSteps.length(); k++) {
String polyline = "";
polyline = (String) ((JSONObject) ((JSONObject) jSteps.get(k)).get("polyline")).get("points");
List list = decodePoly(polyline);
/** Traversing all points */
for (int l = 0; l < list.size(); l++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("lat", Double.toString(((LatLng) list.get(l)).latitude));
hm.put("lng", Double.toString(((LatLng) list.get(l)).longitude));
path.add(hm);
}
}
routes.add(path);
}
}
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
}
return routes;
}
/**
* Method to decode polyline points
* Courtesy : http://jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java
*/
private List decodePoly(String encoded) {
List poly = new ArrayList();
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;
}
}