Some json parsing error in android using volley and json - android

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/

Related

Google Places Parse(Volley)

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 );

How to get String PlaceTypes from Integer value?

I am new to learning Google Places API and have been searching for an answer to this problem without much luck.
So in the first part of my app, I wish to retrieve the placeTypes of my current location. I have successfully done that, but it returns a list of Integer values, which then correlate to different Place.TYPE_*s.
My overall goal, though, is to get the current placeTypes of my location, then do a PlaceSearch via URL. To do so, I need to convert the Integers in my list of the location, to the string types so I can insert them into my URL.
Note: I have tried inserting the integer value for "restaurant" instead of just the word "restaurant" in the URL, but it returned wrong results such as hotels, office buildings that were not returned when I used the word instead.
For example, my URL would look like:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + lat + "," + lng+"&radius=2000&type=restaurant&key=my_key
Notice the word "restaurant" in the URL. That is where I have replaced it with the Integer value and it did not return correct results.
[EDITED]
You can try this.
private String getPlaceTypeForValue(int value) throws Exception {
Field[] fields = Place.class.getDeclaredFields();
String name;
for (Field field : fields) {
name = field.getName().toLowerCase();
if (name.startsWith("type_") && field.getInt(null) == value) {
return name.replace("type_", "");
}
}
throw new IllegalArgumentException("place value " + value + " not found.");
}
I created a test to validate it, should work, just missing some types: administrative_area_level_4, administrative_area_level_5, postal_code_suffix and street_number.
public class ConvertingPlaceTypesUnitTest {
private enum Types {
accounting,
airport,
amusement_park,
aquarium,
art_gallery,
atm,
bakery,
bank,
bar,
beauty_salon,
bicycle_store,
book_store,
bowling_alley,
bus_station,
cafe,
campground,
car_dealer,
car_rental,
car_repair,
car_wash,
casino,
cemetery,
church,
city_hall,
clothing_store,
convenience_store,
courthouse,
dentist,
department_store,
doctor,
electrician,
electronics_store,
embassy,
establishment,
finance,
fire_station,
florist,
food,
funeral_home,
furniture_store,
gas_station,
general_contractor,
grocery_or_supermarket,
gym,
hair_care,
hardware_store,
health,
hindu_temple,
home_goods_store,
hospital,
insurance_agency,
jewelry_store,
laundry,
lawyer,
library,
liquor_store,
local_government_office,
locksmith,
lodging,
meal_delivery,
meal_takeaway,
mosque,
movie_rental,
movie_theater,
moving_company,
museum,
night_club,
painter,
park,
parking,
pet_store,
pharmacy,
physiotherapist,
place_of_worship,
plumber,
police,
post_office,
real_estate_agency,
restaurant,
roofing_contractor,
rv_park,
school,
shoe_store,
shopping_mall,
spa,
stadium,
storage,
store,
subway_station,
synagogue,
taxi_stand,
train_station,
travel_agency,
university,
veterinary_care,
zoo,
administrative_area_level_3,
// administrative_area_level_4, Not in Place.class
// administrative_area_level_5, Not in Place.class
colloquial_area,
floor,
geocode,
intersection,
natural_feature,
neighborhood,
political,
point_of_interest,
post_box,
postal_code_prefix,
// postal_code_suffix, Not in Place.class
postal_town,
premise,
room,
route,
street_address,
// street_number, Not in Place.class
sublocality_level_4,
sublocality_level_5,
sublocality_level_3,
sublocality_level_2,
sublocality_level_1,
subpremise,
transit_station,
locality,
sublocality,
postal_code,
country,
administrative_area_level_1,
administrative_area_level_2
};
#Test
public void extracPlaces() throws Exception {
assertEquals("accounting", getPlaceTypeForValue(Place.TYPE_ACCOUNTING));
assertEquals("local_government_office", getPlaceTypeForValue(Place.TYPE_LOCAL_GOVERNMENT_OFFICE));
}
#Test
public void testAllTypes() throws Exception {
for (Types type : Types.values()) {
final String name = type.toString();
int value = getPlaceTypeValue("TYPE_" + name.toUpperCase());
assertEquals(name, getPlaceTypeForValue(value));
}
}
private int getPlaceTypeValue(String fieldName) throws Exception {
Field field = Place.class.getDeclaredField(fieldName);
return field.getInt(null);
}
private String getPlaceTypeForValue(int value) throws IllegalAccessException {
Field[] fields = Place.class.getDeclaredFields();
String name;
for (Field field : fields) {
name = field.getName().toLowerCase();
if (name.startsWith("type_") && field.getInt(null) == value) {
return name.replace("type_", "");
}
}
throw new IllegalArgumentException("place value " + value + " not found.");
}
}
[OLD]
I didn't understand where type integer is coming from, in documentation it's always referred as string.
type — Restricts the results to places matching the specified type. Only one type may be specified (if more than one type is provided, all types following the first entry are ignored). See the list of supported types.
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "48",
"short_name" : "48",
"types" : [ "street_number" ]
},
{
"long_name" : "Pirrama Road",
"short_name" : "Pirrama Road",
"types" : [ "route" ]
},
{
"long_name" : "Pyrmont",
"short_name" : "Pyrmont",
"types" : [ "locality", "political" ]
},
{
"long_name" : "NSW",
"short_name" : "NSW",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "AU",
"short_name" : "AU",
"types" : [ "country", "political" ]
},
{
"long_name" : "2009",
"short_name" : "2009",
"types" : [ "postal_code" ]
}
],
"adr_address" : "5,
\u003cspan class=\"street-address\"\u003e48 Pirrama Rd\u003c/span\u003e,
\u003cspan class=\"locality\"\u003ePyrmont\u003c/span\u003e
\u003cspan class=\"region\"\u003eNSW\u003c/span\u003e
\u003cspan class=\"postal-code\"\u003e2009\u003c/span\u003e,
\u003cspan class=\"country-name\"\u003eAustralia\u003c/span\u003e",
"formatted_address" : "48 Pirrama Road, Pyrmont NSW, Australia",
"formatted_phone_number" : "(02) 9374 4000",
"geometry" : {
"location" : {
"lat" : -33.8669710,
"lng" : 151.1958750
},
"viewport" : {
"northeast" : {
"lat" : -33.8665053,
"lng" : 151.1960371
},
"southwest" : {
"lat" : -33.8669293,
"lng" : 151.1952183
}
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "4f89212bf76dde31f092cfc14d7506555d85b5c7",
"international_phone_number" : "+61 2 9374 4000",
"name" : "Google Sydney",
"place_id" : "ChIJN1t_tDeuEmsRUsoyG83frY4",
"scope" : "GOOGLE",
"alt_ids" : [
{
"place_id" : "D9iJyWEHuEmuEmsRm9hTkapTCrk",
"scope" : "APP"
}
],
"rating" : 4.70,
"reference" : "CnRsAAAA98C4wD-VFvzGq-KHVEFhlHuy1TD1W6UYZw7KjuvfVsKMRZkbCVBVDxXFOOCM108n9PuJMJxeAxix3WB6B16c1p2bY1ZQyOrcu1d9247xQhUmPgYjN37JMo5QBsWipTsnoIZA9yAzA-0pnxFM6yAcDhIQbU0z05f3xD3m9NQnhEDjvBoUw-BdcocVpXzKFcnMXUpf-nkyF1w",
"reviews" : [
{
"aspects" : [
{
"rating" : 3,
"type" : "quality"
}
],
"author_name" : "Simon Bengtsson",
"author_url" : "https://plus.google.com/104675092887960962573",
"language" : "en",
"rating" : 5,
"text" : "Just went inside to have a look at Google. Amazing.",
"time" : 1338440552869
},
{
"aspects" : [
{
"rating" : 3,
"type" : "quality"
}
],
"author_name" : "Felix Rauch Valenti",
"author_url" : "https://plus.google.com/103291556674373289857",
"language" : "en",
"rating" : 5,
"text" : "Best place to work :-)",
"time" : 1338411244325
},
{
"aspects" : [
{
"rating" : 3,
"type" : "quality"
}
],
"author_name" : "Chris",
"language" : "en",
"rating" : 5,
"text" : "Great place to work, always lots of free food!",
"time" : 1330467089039
}
],
"types" : [ "establishment" ],
"url" : "http://maps.google.com/maps/place?cid=10281119596374313554",
"vicinity" : "48 Pirrama Road, Pyrmont",
"website" : "http://www.google.com.au/"
},
"status" : "OK"
}

Google map search place in Android

I am working on Android application in which I want to get the coordinates of the search place. Right now I am only getting the names and description of the place but not the coordinates that it Latitude and Longitude. My URL and response is given below.
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=karachi&sensor=true&key=MY_KEY
{
"predictions" : [
{
"description" : "Karachi Darbar - 10th Street - Oud Metha - Dubai - United Arab Emirates",
"id" : "66487f019fefe55e2fdd889a5902661dd8bce4eb",
"matched_substrings" : [
{
"length" : 7,
"offset" : 0
}
],
"place_id" : "ChIJW_YoydNCXz4RwTx4gOxOrTg",
"reference" : "CmRVAAAAZ2JaZnVCxKFxn3qtlI0adppFvEJoe-D3NUQWNN0AIfUl0yhKFVm6F_yt2VUGW3x4j5DN6y5jxgkpgVNGCMgE7gxnZ2D1oHpZMEyeuaPgQBmrhP4OrsK0K5ZqLJJPNUOVEhBUBctSVLyD65tVUo0H3jeFGhSln3l44jAdvrmAQi6kSEOEdfHhPg",
"terms" : [
{
"offset" : 0,
"value" : "Karachi Darbar"
},
{
"offset" : 17,
"value" : "10th Street"
},
{
"offset" : 31,
"value" : "Oud Metha"
},
{
"offset" : 43,
"value" : "Dubai"
},
{
"offset" : 51,
"value" : "United Arab Emirates"
}
],
"types" : [ "establishment" ]
},
{
"description" : "Karachi Darbar - 33 B Street - Al Karama - Dubai - United Arab Emirates",
"id" : "bb3e1227cce8b23d3ff172a3285db2defd742de7",
"matched_substrings" : [
{
"length" : 7,
"offset" : 0
}
],
"place_id" : "ChIJB4u8R9dCXz4RUeKUXKVjgOE",
"reference" : "CmRWAAAAfVwdVthVlSfEK10__jrSkqQpBCmc_JILX3Ya7eA09oiweiGwCoq8MoqVzQ9BTOnN7MWfD5RTL83jlWcTvypFEEsMliHpBhsytKTiCAXMn90ffjcS037VRziEBKW53WbsEhCoOZZ7H2jv_GtaP52wA2FhGhQsw8ltBOqHCxEK1gTefMfQ4qzzbA",
"terms" : [
{
"offset" : 0,
"value" : "Karachi Darbar"
},
{
"offset" : 17,
"value" : "33 B Street"
},
{
"offset" : 31,
"value" : "Al Karama"
},
{
"offset" : 43,
"value" : "Dubai"
},
{
"offset" : 51,
"value" : "United Arab Emirates"
}
],
"types" : [ "establishment" ]
},
{
"description" : "Karachi Darbar - Al Karama - Dubai - United Arab Emirates",
"id" : "7ab1b31e281218ec69777aede6d28b3bf5a70f90",
"matched_substrings" : [
{
"length" : 7,
"offset" : 0
}
],
"place_id" : "ChIJb3yfr95CXz4RU4qzVfNY5Bs",
"reference" : "ClRHAAAAlB1PCqHp8_JAMSmpWa29T9XiQ3Wa9qh6ttuC1wh9L3axfEAngculrdf989Z0WrohwyUcytxkPLIH9CZzddphWz26R1b8NkJuCKuQKTqKEb0SEMvw3t6N-ZMX5MCeW9Gh10QaFKSoBa8LaQTjcIocrwlvHumQEIoT",
"terms" : [
{
"offset" : 0,
"value" : "Karachi Darbar"
},
{
"offset" : 17,
"value" : "Al Karama"
},
{
"offset" : 29,
"value" : "Dubai"
},
{
"offset" : 37,
"value" : "United Arab Emirates"
}
],
"types" : [ "establishment" ]
},
{
"description" : "Karachi, Sindh, Pakistan",
"id" : "e65bd2b56cea82ce2b33391e9deee4951396ece7",
"matched_substrings" : [
{
"length" : 7,
"offset" : 0
}
],
"place_id" : "ChIJv0sdZQY-sz4RIwxaVUQv-Zw",
"reference" : "CjQwAAAAqul1rii1VJ6L-fd1hiMQbpvPLHyAwaomCTG_6l_7PPkz1jwqWqkl51bavj1CGLdKEhAk6GFORGWurnJAqE2xzm29GhQoVDUqp11bs7O_B8DOLpTumCAgzw",
"terms" : [
{
"offset" : 0,
"value" : "Karachi"
},
{
"offset" : 9,
"value" : "Sindh"
},
{
"offset" : 16,
"value" : "Pakistan"
}
],
"types" : [ "locality", "political", "geocode" ]
},
{
"description" : "Karachi Darbar Restaurant - Al Mateena Street - Deira - Dubai - United Arab Emirates",
"id" : "7b28747c93dc316b477e11e75bede5da26d7bdab",
"matched_substrings" : [
{
"length" : 7,
"offset" : 0
}
],
"place_id" : "ChIJLbxiB7dcXz4RUy9ny3FXR9Y",
"reference" : "CnRjAAAAs97khr1wHc1GeYxYTt2pRhv9NJPpNtekSGXLz22C6SJfo-6MKapTrnbAg1F_GzwDOuj87kiQe4inXDhFdBqcdH1a7BColMli3wRmuR76ib3AV4ouLuqqHYrZ0IrVeRsNDtjEHfPWyiKsLFrgB9jcIxIQ3z3-VFwCHUHHYa1_PvH6SxoUa7crfBXrD-Ma7Sr2pJGjoagJpeU",
"terms" : [
{
"offset" : 0,
"value" : "Karachi Darbar Restaurant"
},
{
"offset" : 28,
"value" : "Al Mateena Street"
},
{
"offset" : 48,
"value" : "Deira"
},
{
"offset" : 56,
"value" : "Dubai"
},
{
"offset" : 64,
"value" : "United Arab Emirates"
}
],
"types" : [ "establishment" ]
}
],
"status" : "OK"
}
You need to use Place ID tag to get Lat/Long
https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJb3yfr95CXz4RU4qzVfNY5Bs&key=AddYourOwnKeyHere
The best way is that you have to use the geocode api of google like as
http://maps.google.com/maps/api/geocode/json?address=karachi&sensor=false
If the limited access then no need any google api key.
In response, there is a json object "location" inside "geometry" where lat and lng present which will give you a co-ordinate of your entered location (karachi)
geometry: {
bounds: {
northeast: {
lat: 25.2703106,
lng: 67.46845189999999
},
southwest: {
lat: 24.7519801,
lng: 66.6609568
}
},
location: {
lat: 24.8614622,
lng: 67.0099388
},
location_type: "APPROXIMATE",
viewport: {
northeast: {
lat: 25.2703106,
lng: 67.46845189999999
},
southwest: {
lat: 24.7519801,
lng: 66.6609568
}
}
}

No values for JSON array

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");

Comments in Google Place

I saw in the official documentation of google places : https://developers.google.com/places/documentation/search
that the response gave from the server to search is :
"results" : [
{
"formatted_address" : "529 Kent Street, Sydney NSW, Australia",
"geometry" : {
"location" : {
"lat" : -33.8750460,
"lng" : 151.2052720
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "827f1ac561d72ec25897df088199315f7cbbc8ed",
"name" : "Tetsuya's",
"rating" : 4.30,
"reference" : "CnRmAAAAmmm3dlSVT3E7rIvwQ0lHBA4sayvxWEc4nZaXSSjRtfKRGoYnfr3d5AvQGk4e0u3oOErXsIJwtd3Wck1Onyw6pCzr8swW4E7dZ6wP4dV6AsXPvodwdVyqHgyGE_K8DqSp5McW_nFcci_-1jXb5Phv-RIQTzv5BjIGS0ufgTslfC6dqBoU7tw8NKUDHg28bPJlL0vGVWVgbTg",
"types" : [ "restaurant", "food", "establishment" ]
}...
I would like to know if it is posible to reach info like comments,
basically to reach more info that those 8 variables.
Tx !
it is disponible en :
"results" : [
"reviews" : [
{
"aspects" : [
{
"rating" : 3,
"type" : "quality"
}
],
"author_name" : "Simon Bengtsson",
"author_url" : "https://plus.google.com/104675092887960962573",
"text" : "Just went inside to have a look at Google. Amazing.",
"time" : 1338440552869
},
{
"aspects" : [
{
"rating" : 3,
"type" : "quality"
}
],
"author_name" : "Felix Rauch Valenti",
"author_url" : "https://plus.google.com/103291556674373289857",
"text" : "Best place to work :-)",
"time" : 1338411244325
},
{
"aspects" : [
{
"rating" : 3,
"type" : "quality"
}
],
"author_name" : "Chris",
"text" : "Great place to work, always lots of free food!",
"time" : 1330467089039
}
],
"types" : [ "establishment" ],
"url" : "http://maps.google.com/maps/place?cid=10281119596374313554",
"vicinity" : "48 Pirrama Road, Pyrmont",
"website" : "http://www.google.com.au/"
},
Official page: https://developers.google.com/places/documentation/details

Categories

Resources