我是android编程新手。我有一个类,在这个类中,修改API调用是为了解析和显示JSON文件中的几个属性。但我得到:
Java语言lang.IllegalStateException:应为BEGIN\u对象,但在第2行第1列为字符串
好心帮忙。我搜索了答案,但没有发现JSON响应有任何问题。发布了我的JSON响应和JAVA类:
JSON响应:
{
"status": 1,
"active_requests": [
{
"id": "12",
"driver_id": "2",
"booking_id": "12",
"request_status": "1",
"created_on": "2018-09-13 19:57:29",
"updated_on": "2018-09-13 19:57:29",
"customer_id": "1",
"pickup_location": "GN Mills",
"drop_location": "Vadavalli",
"pickup_latitude": "11.025625",
"pickup_longitude": "76.955467",
"drop_latitude": "18.5645654",
"drop_longitude": "17.5645654",
"pickup_date_time": "2018-09-28 15:25:00",
"drop_date_time": "2018-09-28 15:25:00",
"package_weight": "55.5",
"package_type": "1",
"package_description": "fdasd dsaD YDASYD",
"vechicle_type": "car",
"service_status": "1",
"total_distance": "0",
"service_fare": "3460.6110936131",
"driver_fare": "2768.4888748905",
"paid_amount": "0",
"card_id": "0",
"picked_time": "0000-00-00 00:00:00",
"dropped_time": "0000-00-00 00:00:00"
}
]
}
JAVA文件:
public class DriverDashboardFragment extends Fragment {
View root_view;
@BindView(R.id.linear_detail)
LinearLayout linearDetail;
@BindView(R.id.img_back)
ImageView imgBack;
@BindView(R.id.txt_title)
TextView txt_title;
SharedPreferences sharedPreferences;
Unbinder unbinder;
Context context;
RecyclerView recyclerView;
DriverDashboardAdapter driverDashboardAdapter;
List<DriverRequestModel> driver_list;
String id;
public static DriverDashboardFragment newInstance() {
DriverDashboardFragment fragment = new DriverDashboardFragment();
return fragment;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
root_view = inflater.inflate(R.layout.driverfragment_dashboard, container, false);
unbinder = ButterKnife.bind(this, root_view);
context = getContext();
sharedPreferences = getActivity().getSharedPreferences("MyPref", 0); // 0 - for private mode
id = PrefConnect.readString(getActivity(), PrefConnect.CUSTOMER_ID, "");
String msg= sharedPreferences.getString("message","");
Log.i("TAG",msg);
txt_title.setText("Dashboard");
imgBack.setVisibility(View.GONE);
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
ApiService apiService = (ApiService) RetrofitSingleton.getApiService();
Call<DriverRequestModel> call = apiService.getdriverrequest();
call.enqueue(new Callback<DriverRequestModel>() {
@Override
public void onResponse(Call<DriverRequestModel> call, Response<DriverRequestModel> response) {
driver_list = new ArrayList<>();
DriverRequestModel promo=response.body();
driver_list = promo.getActiveRequests();
Log.e("Response = ",new Gson().toJson(response.body()));
PrefConnect.writeString(context,PrefConnect.CUSTOMER_ID,response.body().getCustomerId()+"");
recyclerView = (RecyclerView)root_view.findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
driverDashboardAdapter = new DriverDashboardAdapter(getContext(),driver_list);
recyclerView.setAdapter(driverDashboardAdapter);
}
@Override
public void onFailure(Call<DriverRequestModel> call, Throwable t) {
Log.e("TAG","Response = "+t.toString());
}
});
return root_view;
}
@OnClick({R.id.linear_detail})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.linear_detail:
Intent detail = new Intent(context, DriverServiceDetailActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("key_1", 1);
detail.putExtras(bundle);
startActivity(detail);
break;
}
}
}
模型类:
public class DriverRequestModel {
@SerializedName("status")
@Expose
public Integer status;
@SerializedName("active_requests")
@Expose
public List<ActiveRequest> activeRequests = null;
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public List<ActiveRequest> getActiveRequests() {
return activeRequests;
}
public void setActiveRequests(List<ActiveRequest> activeRequests) {
this.activeRequests = activeRequests;
}
public class ActiveRequest {
@SerializedName("id")
@Expose
public String id;
@SerializedName("driver_id")
@Expose
public String driverId;
@SerializedName("booking_id")
@Expose
public String bookingId;
@SerializedName("request_status")
@Expose
public String requestStatus;
@SerializedName("created_on")
@Expose
public String createdOn;
@SerializedName("updated_on")
@Expose
public String updatedOn;
@SerializedName("customer_id")
@Expose
public String customerId;
@SerializedName("pickup_location")
@Expose
public String pickupLocation;
@SerializedName("drop_location")
@Expose
public String dropLocation;
@SerializedName("pickup_latitude")
@Expose
public String pickupLatitude;
@SerializedName("pickup_longitude")
@Expose
public String pickupLongitude;
@SerializedName("drop_latitude")
@Expose
public String dropLatitude;
@SerializedName("drop_longitude")
@Expose
public String dropLongitude;
@SerializedName("pickup_date_time")
@Expose
public String pickupDateTime;
@SerializedName("drop_date_time")
@Expose
public String dropDateTime;
@SerializedName("package_weight")
@Expose
public String packageWeight;
@SerializedName("package_type")
@Expose
public String packageType;
@SerializedName("package_description")
@Expose
public String packageDescription;
@SerializedName("vechicle_type")
@Expose
public String vechicleType;
@SerializedName("service_status")
@Expose
public String serviceStatus;
@SerializedName("total_distance")
@Expose
public String totalDistance;
@SerializedName("service_fare")
@Expose
public String serviceFare;
@SerializedName("driver_fare")
@Expose
public String driverFare;
@SerializedName("paid_amount")
@Expose
public String paidAmount;
@SerializedName("card_id")
@Expose
public String cardId;
@SerializedName("picked_time")
@Expose
public String pickedTime;
@SerializedName("dropped_time")
@Expose
public String droppedTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDriverId() {
return driverId;
}
public void setDriverId(String driverId) {
this.driverId = driverId;
}
public String getBookingId() {
return bookingId;
}
public void setBookingId(String bookingId) {
this.bookingId = bookingId;
}
public String getRequestStatus() {
return requestStatus;
}
public void setRequestStatus(String requestStatus) {
this.requestStatus = requestStatus;
}
public String getCreatedOn() {
return createdOn;
}
public void setCreatedOn(String createdOn) {
this.createdOn = createdOn;
}
public String getUpdatedOn() {
return updatedOn;
}
public void setUpdatedOn(String updatedOn) {
this.updatedOn = updatedOn;
}
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public String getPickupLocation() {
return pickupLocation;
}
public void setPickupLocation(String pickupLocation) {
this.pickupLocation = pickupLocation;
}
public String getDropLocation() {
return dropLocation;
}
public void setDropLocation(String dropLocation) {
this.dropLocation = dropLocation;
}
public String getPickupLatitude() {
return pickupLatitude;
}
public void setPickupLatitude(String pickupLatitude) {
this.pickupLatitude = pickupLatitude;
}
public String getPickupLongitude() {
return pickupLongitude;
}
public void setPickupLongitude(String pickupLongitude) {
this.pickupLongitude = pickupLongitude;
}
public String getDropLatitude() {
return dropLatitude;
}
public void setDropLatitude(String dropLatitude) {
this.dropLatitude = dropLatitude;
}
public String getDropLongitude() {
return dropLongitude;
}
public void setDropLongitude(String dropLongitude) {
this.dropLongitude = dropLongitude;
}
public String getPickupDateTime() {
return pickupDateTime;
}
public void setPickupDateTime(String pickupDateTime) {
this.pickupDateTime = pickupDateTime;
}
public String getDropDateTime() {
return dropDateTime;
}
public void setDropDateTime(String dropDateTime) {
this.dropDateTime = dropDateTime;
}
public String getPackageWeight() {
return packageWeight;
}
public void setPackageWeight(String packageWeight) {
this.packageWeight = packageWeight;
}
public String getPackageType() {
return packageType;
}
public void setPackageType(String packageType) {
this.packageType = packageType;
}
public String getPackageDescription() {
return packageDescription;
}
public void setPackageDescription(String packageDescription) {
this.packageDescription = packageDescription;
}
public String getVechicleType() {
return vechicleType;
}
public void setVechicleType(String vechicleType) {
this.vechicleType = vechicleType;
}
public String getServiceStatus() {
return serviceStatus;
}
public void setServiceStatus(String serviceStatus) {
this.serviceStatus = serviceStatus;
}
public String getTotalDistance() {
return totalDistance;
}
public void setTotalDistance(String totalDistance) {
this.totalDistance = totalDistance;
}
public String getServiceFare() {
return serviceFare;
}
public void setServiceFare(String serviceFare) {
this.serviceFare = serviceFare;
}
public String getDriverFare() {
return driverFare;
}
public void setDriverFare(String driverFare) {
this.driverFare = driverFare;
}
public String getPaidAmount() {
return paidAmount;
}
public void setPaidAmount(String paidAmount) {
this.paidAmount = paidAmount;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getPickedTime() {
return pickedTime;
}
public void setPickedTime(String pickedTime) {
this.pickedTime = pickedTime;
}
public String getDroppedTime() {
return droppedTime;
}
public void setDroppedTime(String droppedTime) {
this.droppedTime = droppedTime;
}
}
}
您是否使用了正确的模型来映射您的响应?应该是:
DriverRequestModel driverRequestModel = response.body();
问题内容: 我有这种方法: 我想解析一个JSON: 但是我收到一条错误消息: com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但位于第1行第1列 问题答案: 即使没有看到您的JSON字符串,您也可以从错误消息中得知,它不是要解析为类实例的正确结构。 Gson希望您的JSON字符串
问题内容: 我遇到错误了。 由于以下原因,无法解析JSON:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第1行第2列为BEGIN_OBJECT 服务器网址 执行请求 上课后 我该如何解决? 问题答案: 您在注释中声明返回的JSON是这样的: 您告诉Gson您有一个对象数组:
在尝试将json文本文件解析为餐厅对象的数组列表时,我遇到了错误“应为BEGIN\u数组,但在第1行第2列路径处为BEGIN\u对象。”我不知道哪里弄错了,因为我的Restaurant类中的数据成员与json文件中的字段完全对应。 主要的 餐厅类 txt文件
我正在尝试运行此应用程序,但当我尝试登录时,我在“logcat”中看到此错误。消息“Success Login”表示它已被记录,并且有到数据库的连接,但我不知道在哪里等待对象,在哪里接收字符串。 10-11 14:54:10.481 2532-7430/com.alvardev.demos.limaculturalD/com.alvardev.demos.limacultural.http.Res
我正在尝试通过改造将字符串类型列表发送到mysql数据库。当我尝试使用postman时,我获得了成功,但我收到了来自android studio的错误消息。 我的API接口: 我的响应数据类: Php响应: Android Studio错误消息: OkHttp日志: 我认为问题出在我发布的列表中,因为如果我删除列表,结果是成功的。 怎么了?
问题内容: 我从Web服务获取JSon数据,示例数据如下: 当我尝试将其转换时,它将引发错误,我这样做是: 我的班级是: LogCat说: com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第2列为BEGIN_ARRAY 问题答案: 错误说明发生了什么问题…您返回的是数组