Heyy guyz I am receiving JSON using google places details api then In that Json I am looking for "reviews" array.But in logcat I am getting system error that No values for reviews.But I checked in Json that there exist reviews array.Where is the problem?Thanx in advance.This is my JSON data:
{
"result" : {
"address_components" : [
{
"long_name" : "Kalyan",
"short_name" : "Kalyan",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Maharashtra",
"short_name" : "Maharashtra",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
},
{
"long_name" : "421301",
"short_name" : "421301",
"types" : [ "postal_code" ]
}
],
"reviews" : [
{
"aspects" : [
{
"rating" : 0,
"type" : "overall"
}
],
"author_name" : "Kunal Korde",
"author_url" : "https://plus.google.com/108837578005055609416",
"language" : "en",
"rating" : 1,
"text" : "it is very bad to call domino one and only kalyan
"time" : 1382351314
},
{
"aspects" : [
{
"rating" : 3,
"type" : "food"
},
{
"rating" : 2,
"type" : "decor"
},
{
"rating" : 1,
"type" : "service"
}
],
"author_name" : "Hits Daiya",
"author_url" : "https://plus.google.com/101565870698816750539",
"language" : "en",
"rating" : 4,
"text" : "wt a excellent food anybody can get here! I wanna to say that wt a
"time" : 1371385367
},
{
"aspects" : [
{
"rating" : 1,
"type" : "overall"
}
],
"author_name" : "nirmit jallawar",
"author_url" : "https://plus.google.com/116255076196839398528",
"language" : "en",
"rating" : 3,
"text" : "Good but a bit more of standard is necessary ",
"time" : 1402139860
},
{
"aspects" : [
{
"rating" : 2,
"type" : "food"
},
{
"rating" : 0,
"type" : "decor"
},
{
"rating" : 1,
"type" : "service"
}
],
"author_name" : "Rutam Mokashi",
"author_url" : "https://plus.google.com/112151348544733227698",
"language" : "en",
"rating" : 3,
"text" : "best place for pizzas in kalyan...",
"time" : 1353151680
},
{
"aspects" : [
{
"rating" : 1,
"type" : "food"
},
{
"rating" : 2,
"type" : "decor"
},
{
"rating" : 2,
"type" : "service"
}
],
"author_name" : "A.A Varghese varghese",
"author_url" : "https://plus.google.com/103110235851606786394",
"language" : "en",
"rating" : 4,
"text" : "nice hotel.",
"time" : 1375596316
}
],
"scope" : "GOOGLE",
"types" : [ "restaurant", "food", "establishment" ],
"url" : "https://plus.google.com/107013844902194125587/about?hl=en-US",
"user_ratings_total" : 32,
"utc_offset" : 330,
"vicinity" : "Plot No. C 1, Ground Floor, Shop No. 2, 3 & 4, Chikanghar, Kalyan",
"website" : "http://www.dominos.co.in/"
},
"status" : "OK"
}
and this my code:
public class Review_activity extends ActivityGroup {
protected static LocalActivityManager mLocalActivityManager;
// Alert Dialog Manager
AlertDialogManager alert = new AlertDialogManager();
ProgressDialog pDialog;
JSONObject json = null;
// Review Listview
ListView list;
String reference;
public static final String TAG_name = "-NA-";
public static final String TAG_rating = "-NA-";
public static final String TAG_text = "-NA-";
public static String reference_value = "reference";
public static String KEY_REFERENCE = "reference";// id of the place
private static final String TEL_PREFIX = "tel:";
ArrayList<HashMap<String, String>> oslist = newArrayList<HashMap<String,String>>();
// public static String url = "";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.review);
oslist = new ArrayList<HashMap<String, String>>();
// Getting listview
list = (ListView) findViewById(R.id.list);
// Getting place reference from the map
reference = getIntent().getStringExtra("reference");
Log.d("yogesh", reference);
String sb ="https://maps.googleapis.com/maps/api/place/details/json?";
String sb1=sb.concat("&reference="+reference);
String sb2=sb1.concat("&sensor=true");
String sb3=sb2.concat("&key=AIzaSyChVcy-8fLkAq5-ZJCuNomF1lIf-Gda7s8");
String url=sb3;
Log.d("URL", url);
new JSONParse().execute(url);
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Review_activity.this);
pDialog.setMessage("Getting Reviews ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... params) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
String s = params[0];
Log.d("URL", s);
JSONObject json = jParser.getJSONFromUrl(s);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// Getting JSON Array from URL
JSONArray jArray = json.getJSONArray("reviews");
int k;
k=jArray.length();
k--;
String Alength=String.valueOf(k);
Log.d("Array length", Alength);
//To get the items from the array
int j=0;
for (int i=0;i<=k;i++)
{
JSONObject r1 = jArray.getJSONObject(j);
String aname = r1.getString("author_name");
String arating = r1.getString("rating");
String text = r1.getString("text");
Log.d("review", aname);
Log.d("review", arating);
Log.d("review", text);
j++;
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_name, aname);
map.put(TAG_rating, arating);
map.put(TAG_text,text );
oslist.add(map);
}
list=(ListView)findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(Review_activity.this, oslist,
R.layout.review_item,
new String[] { TAG_name,TAG_rating, TAG_text }, new int[] {
R.id.textView2,R.id.textView4, R.id.textView6});
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
2 Answers 2
i found your json is not a valid json
use check if json is valid or not this to check valid json
i correct it and the valid json is here
{
"result": {
"address_components": [
{
"long_name": "Kalyan",
"short_name": "Kalyan",
"types": [
"locality",
"political"
]
},
{
"long_name": "Maharashtra",
"short_name": "Maharashtra",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "India",
"short_name": "IN",
"types": [
"country",
"political"
]
},
{
"long_name": "421301",
"short_name": "421301",
"types": [
"postal_code"
]
}
],
"reviews": [
{
"aspects": [
{
"rating": 0,
"type": "overall"
}
],
"author_name": "Kunal Korde",
"author_url": "https://plus.google.com/108837578005055609416",
"language": "en",
"rating": 1,
"text": "it is very bad to call domino one and only kalyan",
"time": 1382351314
},
{
"aspects": [
{
"rating": 3,
"type": "food"
},
{
"rating": 2,
"type": "decor"
},
{
"rating": 1,
"type": "service"
}
],
"author_name": "HitsDaiya",
"author_url": "https: //plus.google.com/101565870698816750539",
"language": "en",
"rating": 4,
"text": "wtaexcellentfoodanybodycangethere!Iwannatosaythatwta",
"time": 1371385367
},
{
"aspects": [
{
"rating": 1,
"type": "overall"
}
],
"author_name": "nirmitjallawar",
"author_url": "https: //plus.google.com/116255076196839398528",
"language": "en",
"rating": 3,
"text": "Goodbutabitmoreofstandardisnecessary",
"time": 1402139860
},
{
"aspects": [
{
"rating": 2,
"type": "food"
},
{
"rating": 0,
"type": "decor"
},
{
"rating": 1,
"type": "service"
}
],
"author_name": "RutamMokashi",
"author_url": "https: //plus.google.com/112151348544733227698",
"language": "en",
"rating": 3,
"text": "bestplaceforpizzasinkalyan...",
"time": 1353151680
},
{
"aspects": [
{
"rating": 1,
"type": "food"
},
{
"rating": 2,
"type": "decor"
},
{
"rating": 2,
"type": "service"
}
],
"author_name": "A.AVarghesevarghese",
"author_url": "https: //plus.google.com/103110235851606786394",
"language": "en",
"rating": 4,
"text": "nicehotel.",
"time": 1375596316
}
],
"scope": "GOOGLE",
"types": [
"restaurant",
"food",
"establishment"
],
"url": "https: //plus.google.com/107013844902194125587/about?hl=en-US",
"user_ratings_total": 32,
"utc_offset": 330,
"vicinity": "PlotNo C1 GroundFloor ShopNo 2 3 4 Chikanghar Kalyan",
"website": "http: //www.dominos.co.in/"
},
"status": "OK"
}
i found your code seems pretty correct ....
the safer side is first print the response json and check if its valid or not....
12 Comments
Issue is you first to get json object for "result" then use that object to find json array for review.
jsonobject = json.getJsonObject("result");
resultArray = jsonobject.getJsonArray("review");