Google Places JSON在Android中引发了解析错误

时间:2011-06-03 01:07:53

标签: android json google-maps

我正在编写一个Android应用,它会获取您当前的位置,并使用Google商家信息网络服务返回您附近的商店列表。我能够回来(我认为是)有效的JSON,但是当我使用Android JSONObject库解析JSON时,我得到一个解析错误。

这是我的班级:

public class StoresNearMe {

    private String result = "";  
    private String googleAPIKey = "Omitted for this example"; 
    private String searchRadius = "50";
    private Location myLocation;
    final String TAG = getClass().getSimpleName();  

    public StoresNearMe(Location location){
        myLocation = location;
    }//StoresNearMe

    public String getStoresNearMe(){
        String result = "Nothing";
        result = callGoogleWebService(buildURLForGooglePlaces(myLocation));
        convertJSONtoArray(result);
        return result;
    }//getStoresNearMe


    private String callGoogleWebService(String url){  
            HttpClient httpclient = new DefaultHttpClient();  
            HttpGet request = new HttpGet(url);  
            //request.addHeader("deviceId", deviceId);  
            ResponseHandler<String> handler = new BasicResponseHandler();  
            try {  
                result = httpclient.execute(request, handler);  
            } catch (ClientProtocolException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
            httpclient.getConnectionManager().shutdown();  
            return result;
    }//callWebService

    private String buildURLForGooglePlaces(Location location){
        String baseUrl = "https://maps.googleapis.com/maps/api/place/search/json?";
        String lat = String.valueOf(location.getLatitude());
        String lon = String.valueOf(location.getLongitude());
        String url = baseUrl + "location=" + lat + "," + lon + "&" +
                     "radius=" + searchRadius + "&" + "sensor=false" +
                     "&" + "key=" + googleAPIKey;
        Log.d(TAG,url);
        return url;
    }//buildURLForGooglePlaces

    private void convertJSONtoArray(String rawJSON){
        try {
            JSONObject completeJSONObj = new JSONObject(rawJSON);
            String json = completeJSONObj.toString();
            Log.d(TAG,json);
            JSONObject results = completeJSONObj.getJSONObject("results");
        } catch (JSONException e) {
            Log.d(TAG,"JSON parsing error - fix it:" + e.getMessage());
        }
    }//convertJSONtoArray
}//StoresNearMe

这是我的电话回复的JSON:

{
   "html_attributions" : [
      "Listings by \u003ca href=\"http://www.yellowpages.com.au/\"\u003eYellow Pages\u003c/a\u003e"
   ],
   "results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : -33.86694910,
               "lng" : 151.19549870
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
         "id" : "96760d4544ecdaaf2e87565915638238ca960f20",
         "name" : "Pirrama Rd",
         "reference" : "CnRqAAAAL0xKaK8vGlVlD-E5j-88K9YTuCKlOFfoBxhBUwMIBGxNQDC8UdkULJiiC0ICBRxpddCV09wyc-HtShEpquP-drX4sj4tI_tbfveDIbzfSAUaNQjYhhfjQ-eVi0VeWBXdXH0A8Z_-nKXKMo1fvCHtBhIQlAeLnFtotWnYonMUpU1DqxoUcI1weYioNnJPtnhXCThmx0W0ubU",
         "types" : [ "route" ],
         "vicinity" : "Pyrmont"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : -33.8669710,
               "lng" : 151.1958750
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "4f89212bf76dde31f092cfc14d7506555d85b5c7",
         "name" : "Google Sydney",
         "reference" : "CnRkAAAAqvesgoigCyiZfDUwqjbravZHH5zsXrmskbQVhkFk1JMnnrnKNGQyoHBBYofTML_zZqaAUAKJeJCvReUn62oGzSgPgNSjWaXUVorKGDXAAjhGyeh2pRrJFwuC5qsgBYdyN4kY8kspJGu_bMo3xrranxIQ-mFIK_28TAqiagOAzY7RiBoUrZTvFJYSn0OXimLRRb_SQ3OKW44",
         "types" : [ "establishment" ],
         "vicinity" : "Pirrama Road, Pyrmont"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : -33.8670840,
               "lng" : 151.195950
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "27e84d1acda19c14b9c7f5271242796891bc9c9e",
         "name" : "Accenture",
         "reference" : "CoQBfQAAAOpBriMi1e4itUquHQnv_jQyEOibrEnbWfAuJBJqBUzcTqNvY6luCcgiqHr3ensEO81bcTkt4fXGHFLxM7pERmxVvFnRnLsLy5xyQtVxIp8eQnYP6AZxLZyxbfGceG87r6A1lHCOQTFxK85HVFq7pYik9ud_ExxEtwHT3gO54pihEhDYEZ59AYpLMDmlRHk5__q_GhQdTS2wSOU2sAxy0j4J7EYwCuq6Iw",
         "types" : [ "establishment" ],
         "vicinity" : "Pirrama Road, Pyrmont"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : -33.8670520,
               "lng" : 151.1957360
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "3ef986cd56bb3408bc1cf394f3dad9657c1d30f6",
         "name" : "Doltone House - Darling Island Wharf",
         "reference" : "CoQBewAAAExUq-YlS-bahqAJeJbfyGfkUrqyuKjZgCPxHznMZsHg1aHLWoFk8s_04DxDtEFAlSyRP-ZHNsceha6MB8JD7zFMjbYOKNJLSm9OcgFUOFB-4SZP3ZeRyYzna2032gayzUU1_8N-qtb_r2kfllGYAmgnaU7h6RC8haaf-dFfvdiiEhBUdWHpEeL7qwtB91BgNYdPGhTAjf0Xa7-MKHJRRCQu_g7XL80dzQ",
         "types" : [ "establishment" ],
         "vicinity" : "Pirrama Road, Pyrmont"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : -33.8671110,
               "lng" : 151.1958750
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "258f23471a33cfa2828170660d94cd5797edb18c",
         "name" : "Workplace6",
         "reference" : "CnRhAAAAi_hm5c3gpntkNvC7NbQtIe5tcmosCIHHbzqhLURaH7mQ6inEOqg47_cltBrtFBEPSIqCuPQDC3UA-suLBfRslIwUNytKjC87Lt1ZrGXF6ZmnOR2rVcdCGdoWrdi977RHruBO21zbjzVWE-A3ZDn2NBIQ1Vu2-nr3E02DuJAtXL8x4BoUEBRWEHReK2LO3IoldvyyvSKb0Qs",
         "types" : [ "establishment" ],
         "vicinity" : "Pirrama Rd, Pyrmont"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : -33.8670520,
               "lng" : 151.1957360
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "fe9136d6406ed2d35660dea37a0be34043c1a4f2",
         "name" : "Doltone House",
         "reference" : "CnRjAAAAo4zx1lM-veFEPBBeIRFETZoH8QRMmpHIBVtnmfOZ-Sh1-8p4vxKj-146pHGW6wPCZxIV0iIuAGow9OSsHubvY_kSnlBe5OB99ZSxHQsm0LrLh45c05Gf7C8jwp_9D7keDVDAC47MN65niSZW71fOrRIQxODppLktQZFWbf-V1N4gHxoUPetbj0zbU1kQj96YrRLzLKrbMoE",
         "types" : [ "establishment" ],
         "vicinity" : "Pirrama Road, Pyrmont"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : -33.8666470,
               "lng" : 151.195650
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "2722b46120160ec0a322e0a13cb0717ed41a696b",
         "name" : "Biaggio",
         "reference" : "CmRdAAAAp7wiB1b9cK-26FjhO2DVAGA-5uzuZxppHqCkR75dQxoMJiLD5G7H90_dKtNVDlRAdKSOx7hWlUoHGuwkbBA4ZhNIDrPdLWQ1a41sEN9gZJvU-t7uxoRzziZpTVSCU-AXEhDoQ36icc3OBaMH1xxnPgsEGhTB08nD6Q67ojrGYMJWrd4PojJKsw",
         "types" : [ "establishment" ],
         "vicinity" : "Pirrama Rd, Pyrmont"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : -33.86954560,
               "lng" : 151.19454040
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
         "id" : "40840d476baa531e0227a353b1bef70262f66e7e",
         "name" : "Pyrmont",
         "reference" : "CnRnAAAA7aHZwg2JyjTI-xotS1q4XKgbg3eCUwOZm3Nz55Xf5Q2w-nZpBrrd2VAEl60On0puVl8bX7UaJTIECELr3KY2mL4s7zsIznItOiJEG-9QMlnreRSmxSBSUAkPghKWoq4OFbCr0dwNtZSKJFazcbCc1BIQSz5BZfCm-iyMZ8YOgvc4tBoU1CdnwsHS5PhB69jpYHsOvfWXEgo",
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

感谢任何帮助 - 我不确定会出现什么问题。

谢谢!

2 个答案:

答案 0 :(得分:3)

问题是你正在尝试以对象的形式读取数组。

更改

JSONObject results = completeJSONObj.getJSONObject("results");

JSONArray results = completeJSONObj.getJSONArray("results");

并且解析异常消失了。

答案 1 :(得分:0)

您的JSON应有效:示例:

{\"key\":\"value\" }