java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第2行第1列错误处为STRING

时间:2018-09-21 17:40:03

标签: android json gson retrofit

我是android编程的新手。我有一个用于完成 retrofit API 调用的类,以解析和显示一些属性JSON文件。但是我得到了:

  

java.lang.IllegalStateException:预期为BEGIN_OBJECT,但为STRING   在第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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您使用正确的模型来映射您的响应吗?应该是

DriverRequestModel driverRequestModel = response.body();