我想从改造中接收数据,并在 ListView
中设置它,但我有一个例外
改造。错误:com.google.gson。JsonSyntaxException:java.lang.IllegalStateException:应为BEGIN_$
我必须做什么?
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
当响应在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;
}
}
在阅读@Ridcully的响应之后,我想问一下是否有一种方法可以更新,以便它知道JSON是一个数组。比如?
我有以下JSON结构: 以下是我的: 下面是我的POJO类: 最后,这里是我正在进行网络调用的代码:
目前,我正在得到 com.google.gson.jsonSyntaxException:java.lang.IllegalStateException:应为BEGIN_OBJECT,但在第1行第2列路径$处为BEGIN_ARRAY 请帮帮我.
我正在学习youtube视频的改装,但现在我卡住了。它显示一个错误“reverfit expected begin_array but was begin_object at line 1 column 2 path$”我正在尝试从这个站点获取json数据。http://servicio-monkydevs.rhcloud.com/clientes/ ClientService.java clie
我使用django作为服务器端 在django中,我像这样返回Json 我不知道为什么会产生问题
我知道这已经不是第一次有人问这个问题了,但是在Treverfit2中,我找不到解决我问题的正确方法。我看了一个在线教程,效果很好。当我将相同的代码应用到我自己的endpoint时,我得到了这个异常:我不知道如何解决这个问题。 接口: 但这个不是: 我的音乐课: