我正在使用谷歌地图api自动完成,并试图获取搜索地点的图像,我在Json Parser中获得空值 请帮帮我
public class PlaceJSONParserO {
public Place[] parse(JSONObject jObject) {
Log.e("PlaceJSONParserO"," call");
JSONArray jPlaces = null;
try {
jPlaces = jObject.getJSONArray("results");
Log.e("PlaceJSONParserO jplaces", String.valueOf(jPlaces));
} catch (JSONException e) {
e.printStackTrace();
Log.e("Catch-->",e.toString()); //got the exception here
}
return getPlaces(jPlaces);
}
private Place[] getPlaces(JSONArray jPlaces) {
int placesCount = jPlaces.length();
Place[] places = new Place[placesCount];
Log.e("Count", String.valueOf(placesCount));
Log.e("Places ddd", places.toString());
for (int i = 0; i <= placesCount; i++) {
try {
places[i] = getPlace((JSONObject) jPlaces.get(i));
Log.e("get data information ",places[i].toString());
} catch (JSONException e) {
e.printStackTrace();
Log.e("catch exeption data",e.toString());
}
}
return places;
}
private Place getPlace(JSONObject jPlace) {
Log.e("JSON PLACE",jPlace.toString());
Place place = new Place();
try {
// Extracting Place name, if available
if (!jPlace.isNull("long_name")) {
place.mPlaceName = jPlace.getString("long_name");
Log.e("Name==",place.mPlaceName);
}
// Extracting Place Vicinity, if available
if (!jPlace.isNull("short_name")) {
place.mVicinity = jPlace.getString("short_name");
}
if (!jPlace.isNull("photos")) {
JSONArray photos = jPlace.getJSONArray("photos");
place.mPhotos = new Photo[photos.length()];
for (int i = 0; i < photos.length(); i++) {
place.mPhotos[i] = new Photo();
place.mPhotos[i].mWidth = ((JSONObject) photos.get(i)).getInt("width");
place.mPhotos[i].mHeight = ((JSONObject) photos.get(i)).getInt("height");
place.mPhotos[i].mPhotoReference = ((JSONObject) photos.get(i)).getString("photo_reference");
JSONArray attributions = ((JSONObject) photos.get(i)).getJSONArray("html_attributions");
place.mPhotos[i].mAttributions = new Attribution[attributions.length()];
for (int j = 0; j < attributions.length(); j++) {
place.mPhotos[i].mAttributions[j] = new Attribution();
place.mPhotos[i].mAttributions[j].mHtmlAttribution = attributions.getString(j);
}
}
}
place.mLat = jPlace.getJSONObject("geometry").getJSONObject("location").getString("lat");
place.mLng = jPlace.getJSONObject("geometry").getJSONObject("location").getString("lng");
} catch (JSONException e) {
e.printStackTrace();
Log.d("EXCEPTION", e.toString());
}
Log.e("Details places", String.valueOf(place));
return place;
}
}
}
这是我在地图活动中的JSON调用
private class PlacesTask extends AsyncTask<String, Integer, String> {
String data = null;
@Override
protected String doInBackground(String... url) {
try {
Log.e("url do back", url[0]); //here i got the URL
data = downloadUrl(url[0]);
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
@Override
protected void onPostExecute(String result) {
Log.e("pasrde", result); // the result get the currect JSON
ParserTask parserTask1 = new ParserTask();
if(Build.VERSION.SDK_INT >= 11)
parserTask1.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
parserTask1.execute(result);
}
}
private class ParserTask extends AsyncTask<String, Integer, Place[]> {
JSONObject jObject;
@Override
protected Place[] doInBackground(String... jsonData) {
Place[] places = null;
Log.e("json data > ",jsonData.toString());
PlaceJSONParserO placeJsonParser = new PlaceJSONParserO(); // After the call getting null value, so execute the exception
try{
jObject = new JSONObject(jsonData[0]);
places = placeJsonParser.parse(jObject);
Log.e("JSON Object","tst"+places);
}catch(Exception e)
{
e.printStackTrace();
}
return places;
}
@Override
protected void onPostExecute(Place[] places) {
....
}
像这样的JSON值
{ "html_attributions" : [], "result" : { "address_components" : [ { "long_name" : "Kollam", "short_name" : "Kollam", "types" : [ "locality", "political" ] }, { "long_name" : "Kollam", "short_name" : "KLM", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Kerala", "short_name" : "KL", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "India", "short_name" : "IN", "types" : [ "country", "political" ] }, { "long_name" : "691001", "short_name" : "691001", "types" : [ "postal_code" ] } ], "adr_address" : "\u003cspan class=\"locality\"\u003eKollam\u003c/span\u003e, \u003cspan class=\"region\"\u003eKerala\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e691001\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eIndia\u003c/span\u003e", "formatted_address" : "Kollam, Kerala 691001, India", "geometry" : { "location" : { "lat" : 8.8932118, "lng" : 76.6141396 }, "viewport" : { "northeast" : { "lat" : 8.898130699999999, "lng" : 76.61599149999999 }, "southwest" : { "lat" : 8.874412099999999, "lng" : 76.57168419999999 } } }, "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png", "id" : "c880d95b212b2461e5dd9927ba351c29f5c4cf2c", "name" : "Kollam", "photos" : [ { "height" : 2848, "html_attributions" : [ "\u003ca href=\"https://maps.google.com/maps/contrib/117303089050910364610/photos\"\u003elekshmi vegway\u003c/a\u003e" ], "photo_reference" : "CoQBcwAAAEimH48mlgdsY7EdfdphfJwa6-7-8MjooXZqTWBb1hponUzcZhhf-aMJO8L3xQXxtcmDZIj3uXj68m91nbKX5LyCIWBYvQsgwOgv8G_--BLAN3fFIjcP-w2p8G7yhpU3ydroQLWF1Z_ddEn801PQZbxZyp_daeDICujE3rK5xHQ0EhCquFrvT5T-6HpyH_MCE8uFGhTWe3Eyv7e0r99IAQZ1YfTO3r_6Wg", "width" : 4288 }, { "height" : 1152, "html_attributions" : [ "\u003ca href=\"https://maps.google.com/maps/contrib/106519267907454252415/photos\"\u003eJiju Paravur\u003c/a\u003e" ], "photo_reference" : "CoQBcwAAAKIbOVXGQfDpuovBkOOaqE_OgFVBWfb0pwlPj6854EQ6LGJrSK5HX5SvD_WSZE3P8Ntq8m93grV1kFIzk_T1Xs5iUpH3GB-uICqyQFOiKLXk6PN8k0GmDgqD-79-VhQgZkvORE0PKwiN6vX88u0rg0HdwCNK-Vj_tm6sufz1jC8qEhAKnJ-JZQXYBMQSx-ywwC63GhQs1_r4asSxuKigWdGWj6APrCyTpg", "width" : 2048 }, { "height" : 720, "html_attributions" : [ "\u003ca href=\"https://maps.google.com/maps/contrib/115352225097306538446/photos\"\u003eRoby Thankachan\u003c/a\u003e" ], "photo_reference" : "CoQBdwAAAHU5x0qkwzytr9A7FHn-cdklYaSm0GvBHBHnprzY_zEtT06jIV1l1oyizCi7N73nvXPZoNGpe9ruUD75-AC8tvKr6TphNCR_S_5CByGz8I70acvIVV8VvVs0oRvxmYMKVOTBxOA6l3G1gIUpEvQPCWSMa6AbQEVQ1y_YTqQ8h9O_EhB7v5oSjAhboOw_9RQOZ93HGhTdb4jMNpC8FXTfM6w9HNbgYsCjUA", "width" : 960 }, { "height" : 704, "html_attributions" : [ "\u003ca href=\"https://maps.google.com/maps/contrib/112849338948952733528/photos\"\u003eEiffel Bastin\u003c/a\u003e" ], "photo_reference" : "CoQBcwAAALNUi-ooozDAmBbOwrxnfrF73s7JdJtrhinJShhKvQfjsbQKKlpU0hviceNaLYfYYTSaXgNh7eevU-DJt2PgMPyVy2_yi0NTUJP7Ffwbqae7IxxCDiE2h1GgvmbiYIpqNnLsSzPCIlpM-iLGVOXvIpi1Z6aAiCxre-PBnVklglBwEhDJU20ztQ1LtGB35OovuJyYGhQCHCLO3VKQHfCwpV3Wjccd2sfk-Q", "width" : 2208 }, { "height" : 960, "html_attributions" : [ "\u003ca href=\"https://maps.google.com/maps/contrib/116245421984595223878/photos\"\u003eAppu B\u003c/a\u003e" ], "photo_reference" : "CoQBcwAAAKDr605tHHV6"
如何获取照片参考?请帮帮我
答案 0 :(得分:1)
传递json对象,而不是json数组