我想解析Gson信息但出现错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我想解析Gson信息但出现错误相关的知识,希望对你有一定的参考价值。
我正在尝试发送源和目标的数据。
public void sendPost(final SearchSendModel searchSendModel)
//public void sendPost( String source, String destination)
Call call = mAPIService.sendSearch(searchSendModel);
//Call call = mAPIService.sendSearch(source, destination);
call.enqueue(new Callback<SearchModel>()
@Override
public void onResponse(Call<SearchModel> call, Response<SearchModel> response)
Toast.makeText(Search.this, " Responce " +response.body(), Toast.LENGTH_SHORT).show();
Log.v("Responce", "Responce "+response.body());
if (response.isSuccessful())
SearchModel searchResponse = response.body();
assert searchResponse != null;
String content = "";
content += "Code: " +response.code() +"\n";
content += "vech_id: " + searchResponse.getVechId() +"\n";
content += "model_no: " + searchResponse.getModelNo() +"\n";
content += "reg_no: " +searchResponse.getRegNo() +"\n";
content += "delivery_start_date: " +searchResponse.getDeliveryStartDate() +"\n";
content += "delivery_end_date: " +searchResponse.getDeliveryEndDate() +"\n";
content += "full_percent: " +searchResponse.getFullPercent() +"\n";
content += "approval_status: " +searchResponse.getApprovalStatus() +"\n";
content +="source: " +searchResponse.getSource() +"\n";
content += "destination: " +searchResponse.getDestination() +"\n\n";
Toast.makeText(getApplicationContext(),content,Toast.LENGTH_LONG).show();
searchModelList.add(searchResponse);
else
Toast.makeText(getApplicationContext(),"Something is error",Toast.LENGTH_SHORT).show();
这是我期望的Json回应:
[
"vech_id": "30",
"model_no": "8956",
"reg_no": "98765",
"delivery_start_date": "2020-03-18",
"delivery_end_date": "2020-06-24",
"full_percent": "0.14",
"approval_status": "1",
"source": "ranchi",
"destination": "garwah"
]
API的接口是:
@POST("search_vehicle.php")
@FormUrlEncoded
Call<SearchSendModel> sendSearch(@Body SearchSendModel searchSendModel);
SearchSendModel.java
public class SearchSendModel
public SearchSendModel(String source, String destination)
this.source = source;
this.destination = destination;
@SerializedName("source")
private String source;
@SerializedName("destination")
private String destination;
public String getSource()
return source;
public String getDestination()
return destination;
但是我总是会收到此错误:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第2行第2列2路径处为BEGIN_ARRAY $
答案
我认为您的onResponse应该使用List<SearchModel>
而不是SearchModel
。您的响应格式为数组。
以上是关于我想解析Gson信息但出现错误的主要内容,如果未能解决你的问题,请参考以下文章