预期开始数组,但在改造中出现开始对象错误
Posted
技术标签:
【中文标题】预期开始数组,但在改造中出现开始对象错误【英文标题】:Expected Begin Array but was Begin Object error in Retrofit 【发布时间】:2021-05-03 23:50:31 【问题描述】:我是改造库的新手,在获得响应时遇到此错误, 下面是我的 JSON 响应和 Java 代码:
当我敬酒时,它给了我“预期的开始数组但是开始对象”的响应。我想将我的结果存储在 listview 中,并且我已经设置了我的视图并分配了值。但我无法从我使用的 API 获得响应。
我已经从 json2pojoschema 网站复制了我的模型类,所以模型类应该没有任何问题。如果仍然需要,请告诉我。
JSON:
"destination_addresses": [
"Unit no. 25-30, First Floor, Idea Cosmic Plaza, Block C 2, Palam Vihar, Gurugram, Haryana 122017, India",
"863/2, Ganga Vihar, Block A, Sector 12, Gurugram, Haryana 122001, India",
"Unnamed Road, Block C, South City I, Sector 41, Gurugram, Haryana 122003, India"
],
"origin_addresses": [
"964, Block C 2, Sector 3, Gurugram, Haryana 122017, India"
],
"rows": [
"elements": [
"distance":
"text": "0.2 km",
"value": 229
,
"duration":
"text": "1 min",
"value": 45
,
"status": "OK"
,
"distance":
"text": "4.1 km",
"value": 4094
,
"duration":
"text": "11 mins",
"value": 657
,
"status": "OK"
,
"distance":
"text": "9.1 km",
"value": 9092
,
"duration":
"text": "30 mins",
"value": 1791
,
"status": "OK"
]
],
"status": "OK"
APIClient 用于改造:
private static final String BASE_URL = "https://maps.googleapis.com/maps/";
private static ApiClientMaps apiClient;
private static Retrofit retrofit;
private ApiClientMaps()
HttpLoggingInterceptor httpLoggingInterceptor= new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build();
Gson gson = new GsonBuilder()
.setLenient()
.create();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
public static synchronized ApiClientMaps getInstance()
if(apiClient == null)
apiClient = new ApiClientMaps();
return apiClient;
public RetrofitMaps getApi()
return retrofit.create(RetrofitMaps.class);
活动代码:
public class LocationFragment extends Fragment
String vehicleType, damagetype, brandname, modelname, makeyear, locationstr;
private ListView listViewPlaces;
List<Example> results ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_location, container, false);
vehicleType = getArguments().getString("vehicletype");
damagetype = getArguments().getString("damagetype");
brandname = getArguments().getString("brandname");
modelname = getArguments().getString("modelname");
makeyear = getArguments().getString("makeyear");
locationstr = getArguments().getString("locationstr");
listViewPlaces = view.findViewById(R.id.listViewPlaces);
Getlist();
return view;
private void Getlist()
String units = "matric";
String key = String.valueOf(R.string.google_maps_key);
String destinations = "28.495430,77.020937";
Call<List<Example>> call = ApiClientMaps.getInstance().getApi().getNearbyPlaces(units, String.valueOf(locationstr), destinations, key);
call.enqueue(new Callback<List<Example>>()
@Override
public void onResponse(Call<List<Example>> call, Response<List<Example>> response)
if (response.isSuccessful())
assert response.body() != null;
results=response.body();
PlacesListAdapter placesListAdapter = new PlacesListAdapter(getContext(), results);
listViewPlaces.setAdapter(placesListAdapter);
else
Toast.makeText(getContext(), "Failed", Toast.LENGTH_LONG).show();
@Override
public void onFailure(Call<List<Example>> call, Throwable t)
Toast.makeText(getContext(), "Fail" + t.getMessage(), Toast.LENGTH_LONG).show();
);
界面:
public interface RetrofitMaps
@POST("api/distancematrix/json")
Call<List<Example>> getNearbyPlaces(
@Query("units") String units,
@Query("origins") String origin,
@Query("destinations") String destination,
@Query("key") String key);
主模型类:
例子:
public class Example
@SerializedName("destination_addresses")
@Expose
private List<String> destinationAddresses = null;
@SerializedName("origin_addresses")
@Expose
private List<String> originAddresses = null;
@SerializedName("rows")
@Expose
private List<Row> rows = null;
@SerializedName("status")
@Expose
private String status;
public Example()
public Example(List<String> destinationAddresses, List<String> originAddresses, List<Row> rows, String status)
super();
this.destinationAddresses = destinationAddresses;
this.originAddresses = originAddresses;
this.rows = rows;
this.status = status;
public List<String> getDestinationAddresses()
return destinationAddresses;
public void setDestinationAddresses(List<String> destinationAddresses)
this.destinationAddresses = destinationAddresses;
public List<String> getOriginAddresses()
return originAddresses;
public void setOriginAddresses(List<String> originAddresses)
this.originAddresses = originAddresses;
public List<Row> getRows()
return rows;
public void setRows(List<Row> rows)
this.rows = rows;
public String getStatus()
return status;
public void setStatus(String status)
this.status = status;
行.java:
public class Row
@SerializedName("elements")
@Expose
private List<Element> elements = null;
public Row(List<Element> elements)
super();
this.elements = elements;
public List<Element> getElements()
return elements;
public void setElements(List<Element> elements)
this.elements = elements;
我想在列表视图中获取和存储数据,请帮助
【问题讨论】:
请发布原始模型类。 我已经发布了行模型类,请检查 这能回答你的问题吗? Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 不,我无法理解那个答案。 【参考方案1】:尝试更改此设置
@POST("api/distancematrix/json")
Call<List<Example>> getNearbyPlaces(
@Query("units") String units,
@Query("origins") String origin,
@Query("destinations") String destination,
@Query("key") String key);
到
@POST("api/distancematrix/json")
Call<Example> getNearbyPlaces(
@Query("units") String units,
@Query("origins") String origin,
@Query("destinations") String destination,
@Query("key") String key);
以及与此相关的其他更改
因为您的响应是 JsonObject 而不是 JsonArray
【讨论】:
其实我之前试过了,响应是成功的,但是我无法将它存储到listview。 这可能是另一个问题。我已经为您在问题标题中提到的问题编写了解决方案 如何在列表视图中存储对象然后你能帮我吗? 如果您在显示列表视图时遇到问题,请相应地更改问题或使用与该问题相关的代码发布新问题。以便有人可以帮助您解决该特定问题 适配器期望列表,但响应作为对象返回,我该怎么办?以上是关于预期开始数组,但在改造中出现开始对象错误的主要内容,如果未能解决你的问题,请参考以下文章
快速 API 错误:预期 BEGIN_ARRAY 但 BEGIN_OBJECT 在第 1 行第 2 列 PATH $ 通过使用改造
如何修复预期的字符串,但在带有改造的嵌套数组上是 BEGIN_OBJECT
IE会引发预期的冒号错误,适用于Chrome吗?具有对象文字和函数的对象数组
预期 BEGIN_OBJECT 但在第 1 行第 1 列路径为 STRING - Laravel 到改造 2