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();
}
}
}
}
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....
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");
Related
I am trying to parse this file.
I am new to maps API so.
I am trying to parse this set into recyclerview
I trying to parse using String Request that works but I need json object to store data.
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "Ahmedabad",
"short_name" : "Ahmedabad",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Suradhara Society",
"short_name" : "Suradhara Society",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Khodiar Nagar",
"short_name" : "Khodiar Nagar",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Ahmedabad",
"short_name" : "Ahmedabad",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Gujarat",
"short_name" : "GJ",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
},
{
"long_name" : "382350",
"short_name" : "382350",
"types" : [ "postal_code" ]
}
],
"adr_address" : "3rd Floor, Sardar Patel Mall, Nikol Gam Road, \u003cspan class=\"street-address\"\u003eBapunagar\u003c/span\u003e, \u003cspan class=\"extended-address\"\u003eSuradhara Society, Khodiar Nagar\u003c/span\u003e, \u003cspan class=\"locality\"\u003eAhmedabad\u003c/span\u003e, \u003cspan class=\"region\"\u003eGujarat\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e382350\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eIndia\u003c/span\u003e",
"formatted_address" : "3rd Floor, Sardar Patel Mall, Nikol Gam Road, Bapunagar, Suradhara Society, Khodiar Nagar, Ahmedabad, Gujarat 382350, India",
"formatted_phone_number" : "085116 15040",
"geometry" : {
"location" : {
"lat" : 23.0379687,
"lng" : 72.6433222
},
"viewport" : {
"northeast" : {
"lat" : 23.0393176802915,
"lng" : 72.64467118029151
},
"southwest" : {
"lat" : 23.0366197197085,
"lng" : 72.64197321970849
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "7ddb8968d27481b07a700f6e34ac5442f528eaf2",
"international_phone_number" : "+91 85116 15040",
"name" : "Parsana Gym",
"photos" : [
{
"height" : 2322,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/104290859169763692879/photos\"\u003eAlpesh Patel\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAAgWV9QS54sRCALTitzLBc7LowfL3gKMw_jbNaTSmeFM9GqqG6iu1hx4HJwj332LJQHpfk44xeAPIgYg0mexUJaoWd8mKIUt4oTUj0f-DvbmnrOG67Q5BwAh9eP6VjcPz3EhAHDkHpn52M8rURjiAncwzrGhTLyHQZ5qHXowonHaOnDvld63wrXg",
"width" : 4128
}
],
"place_id" : "ChIJEbpNLyCHXjkRny4b5TptHsY",
"plus_code" : {
"compound_code" : "2JQV+58 Ahmedabad, Gujarat, India",
"global_code" : "7JMJ2JQV+58"
},
"rating" : 4.4,
"reference" : "ChIJEbpNLyCHXjkRny4b5TptHsY",
"reviews" : [
{
"author_name" : "Sumit Patel",
"author_url" : "https://www.google.com/maps/contrib/101190972481759472779/reviews",
"language" : "en",
"profile_photo_url" : "https://lh3.googleusercontent.com/-70hcgZha3k4/AAAAAAAAAAI/AAAAAAAAAAA/ABtNlbDZJQHYZ86bOPzJBQjG8VNA0_Sh6g/s128-c0x00000000-cc-rp-mo/photo.jpg",
"rating" : 5,
"relative_time_description" : "4 months ago",
"text" : "gym is good and trainer are also good.\nbut some new machine are require for excersize.",
"time" : 1527730807
},
{
"author_name" : "Mahesh Bhayani",
"author_url" : "https://www.google.com/maps/contrib/105571914899766322779/reviews",
"language" : "en",
"profile_photo_url" : "https://lh3.googleusercontent.com/-EKevbjOaZsw/AAAAAAAAAAI/AAAAAAAAA4o/k75Wr5thZvg/s128-c0x00000000-cc-rp-mo/photo.jpg",
"rating" : 5,
"relative_time_description" : "4 months ago",
"text" : "Good atmosfiyar for parsana gym. Good service provide good trinar.",
"time" : 1527820640
},
{
"author_name" : "Nadiya Dipak",
"author_url" : "https://www.google.com/maps/contrib/100514831030336220877/reviews",
"language" : "en",
"profile_photo_url" : "https://lh6.googleusercontent.com/-he9oXwcUqLI/AAAAAAAAAAI/AAAAAAAAAAA/ABtNlbBq3ow6IHVx5QNQktTbXQEDI2v2xw/s128-c0x00000000-cc-rp-mo/photo.jpg",
"rating" : 5,
"relative_time_description" : "4 months ago",
"text" : "Parsana gym is very good sarvice provide . Nice team work",
"time" : 1527738686
}
],
"scope" : "GOOGLE",
"types" : [ "gym", "health", "point_of_interest", "establishment" ],
"url" : "https://maps.google.com/?cid=14275967968530345631",
"utc_offset" : 330,
"vicinity" : "3rd Floor, Sardar Patel Mall, Nikol Gam Road, Bapunagar, Ahmedabad"
},
"status" : "OK"
}
I use volley that gives parse exception.
Here is my code
I am trying to parse JSON format using volley using below code.
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject object = response.getJSONObject(i);
JSONObject result = object.getJSONObject("result");
JSONArray array = result.getJSONArray("address_components");
Log.d("result", "" + array.length());
for (int adress = 0; adress < array.length(); adress++) {
JSONObject object1 = array.getJSONObject(i);
Log.d("result", object1.getString("long_name"));
Log.d("result", object1.getString("short_name"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
//Error lisnter for request
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("error", error.getMessage());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest );
Can Anyone help what I am missing in this?
here is exception.
Note-: org.json.JSONException: Value {"html_attributions":[],"result":{"address_components":[{"long_name":"Ahmedabad","short_name":"Ahmedabad","t.........}
Thanks in advance !!
Do this :
your response starts with jsonObject'{' not jsonArray'['
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONObject jsonObject = response.getJSONObject("result");
JSONArray jsonArray = jsonObject.getJSONArray("address_components");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
Log.d("result", object.getString("long_name"));
Log.d("result", object.getString("short_name"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
//Error lisnter for request
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("error", error.getMessage());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest );
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I can't extract JSONObject from saved preference class by using TestCrowd.java class.
My JSON,
{
"success": true,
"message": {
"user": {
"firstName": "aaa",
"lastName": "aaa",
"email": "xxx#gmail.com",
"role": 1,
"profileUrl": ""
},
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7ImZpcnN0TmFtZSI6ImFhYSIsImxhc3ROYW1lIjoiYWFhIiwiZW1haWwiOiJ4eHhAZ21haWwuY29tIiwicm9sZSI6MSwicHJvZmlsZVVybCI6IiJ9LCJpYXQiOjE0ODY2MzU3NjUsImV4cCI6MTQ4NjY0NzIwNX0.rRlqKNMoBs_AaDKOUlToT5-D5QKb20IjYMuUBqK9G7c",
"enum": {
"_id": "5889d3f10893a7a42243998e",
"updatedAt": "2017-01-26T10:48:17.233Z",
"createdAt": "2017-01-26T10:48:17.233Z",
"__v": 0,
"mailCategories": [
{
"color": [
"Work",
"Document",
"Social",
"Advertising",
"Client"
],
"value": [
"#1ab394",
"#EF5352",
"#1c84c6",
"#23c6c8",
"#F8AC59"
]
}
],
"folders": [
"Send",
"Draft"
],
"uploadsImageTypes": [
"jpg",
"jpeg",
"png",
"gif"
],
"uploadsFileTypes": [
"jpg",
"jpeg",
"docx",
"pdf",
"txt",
"ppt",
"png",
"gif"
],
"workOrderStatus": {
"color": [
"#A09580",
"#f8ac59",
"#41b0f6",
"#0d71b0",
"#1ab394",
"#067e47",
"#2819bc",
"#0d08f3",
"#ef0c34",
"#ef9aa9",
"#f2830b"
],
"value": [
"Draft",
"Requested",
"Requested Awaiting Approval",
"Assigned",
"Work In Progress",
"Ready",
"Done",
"Closed,Completed",
"Closed,Incompleted",
"On Hold",
"Open"
]
},
"priorities": {
"color": [
"#0B7409",
"#CE9B9B",
"#7CC396",
"#CEBE99"
],
"value": [
"Hard Down",
"High",
"Medium",
"Low"
]
},
"categories": [
"Equipment",
"Cranes",
"Delivery Vehicles",
"Rotating Spares"
],
"projects": [
"Vehicle New",
"System #7865",
"Book Shop",
"Site"
],
"maintenanceTypes": {
"color": "#FFFFFF",
"bg_color": [
"#1c84c6",
"#262626",
"#A09580",
"#ed5565"
],
"value": [
"Electrical",
"Damage",
"Safty",
"Broken"
]
},
"assets": [
"Conveyor Belt 1",
"Cranes",
"Delivery Vehicles",
"Rotating Spares"
],
"assignToUser": [
1,
2,
4,
5,
6
],
"roles": [
"Admin",
"Manager",
"Technician",
"Customer",
"Supplier",
"Engineer",
"Guest"
]
}
}
}
TestCrowd.java
public class TestCrowd extends Application {
private JSONObject roles,priorities;
public JSONObject getRoles() {
return roles;
}
public void setRoles(JSONObject roles) {
this.roles = roles;
}
public JSONObject getPriorities() {
return priorities;
}
public void setPriorities(JSONObject priorities) {
this.priorities = priorities;
}
}
And my main activity called NewWorkOrderActivity.java
try {
JSONObject details = ((TestCrowd) getApplicationContext()).getPriorities();
JSONObject priority = details.getJSONObject("priorities"); /*Where the null value object reference located*/
JSONArray priority_arry = priority.getJSONArray("value");
for (int i = 0; i < priority_arry.length(); i++) {
arraySpinner_1.add(priority_arry.get(i).toString());
}
ArrayAdapter<String> spinnerAdepter_1 = new ArrayAdapter<String>(this,R.layout.spinner_item,arraySpinner_1);
spinner_1.setAdapter(spinnerAdepter_1);
} catch (JSONException e) {
e.printStackTrace();
}
And the error is
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.malith.testcrowd/com.example.malith.testcrowd.NewWorkOrderActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONObject org.json.JSONObject.getJSONObject(java.lang.String)' on a null object reference
I just need to extract priorities->values from the main Json
You are accessing the json object wrongly,
priorities is inside message-->enum-->priorities
JSONObject obj= ((TestCrowd) getApplicationContext()).getPriorities();
JSONObject message= obj.getJSONObject("message");
JSONObject enum= message.getJSONObject("enum");
JSONObject priorities = enum.getJSONObject("priorities");
JSONArray value=priorities .getJSONArray("value")
now do what every you want with value
Convert JSON as string using GSON and save it in Preference get as String and Convert in class object using GSON
check here for example
I'm having this response JSON that theorically follows the JSON API specs. I'm trying to parse with the moshi-jsonapi library but I don't know how to parse the some_objects relationship. In the SomeType class I have a member HasMany<SomeObject> someObjects and the class SomeObject is annotated in a proper way:
#JsonApi(type = "some_objects")
public class SomeObject extends Resource {
//....
}
But, after doing the parsing, I'm getting the someObjects member as null. Anyone knows why?
The JSON is that one:
"links": {
"self": "someurl/params"
},
"data": [
{
"type": "some_type",
"id": "12345",
"attributes": {
"attr1": 1,
"attr2": 2,
"attr3": 3
},
"relationships": {
"some_objects": {
"data": [
{
"type": "some_objects",
"id": "1"
},
{
"type": "some_objects",
"id": "2"
}
]
}
}
}
],
"included": [
{
"type": "some_objects",
"id": "1",
"attributes": {
"id": "1",
"parentId": "1"
},
"relationships": {
"subobjects": {
"data": [
{
"type": "subobjects",
"id": "2"
}
]
}
}
{
"type": "subobjects",
"id": "2",
"attributes": {
"metadata": {
"code": "AA"
},
"id": "2",
"parentId": "1"
}
}
],
"meta": {
"total": 1,
"totalCount": 1,
"correction": []
}
}
The only problem was the name of the member, after changing someObjects by 'some_objects' it worked like a charm.
i am getting some paring error while running this code but i can not tell what is the problem .there is no error in the android studio syntax but there is a problem in the json parsing or with my Json as i am not able to understand how to call json object inside another json object and json arrays
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.ImageView;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import org.json.JSONObject;
public class BookFragment extends Fragment {
OneRestaurantBean restaurentinfo;
String placid="ChIJO3JIy9iDGjkRIekztZhsmGg";
PhotosBean photolist;
ReviewBean reviewlist;
StringRequest stringRequest;
Spinner table_for;
PhotosBean photolist;
ReviewBean reviewlist;
private String format = "";
TextView date,time;
int day,month,year,hour,minute;
String url="https://maps.googleapis.com/maps/api/place/details/json?placeid="+placeid+"&key=AIzaSyDGtQbIzeOGRk0kGq3rCy3xAvo1xVNB304";
TextView txtaddress,txtphone,txttiming;
ImageView image1,image2,image3;
// Executes the Request
RequestQueue requestQueue;
ProgressDialog pd;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_book, container, false);
requestQueue = Volley.newRequestQueue(getActivity());
table_for=(Spinner)view.findViewById(R.id.spinner);
date=(TextView)view.findViewById(R.id.textViewdate) ;
// timePicker1=(TimePicker)view.findViewById(R.id.timePicker2);
time=(TextView)view.findViewById(R.id.textviewtime);
txtaddress=(TextView)view.findViewById(R.id.textViewaddress);
txtphone=(TextView)view.findViewById(R.id.textViewphone);
txttiming=(TextView)view.findViewById(R.id.textViewTiming);
image1=(ImageView)view.findViewById(R.id.imageView2);
image2=(ImageView)view.findViewById(R.id.imageView3);
image3=(ImageView)view.findViewById(R.id.imageView4);
init();
getRestaurentsinfo();
return view;
}
private void init() {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.Table_for, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
table_for.setAdapter(adapter);
}
private void getRestaurentsinfo() {
stringRequest = new StringRequest(com.android.volley.Request.Method.GET, url,
new com.android.volley.Response.Listener<String>() {
#Override
public void onResponse(String s) {
// pd.dismiss();
try {
JSONObject jsonObject = new JSONObject(s);
Gson gson = new Gson();
restaurentinfo = gson.fromJson(jsonObject.toString(), OneRestaurantBean.class);
Type listType = new TypeToken<List<PhotosBean>>(){}.getType();
photolist = (PhotosBean) gson.fromJson(jsonObject.toString(), listType);
Type listType2 = new TypeToken<List<ReviewBean>>(){}.getType();
reviewlist = (ReviewBean) gson.fromJson(jsonObject.toString(), listType2);
txtaddress.setText(restaurentinfo.getVicinity()); txttiming.setText(Arrays.toString(restaurentinfo.getOpenhour().getWeekday_text()).replaceAll("\\[|\\]", ""));
} catch (Exception e) {
Toast.makeText(getActivity(), "Some JSON Parsing Error", Toast.LENGTH_LONG).show();
}
}
},
// failure
new com.android.volley.Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(getActivity(), "Some Volley Error", Toast.LENGTH_LONG).show();
}
}
);
requestQueue.add(stringRequest);
}
my json file is as displayed below
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "Ludhiana",
"short_name" : "Ludhiana",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Sarabha Nagar",
"short_name" : "Sarabha Nagar",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Ludhiana",
"short_name" : "Ludhiana",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Punjab",
"short_name" : "PB",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
},
{
"long_name" : "141003",
"short_name" : "141003",
"types" : [ "postal_code" ]
}
],
"adr_address" : "SCF-21, Sarabha Nagar Market, \u003cspan class=\"extended-address\"\u003eSarabha Nagar\u003c/span\u003e, \u003cspan class=\"locality\"\u003eLudhiana\u003c/span\u003e, \u003cspan class=\"region\"\u003ePunjab\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e141003\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eIndia\u003c/span\u003e",
"formatted_address" : "SCF-21, Sarabha Nagar Market, Sarabha Nagar, Ludhiana, Punjab 141003, India",
"formatted_phone_number" : "0161 245 2810",
"geometry" : {
"location" : {
"lat" : 30.8927238,
"lng" : 75.8218431
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "17865244baa6a88165cbe83f21d2713656bcece0",
"international_phone_number" : "+91 161 245 2810",
"name" : "Domino's Pizza",
"opening_hours" : {
"open_now" : false,
"periods" : [
{
"close" : {
"day" : 0,
"time" : "2300"
},
"open" : {
"day" : 0,
"time" : "1100"
}
},
{
"close" : {
"day" : 1,
"time" : "2300"
},
"open" : {
"day" : 1,
"time" : "1100"
}
},
{
"close" : {
"day" : 2,
"time" : "2300"
},
"open" : {
"day" : 2,
"time" : "1100"
}
},
{
"close" : {
"day" : 3,
"time" : "2300"
},
"open" : {
"day" : 3,
"time" : "1100"
}
},
{
"close" : {
"day" : 4,
"time" : "2300"
},
"open" : {
"day" : 4,
"time" : "1100"
}
},
{
"close" : {
"day" : 5,
"time" : "2300"
},
"open" : {
"day" : 5,
"time" : "1100"
}
},
{
"close" : {
"day" : 6,
"time" : "2300"
},
"open" : {
"day" : 6,
"time" : "1100"
}
}
],
"weekday_text" : [
"Monday: 11:00 AM – 11:00 PM",
"Tuesday: 11:00 AM – 11:00 PM",
"Wednesday: 11:00 AM – 11:00 PM",
"Thursday: 11:00 AM – 11:00 PM",
"Friday: 11:00 AM – 11:00 PM",
"Saturday: 11:00 AM – 11:00 PM",
"Sunday: 11:00 AM – 11:00 PM"
]
},
"photos" : [
{
"height" : 1836,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/102359458524790058006/photos\"\u003eKAMALJEET SINGH RANDHAWA\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAAIhIrZLQ_Z3H9UUm8Hu96z7N5L4HdKPd_IJbGNEfzW_v2c6zNe3bSYoz3Ya2XkoBHM_9bXBmKbw72d8N_vmFPyT14uqWCHz_WuLWGp_K0Ss-u7VCghzyUpVZ1F5-asNOeTCNLOwjre0FdO8rQsgb4slyJhMGgJg8Okogx_jlShuLEhDdW69Zmuh-aB-SCdI0XdFDGhT4zjH4vKwmgNYkWdcnY53bwD4eaA",
"width" : 3264
},
{
"height" : 600,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/109327830988106108854/photos\"\u003eNirwal Karm\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAAPpyo99c8N-lt7F-l3YA7qSK7mZKY8zIyaMUoV3OqgOJ9DXVqspqpMQwIvu7KDycvrWcHpy6jaCpc2UyDm3SqtYhw2Fsr5IfgKjKbRTjDF-ST5a9w1vNs44O3SbHWwSLuKV6fhyax1xfqc2TEM7XqAE3K2XSOg5XCzgD8Nef3NJuEhAhTK3UI_yW-dim4bXP4ZK0GhQCO7kgf4E1f53t-_Tf4DEsjYF5xw",
"width" : 800
}
],
"place_id" : "ChIJAQAAwM2DGjkR6gVuwpeWwKk",
"price_level" : 1,
"rating" : 4.1,
"reference" : "CmRSAAAATiwel1VKVuyHEFkgl2LEMsL_Rxf9xV3EXj_N_o59Q0ZWGgy-XRjCTmFAPK6z1xRof9WpAKu9FTu-XaNyx6zxGlnzXVNRf-KUOjw-xBB5YdrdDN1VW7eN3E-qw6-pKdXREhCr9EeBMjBidk_KW74s9S9aGhS4NLG4UNMqWveymd0sBkpcieyybg",
"reviews" : [
{
"aspects" : [
{
"rating" : 2,
"type" : "overall"
}
],
"author_name" : "Vardaan Sharma",
"author_url" : "https://plus.google.com/116622040002550992578",
"language" : "en",
"profile_photo_url" : "//lh4.googleusercontent.com/-oc3UC8zHbqE/AAAAAAAAAAI/AAAAAAAACDE/eD2qok-jrMQ/photo.jpg",
"rating" : 4,
"text" : "Love The Garlic Bread!! Awesome Taste for Decent Prices!!!",
"time" : 1465754538
},
{
"aspects" : [
{
"rating" : 0,
"type" : "overall"
}
],
"author_name" : "Darleen Grewal",
"author_url" : "https://plus.google.com/102174339060680368258",
"language" : "en",
"rating" : 1,
"text" : "Pathetic delivery service , orders delayed by one and a half hour and staff is uneducated and non Cooperating",
"time" : 1476727634
},
{
"aspects" : [
{
"rating" : 2,
"type" : "overall"
}
],
"author_name" : "Abhishek Attri",
"author_url" : "https://plus.google.com/103724862705815258945",
"language" : "en",
"profile_photo_url" : "//lh6.googleusercontent.com/-8XVYso4qYxc/AAAAAAAAAAI/AAAAAAAAHIA/63UVRnCpc8M/photo.jpg",
"rating" : 4,
"text" : "Nice place.. ",
"time" : 1478198571
},
{
"aspects" : [
{
"rating" : 0,
"type" : "overall"
}
],
"author_name" : "Bhavesh Bansal",
"author_url" : "https://plus.google.com/107902290149968294793",
"language" : "en",
"profile_photo_url" : "//lh5.googleusercontent.com/-0GAn_nkPa9g/AAAAAAAAAAI/AAAAAAAAAro/1Z2WU1Tup7g/photo.jpg",
"rating" : 1,
"text" : "Poor service. Last 2 orders have been delayed by almost 2 hours. All their contact numbers are unreachable or busy, when you try to contact them.\nSeems like they dont care anymore about customer",
"time" : 1463848060
},
{
"aspects" : [
{
"rating" : 0,
"type" : "overall"
}
],
"author_name" : "Gurmeet Gujral",
"author_url" : "https://plus.google.com/113150303121450342768",
"language" : "en",
"profile_photo_url" : "//lh3.googleusercontent.com/-zzCCjI2B4HU/AAAAAAAAAAI/AAAAAAAAJ0w/JOBa4Aetq3E/photo.jpg",
"rating" : 1,
"text" : "Don't call them for home delivery. They will serve you cold",
"time" : 1439473587
}
],
"scope" : "GOOGLE",
"types" : [
"meal_delivery",
"meal_takeaway",
"restaurant",
"food",
"point_of_interest",
"establishment"
],
"url" : "https://maps.google.com/?cid=12231942166484485610",
"utc_offset" : 330,
"vicinity" : "SCF-21, Sarabha Nagar Market, Ludhiana",
"website" : "http://www.dominos.co.in/"
},
"status" : "OK"
}
I found error in json parsing,use some other parsing method such as
Retrofit . Volley JSON Parsing Sample Example
http://www.androidhive.info/2014/09/android-json-parsing-using-volley/
For example : if the given below is json of the person's profile on facebook got thorugh facebook sdk login through android app , How we will get the School Name fron the education Field in th json data in android . Please Help
Data :
{
"id": "1464730016",
"name": "Ravi Tamada",
"first_name": "Ravi",
"last_name": "Tamada",
"link": "https://www.facebook.com/ravi8x",
"username": "ravi8x",
"birthday": "12/22/1988",
"hometown": {
"id": "112158005464147",
"name": "Baruva"
},
"location": {
"id": "102186159822587",
"name": "Chennai, Tamil Nadu"
},
"bio": "Author: www.androidhive.info\r\nCo-author: www.9lessons.info",
"work": [
{
"employer": {
"id": "179366562092719",
"name": "ByteAlly"
},
"location": {
"id": "102186159822587",
"name": "Chennai, Tamil Nadu"
},
"position": {
"id": "124917314217511",
"name": "Product Head"
}
]
}
],
"favorite_athletes": [
{
"id": "18620649907",
"name": "Virat Kohli"
}
],
"education": [
{
"school": {
"id": "131587206873093",
"name": "Raghu Engineering College (REC)"
},
"degree": {
"id": "140065339390579",
"name": "B.Tech"
},
"year": {
"id": "142963519060927",
"name": "2010"
},
"type": "Graduate School",
"classes": [
{
"id": "192259410803415",
"name": "2010",
"with": [
{
"id": "584960408",
"name": "Santosh Patnaik"
}
],
"from": {
"id": "584960408",
"name": "Santosh Patnaik"
}
}
]
}
],
"gender": "male",
"relationship_status": "Single",
"website": "www.androidhive.info\nwww.9lessons.info\nwww.twitter.com/ravitamada\nwww.about.me/rv",
"timezone": 5.5,
"locale": "en_US",
"languages": [
{
"id": "106059522759137",
"name": "English"
},
{
"id": "107617475934611",
"name": "Telugu"
},
{
"id": "112969428713061",
"name": "Hindi"
},
{
"id": "343306413260",
"name": "Tamil"
}
],
"verified": true,
"updated_time": "2012-03-02T17:04:18+0000"
}
JSONObject jsonResult = new JSONObject(jsonUser);
JSONArray data = jsonResult.getJSONArray("education");
if(data != null)
{
for(int i = 0 ; i < data.length() ; i++)
{
JSONObject c = data.getJSONObject(i);
String type = c.getString("type");
if(type.equalsIgnoreCase("college"))
{
JSONObject school = c.getJSONObject("school");
String id2 = school.getString("id");
String name2 = school.getString("name");
JSONObject year = c.getJSONObject("year");
String id_y = school.getString("id");
String name_y = school.getString("name");
}
}
}
Supposing that you've this json into a jsonObject that you retrieve as response, this is the way:
// Get jsonArray 'education' from main jsonObject
JSONArray jsonArrayEducation = jsonObject.getJSONArray("education");
JSONObject jsonSchool = jsonArrayEducation.getJSONObject("school");
Note that if you are interested only at the name, you can group the two lines above into
JSONObject jsonSchool = jsonObject.getJSONArray("education").getJSONObject("school");
// get school name
String schoolName = jsonSchool.getString("name");