【发布时间】:2015-03-26 07:42:43
【问题描述】:
这是我的 getCurrentLocation 方法:
private CurrentLocation getCurrentLocation(String jsonData) throws JSONException {
JSONObject object = new JSONObject(jsonData);
JSONArray results = object.getJSONArray("results");
JSONObject objects = results.getJSONObject(0);
JSONObject geometry = objects.getJSONObject("geometry");
JSONObject location = geometry.getJSONObject("location");
//instance of CurrentLocation class
CurrentLocation currentLocation = new CurrentLocation();
currentLocation.setLongitude(location.getDouble("lat"));
currentLocation.setLatitude(location.getDouble("lng"));
Log.i(TAG, "FROM JSON: " + currentLocation.getLatitude());
Log.i(TAG, "FROM JSON: " + currentLocation.getLongitude());
return currentLocation;
}
从 JSON 中打印出来:-117.9145036 FROM JSON: 33.8352932 这是正确的,
我得到一个 NullPointerException 的错误就行了:
private void updateLocation() {
getForecast(mCurrentLocation.getLatitude(),mCurrentLocation.getLongitude());
}
我知道 private void getForecast(double latitude, double longitude) 有效,因为我已经输入了两个双精度参数并且它有效。 我对 android 还是很陌生,我知道工作室本身有一个地理定位器,但我想通过 API 来做。我不确定 nullException 指向什么,任何帮助将不胜感激。谢谢。
如果有帮助,这里是我的其他方法: 创建时:
This is my Oncreate method:
public void onClick(View view) {
String name = mNameField.getText().toString();
getLocation(name);
updateLocation(); // error here?
和获取位置:
public void getLocation(String name) {
//get Google api
String geoLocatingUrl = "https://maps.googleapis.com/maps/api/geocode/json?address="+name+"&key=AIzaSyAsJJIEeB952KpjpoFoMuYHd6gev85uzYs";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(geoLocatingUrl)
.build();
Call call = client.newCall(request);
//UI thread executes enqueue, sends to OKHttp Library gets data, response or failure back to UI
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
try {
String jsonData = response.body().string();
if (response.isSuccessful()) {
mCurrentLocation = getCurrentLocation(jsonData);
Log.v(TAG, jsonData);
} else {
alertUserAboutError();
}
} catch (IOException e) {
Log.e(TAG, "Exception caught: ", e);
}
catch (JSONException e) {
}
}
});
}
和getForecast:
private void getForecast(double latitude, double longitude) {
String apiKey = "//myapikey";
String forecastUrl = "https://api.forecast.io/forecast/" + apiKey +
"/" + latitude + "," + longitude;
if (isNetworkAvailable()) { //runs if network is there and available
toggleRefresh();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(forecastUrl)
.build();
Call call = client.newCall(request);
//UI thread executes enqueue, sends to OKHttp Library gets data, response or failure back to UI
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
toggleRefresh();
}
});
alertUserAboutError();
}
@Override
public void onResponse(Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
toggleRefresh();
}
});
try {
String jsonData = response.body().string();
if (response.isSuccessful()) {
mCurrentWeather = getCurrentDetails(jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
updateDisplay();
}
});
} else {
alertUserAboutError();
}
}
catch (IOException e) {
}
catch (JSONException e) {
}
}
});
}
else {
Toast.makeText(this, getString(R.string.
network_unavailable_message),
Toast.LENGTH_LONG
).show();
}
}
【问题讨论】:
-
您正在打印整个对象,请尝试打印特定数据,例如 currentLocation.getLatitude()。
标签: java android api google-maps