2016-11-22 110 views
0

我目前正試圖實現JsonObjectRequest。我正在尋找一個位置並試圖獲取該地點的信息。但是,jsObjRequest(JsonObjectRequest類型)存在問題 - 即使它在檢查監視列表時不爲null,也會拋出空指針異常。我很困惑,爲什麼。下面的代碼: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的:https://maps.googleapis.com/maps/api/place/search/json?key=AIzaSyBlOhVQWuyQGwlAGZnDo81Aeg50UJbFrfw&location=43.816,-111.78199833333335&radius=10.0

I/System.out:找不到地方。搜索異常。

這裏是什麼文件說,大約JsonObjectRequest:https://developer.android.com/training/volley/request.html#request-json

我覺得我下面的文檔上的說明完全一樣。是否有任何理由爲什麼它會拋出該代碼行?

回答

0

嘗試這種設置對於凌空..

http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

也許出了點問題抽射設置的項目。與我所有的時間發生..

+0

如果抽射設置做正確的並正在爲其他東西工作..讓我知道.. – Azi

+0

安裝程序的作品,我沒有得到相同的錯誤,但我在onErrorResponse VolleyError。當我嘗試使用VolleyLog.e(「Error:」,error.getMessage())打印錯誤消息時,它告訴我什麼都沒有。它所說的是「E/Volley:[1] 2.onErrorResponse:Error:」。任何有關爲什麼這可能會發生的見解?非常感謝您的參與。 –