JsonObjectRequest 不为空时一直抛出空指针异常
JsonObjectRequest keeps throwing null pointer exception when it's not null
我目前正在尝试实施 JsonObjectRequest。我正在寻找一个位置并尝试获取该地点的信息。但是,jsObjRequest(类型为 JsonObjectRequest)存在一个问题——它抛出一个空指针异常,即使当我检查监视列表时它不是空的。我对为什么感到困惑。这是代码:
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
private static final String API_KEY = "AIzaSyBlOhVQWuyQGwlAGZnDo81Aeg50UJbFrfw";
private static final String PLACES_SEARCH_URL =
"https://maps.googleapis.com/maps/api/place/search/json?";
private static final String PLACES_TEXT_SEARCH_URL =
"https://maps.googleapis.com/maps/api/place/search/json?";
private static final String PLACES_DETAILS_URL =
"https://maps.googleapis.com/maps/api/place/details/json?";
private double _latitude;
private double _longitude;
private double _radius;
/**
* Searching places
* @param latitude - latitude of place
* @params longitude - longitude of place
* @param radius - radius of searchable area
* //@param types - type of place to search
* @return list of places
* */
public PlacesList search(double latitude, double longitude, double radius/*, String types*/)
throws Exception {
String url = PLACES_SEARCH_URL + "key=" + API_KEY + "&location=" + latitude + "," + longitude + "&radius=" + radius;
System.out.println(url);
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d("SEARCH", "Part 1");
VolleyLog.v("Response:%n %s", response.toString(4));
} catch (JSONException e) {
Log.d("SEARCH", "Part 2 - Exception called");
e.printStackTrace();
}
catch (Exception e)
{
System.out.println("Exception caught in JsonObjectRequest");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("SEARCH", "Part 3");
VolleyLog.e("Error: ", error.getMessage());
}
});
ApplicationController.getInstance().addToRequestQueue(jsObjRequest);
return new PlacesList();
}
它抛出包含以下内容的行:ApplicationController.getInstance().addToRequestQueue(jsObjRequest);
下面的代码块是我们捕获异常的地方:
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
Double lat = location.getLatitude();
Double lon = location.getLongitude();
PlacesList myPlaces = new PlacesList();
try {
myPlaces = finder.search(lat, lon, 10);
} catch (Exception e) {
System.out.println("Could not find place. Exception in search.");
}
if (myPlaces.getMostLikelyPlace() != null) {
if (myPlaces.getMostLikelyPlace().getId() != null) {
Log.d("PLACEID", myPlaces.getMostLikelyPlace().getId());
System.out.println(myPlaces.getMostLikelyPlace().getId());
}
}
}
我们捕获的错误如下所示:
I/System.out: 找不到位置。搜索异常。
以下是文档中关于 JsonObjectRequest 的内容:https://developer.android.com/training/volley/request.html#request-json
我觉得我完全按照文档中的说明进行操作。有什么原因可以解释为什么它会抛出那行代码吗?
试试这个排球设置..
http://www.androidhive.info/2014/05/android-working-with-volley-library-1/
也许项目的截击设置出了点问题..我一直都在发生..
我目前正在尝试实施 JsonObjectRequest。我正在寻找一个位置并尝试获取该地点的信息。但是,jsObjRequest(类型为 JsonObjectRequest)存在一个问题——它抛出一个空指针异常,即使当我检查监视列表时它不是空的。我对为什么感到困惑。这是代码:
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
private static final String API_KEY = "AIzaSyBlOhVQWuyQGwlAGZnDo81Aeg50UJbFrfw";
private static final String PLACES_SEARCH_URL =
"https://maps.googleapis.com/maps/api/place/search/json?";
private static final String PLACES_TEXT_SEARCH_URL =
"https://maps.googleapis.com/maps/api/place/search/json?";
private static final String PLACES_DETAILS_URL =
"https://maps.googleapis.com/maps/api/place/details/json?";
private double _latitude;
private double _longitude;
private double _radius;
/**
* Searching places
* @param latitude - latitude of place
* @params longitude - longitude of place
* @param radius - radius of searchable area
* //@param types - type of place to search
* @return list of places
* */
public PlacesList search(double latitude, double longitude, double radius/*, String types*/)
throws Exception {
String url = PLACES_SEARCH_URL + "key=" + API_KEY + "&location=" + latitude + "," + longitude + "&radius=" + radius;
System.out.println(url);
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d("SEARCH", "Part 1");
VolleyLog.v("Response:%n %s", response.toString(4));
} catch (JSONException e) {
Log.d("SEARCH", "Part 2 - Exception called");
e.printStackTrace();
}
catch (Exception e)
{
System.out.println("Exception caught in JsonObjectRequest");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("SEARCH", "Part 3");
VolleyLog.e("Error: ", error.getMessage());
}
});
ApplicationController.getInstance().addToRequestQueue(jsObjRequest);
return new PlacesList();
}
它抛出包含以下内容的行:ApplicationController.getInstance().addToRequestQueue(jsObjRequest);
下面的代码块是我们捕获异常的地方:
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
Double lat = location.getLatitude();
Double lon = location.getLongitude();
PlacesList myPlaces = new PlacesList();
try {
myPlaces = finder.search(lat, lon, 10);
} catch (Exception e) {
System.out.println("Could not find place. Exception in search.");
}
if (myPlaces.getMostLikelyPlace() != null) {
if (myPlaces.getMostLikelyPlace().getId() != null) {
Log.d("PLACEID", myPlaces.getMostLikelyPlace().getId());
System.out.println(myPlaces.getMostLikelyPlace().getId());
}
}
}
我们捕获的错误如下所示:
I/System.out: 找不到位置。搜索异常。
以下是文档中关于 JsonObjectRequest 的内容:https://developer.android.com/training/volley/request.html#request-json
我觉得我完全按照文档中的说明进行操作。有什么原因可以解释为什么它会抛出那行代码吗?
试试这个排球设置..
http://www.androidhive.info/2014/05/android-working-with-volley-library-1/
也许项目的截击设置出了点问题..我一直都在发生..