我想从改造中接收数据,并在ListView
中设置,但我有一个例外
retrofit.RetrofitError:com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:预期BEGIN_ARRAY但是 BEGIN_OBJECT在第1行第2列路径$
我该怎么办?
public class Retrofit {
private static final String ENDPOINT = "https://maps.googleapis.com/maps/api/place";
private static ApiInterface apiInterface;
interface ApiInterface {
@GET("/nearbysearch/json?location=49.9944422,36.2368201&radius=500&types=food&key=AIzaSyDQZoJKznf0uRkvJFqLYbZ7-1GeGudjmv0")
void getBars(Callback<List<Bars>> callback);
}
static {
init();
}
private static void init() {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(ENDPOINT)
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
apiInterface = restAdapter.create(ApiInterface.class);
}
public static void getBars(Callback<List<Bars>> callback) {
apiInterface.getBars(callback);
}
}
public class Bars implements Serializable {
String name;
String vicinity;
public Bars(String name, String vicinity) {
this.name = name;
this.vicinity = vicinity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVicinity() {
return vicinity;
}
public void setVicinity(String vicinity) {
this.vicinity = vicinity;
}
}
Retrofit.getBars(new Callback<List<Bars>>() {
@Override
public void success(List<Bars> bars, Response response) {
listView.setAdapter(new MyAdapter(MainActivity.this, bars));
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), "Something Wrong", Toast.LENGTH_SHORT).show();
Log.d("Retrofit error", error.toString());
}
});
class MyAdapter extends ArrayAdapter<Bars> {
public MyAdapter(Context context, List<Bars> objects) {
super(context, R.layout.list_item, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.textNameOfBar = (TextView) rowView.findViewById(R.id.name_id);
holder.textVicinity = (TextView) rowView.findViewById(R.id.vicinity_id);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
Bars bars = getItem(position);
holder.textNameOfBar.setText(bars.getName());
holder.textVicinity.setText(bars.getVicinity());
return rowView;
}
class ViewHolder {
public TextView textNameOfBar;
public TextView textVicinity;
}
}
数据如下:
D/Retrofit: ogle.com/maps/contrib/111140660648735044302/photos\"\u003eОтель Гостиный Двор\u003c/a\u003e"
],
"photo_reference": "CmRdAAAA0qow_LjH6-RujURITuXtpvNeQpJ0xaIy7dL80VRhLjp-OMIXJYsIGj0XlkB6SRiORlmW_RxqbkGGaM2yyB6bOkz3RBSwzVmgtP3kLeN1JAwxpW62LFn2hvdc_921_JNZEhDfmW37jl6FYQ4mLtYZDwA0GhTFUWldj0Bbi1EdkzBrjcu0xVeqgA",
"width": 627
}
],
"place_id": "ChIJiZnTsuWgJ0ERVUB0bbViq7k",
"reference": "CnRmAAAAEF4DAZnJz_OxXVua7ChBhxS7CQFgMNlkA1c-8MnzeqVoIWRwyMTQOr7bMcjI16jRmFs5uYc6_Zb-9YTjjamXgYzO6ymZbrmR8tmA0alkr8CcmI37lUrKJvAUYy5l2-O80EVq314RWp7Set0bfcLzwRIQE1WYSj1Vasoa4eZ0kAW49hoU4JT9ABn_MCbF_s8PTDVpE86HfEk",
"scope": "GOOGLE",
"types": ["restaurant", "food", "lodging", "point_of_interest", "establishment"],
"vicinity": "Rimarskaya Str.28, Kharkiv"
}, {
"geometry": {
"location": {
"lat": 49.994499,
"lng": 36.237983
}
},
"icon": "https://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png",
"id": "6a63d528bbd4d7461fe1062f3e10f2575d51574f",
"name": "Reikartz Kharkiv",
"opening_hours": {
"open_now": true,
"weekday_text": []
},
"photos": [{
"height": 768,
"html_attributions": [
"\u003ca href=\"https://maps.google.com/maps/contrib/104215460438823286508/photos\"\u003eReikartz Харьков\u003c/a\u003e"
],
"photo_reference": "CmRdAAAA8lNiz4Z2yK0AD20LLblcrDoWvGVjrp3datMDcq2Sj9V7ktRA6pajcQ1v_TlwhHVNrm-73MwUGZqnlVgQLS3EzNwfzHhs2irIMzO2oqg75dVBFpCYGP57PeuhWDJ-W53UEhCF71N-dozQfnzsaLeYnBUjGhQYbbIHm_9RjSyB3dKT9Nazlch8BQ",
"width": 1024
}],
"place_id": "ChIJaSzvVO6gJ0ERtIpreEKDltw",
"rating": 4.6,
"reference": "CnRkAAAA1U-Hp0Nep02UFPtGcfn_W_qTYdAzPjv5teMOr2tNeNdaGHjfHX1riTR8JMG3rNyIXJTpXnjI5ZtfivNCf94pF-IzX39guAmTRRvLkXGBOS4z1Ty9_sgOK5DmZx6UKNeMS_SfZFA0j5OfeGtDlXI9JhIQ4MEkLnOpvIzNhfSOfgYHjRoUAeoSxp0NqOlmvnFsS8HGaUCdBsk",
"scope": "GOOGLE",
"types": ["restaurant", "food", "lodging", "point_of_interest", "establishment"],
"vicinity": "Sadova Street, 4, Kharkiv"
}, {
"geometry": {
"location": {
"lat": 49.9904245,
"lng": 36.2344184
}
},
"icon": "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id": "1d18eefa6be027ae1a18c9ed421917dfab8fbe8c",
"name": "МИЛЕНА, РЕСТОРАН",
"place_id": "ChIJI34e5fOgJ0ER6vn5PcHDGeA",
"reference": "CoQBcgAAAENtH6QLvT7_n8LlEsWSpMgQ562g5BDsAKdN_P98RRAs-WEsecVZ255_5TPWQTFeuZwuq8s7q8-VukDDnAYai3PHlu8Uy2r9H7sbIxkN5HxYs5KFFs1WAWESN8GtaaCTJzz6gkHeoYUyP0jb5wgxnPKWMu6lWLVUO3m2BZueQ3vOEhAwht5-huQPLsVSnpNROXVRGhSjF6AX4cChwhvKweczKsDO7kZBfg",
"scope": "GOOGLE",
"types": ["restaurant", "food", "point_of_interest", "establishment"],
"vicinity": "Korolenka Lane, 4, Kharkiv"
}, {
"geometry": {
"location": {
"lat": 49.99132549999999,
"lng": 36.2320451
}
},
"icon": "https://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png",
"id": "a7bab463a9623445a7f9e82ef18982abfe131d7c",
"name": "Kofeynya F.R.I.E.N.D.S",
"photos": [{
"height": 2448,
"html_attributions": [
"\u003ca href=\"https://maps.google.com/maps/contrib/112321527648150256042/photos\"\u003eВладимир опенченко\u003c/a\u003e"
],
"photo_reference": "CmRdAAAA6BLiXNDxmlvAo7wbBg_NakRA_mIlLoBUNhGMQ3ArVQGC6Li5m17-cjQzpQmdgSli8QWFVAu-UVk-jRyCkacQ7SHiKgHhvkwZDXWArutJChOYIs5uZabItJ-mHIgxuahYEhBsPO1M294IfxJ8b_19aH8MGhSC37RX
答案 0 :(得分:0)
当响应在JSONObject中并且你创建了JSONArray POJO时出现了这个错误。我这样解决了我的问题。 使JSONObject POJO如下所示:
public class GetEventResult {
@SerializedName("event")
@Expose
private Event event;
public Event getEvent() {
return event;
}
public void setEvent(Event event) {
this.event = event;
}
}
而不是:
public class OneRegistryBeanResult {
@SerializedName("event")
@Expose
private List<Event> event = new ArrayList<Event>();
public List<Event> getEvent() {
return event;
}
public void setEvent(List<Event> event) {
this.event = event;
}
}