我一直在Android Studio中编写Android应用程序,目的是从Google Directions API获取JSONObject(使用Volley库),然后将其传递给新活动(然后只打印它) ,以便我可以在解析之前进行调试)。但是,通过ADB在我的手机上运行应用程序LogCat returns this。
通过提交按钮调用onClick(应用程序崩溃时)的方法是:
public void submit(View view) {
Location location = LocationServices.FusedLocationApi.getLastLocation(locationClient);
String destination = findViewById(R.id.destinationInput).toString();
String url = "https://maps.googleapis.com/maps/api/directions/json?origin="
+ location.getLatitude()
+ location.getLongitude()
+ "&destination=" + destination
+ "&mode=driving"
+ avoids()
+ "departure_time=now"
+ "&key=" + getString(R.string.API_KEY);
RequestQueue requestQueue;
// Instantiate the cache
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap
// Set up the network to use HttpURLConnection as the HTTP client.
Network network = new BasicNetwork(new HurlStack());
// Instantiate the RequestQueue with the cache and network.
requestQueue = new RequestQueue(cache, network);
// Start the queue
requestQueue.start();
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject dirs) {
Intent intent;
intent = new Intent(getApplicationContext(), DisplayDirections.class);
intent.putExtra("DIRECTIONS", dirs.toString());
startActivity(intent);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
// Add the request to the RequestQueue.
requestQueue.add(jsObjRequest);
}
private String avoids() {
String avoid = "avoid=";
boolean any = false;
if(((Switch) findViewById(R.id.avoidTollsSwitch)).isChecked()) {
avoid += "tolls";
any = true;
}
if(((Switch) findViewById(R.id.avoidMotorwaysSwitch)).isChecked()) {
if(any) avoid += "|";
avoid += "highways";
any = true;
}
if(((Switch) findViewById(R.id.avoidFerriesSwitch)).isChecked()) {
if(any) avoid += "|";
avoid += "ferries";
any = true;
}
if(any) return avoid;
else return "";
}
LogCat日志链接到字符串url =,但我对此行的错误感到困惑。据我所知,我的代码应该有用......
排球教程:https://developer.android.com/training/volley/simple.html
答案 0 :(得分:0)
您确定location
不为空吗?在String url =
行放置一个断点,看看它的值是什么。
答案 1 :(得分:0)
如果您在模拟器上运行它,则需要将坐标发送到模拟器。否则location
将为空。
我第一次使用地理定位服务的时候,我正在把头发拉过来。
只需检查
if (location!=null) {
foo.doSomethingWithLocation(bar);
} else {
Log.d(TAG, "Sorry. Location is null");
}
在你打电话之前