2017-04-19 98 views
-2

我想从谷歌api中使用volley获取json数据。我在logcat中获得Error: org.json.JSONException。我如何解决它?`错误:org.json.JSONException`为json解析使用volley

我的谷歌API网址,

private String urlJsonObj = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=21.2150756,72.8880545&radius=500&type=hospital&key=AIzaSyBldIefF25dfjjtMZq1hjUxrj4T4hK66Mg"; 

makeJsonObjectRequest功能来获取数据,

private void makeJsonObjectRequest() { 
showpDialog(); 

// Creating volley request obj 
JsonArrayRequest jsonObjReq = new JsonArrayRequest(urlJsonObj, 
     new Response.Listener<JSONArray>() { 
      @Override 
      public void onResponse(JSONArray response) { 
       Log.d(TAG, response.toString()); 
       hidepDialog(); 

       try { 
        JSONArray results = response.getJSONArray(1); 
        // Parsing json 
        for (int i = 0; i < results.length(); i++) { 
         try { 
          JSONObject obj = results.getJSONObject(i); 
          LocationDetailModel locationDet = new LocationDetailModel(); 
          locationDet.setTitle(obj.getString("name")); 
          locationDet.setThumbnailUrl(obj.getString("vicinity")); 

          LocationDetailList.add(locationDet); 

         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 

        } 

        // notifying list adapter about data changes 
        // so that it renders the list view with updated data 
        adapter.notifyDataSetChanged(); 
       } catch (JSONException e) { 
        Log.e("MYAPP", "unexpected JSON exception", e); 
        // Do something to recover ... or kill the app. 
       } 


      } 
     }, new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 
     Log.d("VolleyError","Error: " + error.getMessage()); 
     VolleyLog.d(TAG, "Error: " + error.getMessage()); 
     hidepDialog(); 

    } 
}); 

// Adding request to request queue 
AppController.getInstance().addToRequestQueue(jsonObjReq); 

}

我得到波纹管的错误在我的logcat,

04-19 12:56:25.348 4485-4485/com.example.chaitanya.nearbyplace D/VolleyError: Error: org.json.JSONException: Value {"html_attributions":[],"results":[{"geometry":{"location":{"lat":21.21508679999999,"lng":72.8883554},"viewport":{"northeast":{"lat":21.2164357802915,"lng":72.8897043802915},"southwest":{"lat":21.21373781970849,"lng":72.88700641970848}}},"icon":"https:\/\/maps.gstatic.com\/mapfiles\/place_api\/icons\/doctor-71.png","id":"831a38ee464657c1b60683be963e145b566e95d1","name":"Deep Children Hospital","place_id":"ChIJE1cZjXpP4DsR1ITMcESvDeU","rating":4.2,"reference":"CmRSAAAApsEU5Kvy3RIGkxaQklHK-Jr700tF_DGu4JAEAEibQ-lVZQApAddzMtONETVQihcJ1MYooX0k5QRphjsplHb6xgALmefjoHiAc-O3N08_S5blHqRKVZogOeNCe6urVEaaEhAfVhJbsQvCY2dKrl5j8CfdGhSCsQtU2BuIGkWvMgRzVwf4oYziSQ","scope":"GOOGLE","types":["hospital","point_of_interest","establishment"],"vicinity":"302-305, Swastik Plaza, Pune- Simada Road, Yogi Chowk, Yogidarshan Society, Nana Varachha, Surat"},{"geometry":{"location":{"lat":21.2149842,"lng":72.8883906},"viewport":{"northeast":{"lat":21.2163331802915,"lng":72.88973958029149},"southwest":{"lat":21.2136352197085,"lng":72.88704161970848}}},"icon":"https:\/\/maps.gstatic.com\/mapfiles\/place_api\/icons\/doctor-71.png","id":"e62d17d2eaba8c7af64144418c845476837d0610","name":"Chirayu General Hospital","opening_hours":{"open_now":true,"weekday_text":[]},"place_id":"ChIJI_rAjHpP4DsRVfNqHtl8s24","reference":"CmRRAAAA6Sr99xxP0-2xzTjFP-ajIfr63ZtMMVCyz4xtAuV3y6CxvlsyQRUp5Lx35uc7-Ir7jafjCJPPQ0XBCy28QSqj8drtgZqhu-Pk6MAz--jG4RjdwdL1WQGzu3Pf_SEMIJMBEhBXQCMOtyY01AkUfLNSNe-rGhSjLdu_AbeYiW5cTp3xQbaYDsAO7A","scope":"GOOGLE","types":["hospital","point_of_interest","establishment"],"vicinity":"402-406 Swastik Plaza, Puna-Simada Road, Yogi Chowk Ground, Chikuwadi, Nana Varachha, Surat"},{"geometry":{"location":{"lat":21.213784,"lng":72.88569799999999},"viewport":{"northeast":{"lat":21.2151329802915,"lng":72.8870469802915},"southwest":{"lat":21.2124350197085,"lng":72.8843490197085}}},"icon":"https:\/\/maps.gstatic.com\/mapfiles\/place_api\/icons\/doctor-71.png","id":"c4290cb09a3be448718cd65ebc9a62c90799c2aa","name":"Vaghani Hospital","photos":[{"height":794,"html_attributions":["<a href=\"https:\/\/maps.google.com\/maps\/contrib\/103410682498224690153\/photos\">Vaghani Hospital<\/a>"],"photo_reference":"CoQBdwAAAFzUTtvhGHzcYJq-UdYpzxRpMKlgSkkHtu87G8u3WD6s99wwxEnwGxUEcUSCRJDsSIM8veQVPKRkUkn1rFTCB4pPotitPXtxYIMQksBTvDvuAynm9sHzxJnf65RaSFJcJ_rMUJ9ghS4PeIF0SV_C2XZ5BCBwOgkD9tgCNmOxXrxSEhAMrcGa8Khzysiffh2Vo8CkGhT-ZGU78NmQcMC5trHSS1h3nNsp_g","width":794}],"place_id":"ChIJ13n1jHBP4DsRhVsMMvBHBrU","rating":4.8,"reference":"CmRSAAAAG-tPKgTwGveJEt0PL9IDIe9XLQkBnpZFJTsY0XDArF7jUT81szl_x7F1ndeOB3TZVsYcEEMf7G93CIuPs-fodpEjM413PI7Mb1IdlO5bTWGNBebPZoAyu9dPqGjJCM6yEhAyotMjeoI9Hyvr4PEb0vKjGhRbPbYmp-uwwdVyDapzxpV4pOMfXw","scope":"GOOGLE","types":["hospital","point_of_interest","establishment"],"vicinity":"434-445, 4 th Floor, Apple Square,, Beside Swastik Plaza, Yogi Chowk, Surat., Surat"},{"geometry":{"location":{"lat":21.2150921,"lng":72.8880922},"viewport":{"northeast":{"lat":21.2164410802915,"lng":72.88944118029151},"southwest":{"lat":21.21374311970849,"lng":72.88674321970849}}},"icon":"https:\/\/maps.gstatic.com\/mapfiles\/place_api\/icons\/doctor-71.png","id":"34d3977def7c2ee31d41db96d55df1b4c07c7e72","name":"Virdiya Hospital","place_id":"ChIJ5U12jnpP4DsRylJBztn0bdE","reference":"CmRSAAAAZefzhhlgAEYxBv29zBLtp_eLrZpy-RVAFzeVtpFrNGFoYPKQ6JQddoNUvGtTz2Ow10KHYJw7pajUHe9KhDBSM5lgaDRu9bNBMX1jCgjmZqq9MO3df_EpayVsFST5Jx6NEhA8jCjggaoVYu6rZnXcFy0eGhSiq_VX6gmNptn73kB1hNRZngIuxw","scope":"GOOGLE","types":["hospital","point_of_interest","establishment"],"vicinity":"315, 3rd Floor, Swastik Plaza, Yogi Chowk, Yogi Chowk Ground, Chikuwadi, Nana Varachha, Surat"},{"geometry":{"location":{"lat":21.2150999,"lng":72.88838419999999},"viewport":{"northeast":{"lat":21.2162783302915,"lng":72.88982628029149},"southwest":{"lat":21.2135803697085,"lng":72.88712831970848}}},"icon":"https:\/\/maps.gstatic.com\/mapfiles\/place_api\/icons\/doctor-71.png","id":"9469cd58c282297693fbc0cfe0cd25a1c34926a7","name":"Viradiya Test Tube Baby"," 
04-19 12:56:25.349 4485-4485/com.example.chaitanya.nearbyplace D/Volley: [1] override.onErrorResponse: ItemDetailsActivity 
+0

结果arrayname --Json数组名和Vicinty是jsonobject。如果有任何疑问意味着我...... –

回答

0

你的API响应是JSON对象没有JSON数组所以要尽量改变JsonArrayRequestJsonObjectRequest

1

使用JSONArray results = response.getJSONArray("results"),而不是JSONArray results = response.getJSONArray(1)

试试这个:

 @Override 
     public void onResponse(JSONArray response) { 
      Log.d(TAG, response.toString()); 
      hidepDialog(); 

      try { 
       JSONArray results = response.getJSONArray("results"); 
       // Parsing json 
       for (int i = 0; i < results.length(); i++) { 
        try { 
         JSONObject obj = results.getJSONObject(i); 
         LocationDetailModel locationDet = new LocationDetailModel(); 
         locationDet.setTitle(obj.getString("name")); 
         locationDet.setThumbnailUrl(obj.getString("vicinity")); 

         LocationDetailList.add(locationDet); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

       } 

       // notifying list adapter about data changes 
       // so that it renders the list view with updated data 
       adapter.notifyDataSetChanged(); 
      } catch (JSONException e) { 
       Log.e("MYAPP", "unexpected JSON exception", e); 
       // Do something to recover ... or kill the app. 
      } 
     } 

     ............ 
     ....................