我在运行此代码时遇到了一些配对错误,但我不知道是什么问题。在android studio语法中没有错误但是在json解析或我的Json中存在问题,因为我无法了解如何在另一个json对象和json数组中调用json对象
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);
}
我的json文件如下所示
{
"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"
}
答案 0 :(得分:1)
我在json解析中发现错误,使用其他一些解析方法,如 改造。 Volley JSON解析示例示例
http://www.androidhive.info/2014/09/android-json-parsing-using-volley/