我是初学者,我在我的应用中使用 Retrofit 2 。我有 JSON 。 (我尝试了很多解决方案,但对我没什么用)。谢谢你的帮助
我有错误:预计BEGIN_ARRAY但第2行第2行是BEGIN_OBJECT
JSON
{
responseCode: 1,
responseCodeText: "ok",
response: [
{
lat: 67.2422432322,
lng: 25.1441441441,
title: "Point na mapě 1",
desc: "Lorem ipsum dolor sit amet, consectetur adipisicing elit.\nVero praesentium fugiat nobis pariatur cupiditate saepe dolorum, soluta dignissimos.",
photo: [
"http://photo3.jpg",
"http://photo4.jpg",
]
},
{
lat: 39.1234787,
lng: 25.2242445,
title: "Point na mapě 2",
desc: "Possimus veritatis, neque a et odit ad itaque iusto asperiores perspiciatis voluptates,\nvero praesentium fugiat nobis pariatur cupiditate saepe dolorum, soluta dignissimos.",
photo: [
"http://photo1.jpg",
"http://photo2.jpg",
]
},
//other
接口
public interface PointsInterface {
String BASE_URL = "MY_URL/";
@POST("getPointsOnMap")
Call<List<PointsOnMap>> getPointsOnMap();
class Factory {
private static PointsInterface service;
public static PointsInterface getInstance() {
if (service == null) {
Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(BASE_URL).build();
service = retrofit.create(PointsInterface.class);
return service;
} else {
return service;
}
}
}
}
API调用
// apiService
public void serviceInit() {
PointsInterface.Factory.getInstance().getPointsOnMap().enqueue(new Callback<List<PointsOnMap>>() {
@Override
public void onResponse(Call<List<PointsOnMap>> call, Response<List<PointsOnMap>> response) {
List<PointsOnMap> result = response.body();
Log.d("response", "Number of points received: " + result.size());
}
@Override
public void onFailure(Call<List<PointsOnMap>> call, Throwable t) {
Log.e("error", "Error");
}
});}
模型
public class PointsOnMap {
@SerializedName("lat") private Double lat;
@SerializedName("lng") private Double lng;
@SerializedName("title") private String title;
@SerializedName("desc") private String desc;
public PointsOnMap(Double lat, Double lng, String title, String desc) {
this.lat = lat;
this.lng = lng;
this.title = title;
this.desc = desc;
}
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLng() { return lng; }
public void setLng(Double lng) {
this.lng = lng;
}
public String getTitle() { return title; }
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
答案 0 :(得分:2)
替换
@POST("getPointsOnMap")
Call<List<PointsOnMap>> getPointsOnMap();
到
@POST("getPointsOnMap")
Call<PointsOnMap> getPointsOnMap();
以及替换
public void serviceInit() {
PointsInterface.Factory.getInstance().getPointsOnMap().enqueue(new Callback<List<PointsOnMap>>() {
@Override
public void onResponse(Call<List<PointsOnMap>> call, Response<List<PointsOnMap>> response) {
List<PointsOnMap> result = response.body();
Log.d("response", "Number of points received: " + result.size());
}
@Override
public void onFailure(Call<List<PointsOnMap>> call, Throwable t) {
Log.e("error", "Error");
}
});}
到
public void serviceInit() {
PointsInterface.Factory.getInstance().getPointsOnMap().enqueue(new Callback<PointsOnMap>() {
@Override
public void onResponse(Call<PointsOnMap> call, Response<PointsOnMap> response) {
PointsOnMap result = response.body();
Log.d("response", "Number of points received: " + result.size());
}
@Override
public void onFailure(Call<PointsOnMap> call, Throwable t) {
Log.e("error", "Error");
}
});}
答案 1 :(得分:1)
这是因为
@POST("getPointsOnMap")
Call<List<PointsOnMap>> getPointsOnMap();
在此处删除列表,您的错误将会解决。
@POST("getPointsOnMap")
Call<PointsOnMap> getPointsOnMap();
祝你好运
答案 2 :(得分:1)
您的Retrofit界面
@POST("getPointsOnMap")
Call<List<PointsOnMap>> getPointsOnMap();
应该是
@POST("getPointsOnMap")
Call<PointsOnMapResponse> getPointsOnMap();
其中PointsOnMapResponse
是
public class PointsOnMapResponse {
@SerializedName("responseCode")
long responseCode;
@SerializedName("responseCodeText")
String responseCodeText;
@SerializedName("response")
List<PointsOnMap> response;
//getters, setters
}