代码之家  ›  专栏  ›  技术社区  ›  Kaigo

使用改装2处理对象阵列响应

  •  0
  • Kaigo  · 技术社区  · 7 年前

    冒着第11次问这个问题的风险,我遇到了一些问题 处理POST请求的响应 。并且无法使stackoverflow的任何示例正常工作。

    我正在向后端发出一个简单的请求,返回 json对象数组

    我得到的错误是。

    Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

    这就是我要做的

    WebAPIService。班

    @Headers("Content-Type: application/json")
    @POST("pendingTransfers")
    Call<List<VehicleTransferData>> refreshVehicles(@Body VehicleTransferData msg);
    

    活动

    public void refreshCars(View view) {
    
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(TRANSFER_API)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    
        WebAPIService client = retrofit.create(WebAPIService.class);
    
        setTestData();
    
        Call<List<VehicleTransferData>> call = client.refreshVehicles(vtData);
    
        call.enqueue(new Callback<List<VehicleTransferData>>()
        {
            @Override
            public void onResponse(Call<List<VehicleTransferData>> call, Response<List<VehicleTransferData>> response) {
    
                Toast.makeText(RegisteredVehicles.this, response.body().get(0).getVehicleReg(),Toast.LENGTH_LONG).show();
            }
    
            @Override
            public void onFailure(Call<List<VehicleTransferData>> call, Throwable t) {
                Toast.makeText(RegisteredVehicles.this, t.getMessage(),Toast.LENGTH_LONG).show();
            }
        });
    
    }
    

    来自服务器的响应数据

    {ResponseCode=200, ResponseMessage=ok, ApiVersion=v1, ResponsePackage=[
      {
        "transferUUID":"TRANSFER-UUID-5678-0000-567812345678",
        "vehicleUUID":"0VEHICLE-UUID-5678-0000-567812345678",
        "vehicleRegUUID":"VEHICLER-UUID-5678-0000-567812345678",
        "vehicleReg":"j107",
        "status":"pending",
        "transferDate":"2099-12-3",
        "seller":{
          "personUUID":"00SELLER-UUID-5678-0000-567812345678",
          "signature":"%3FR%3FD%3FV%3F%344DX54F%3F%3F%3F%3F%3F%3FBOOT-INF%2Flib%2FasmPK%0D%0A",
          "mileage":39993,"timestamp":"2099-12-31T23:59:59"
        },
        "buyer":{
          "personUUID":"000BUYER-UUID-5678-0000-567812345678",
          "signature":"Alfred Schmidt",
          "mileage":3993,
          "timestamp":"time"
        }
      }
    ]}
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   MobileMon    7 年前

    如果这确实是来自服务器的响应,那么您需要创建一个如下所示的类:

    class VehicleResponse{
        int ResponseCode;
        string ResponseMessage;
        string ApiVersion;
        List<VehicleTransferData> ResponsePackage;
    }
    

    API:

    @Headers("Content-Type: application/json")
    @POST("pendingTransfers")
    Call<VehicleResponse> refreshVehicles(@Body VehicleTransferData msg);
    

    活动:

    call.enqueue(new Callback<VehicleResponse>()
        {
            @Override
            public void onResponse(Call<VehicleResponse> call, Response<VehicleResponse> response) {
    
                Toast.makeText(RegisteredVehicles.this, response.body().get(0).getVehicleReg(),Toast.LENGTH_LONG).show();
            }
    
            @Override
            public void onFailure(Call<VehicleResponse> call, Throwable t) {
                Toast.makeText(RegisteredVehicles.this, t.getMessage(),Toast.LENGTH_LONG).show();
            }
        });
    
        2
  •  -1
  •   MobileMon    7 年前

    你能在你的VehicletTransferData类中发布代码吗?

    此外,JSON可能没有返回数组,而是返回一个对象。如果是这种情况,则改为:

     @Headers("Content-Type: application/json")
    @POST("pendingTransfers")
    Call<VehicleTransferData> refreshVehicles(@Body VehicleTransferData msg);
    

    和在活动中

    call.enqueue(new Callback<VehicleTransferData>()
        {
            @Override
            public void onResponse(Call<VehicleTransferData> call, Response<VehicleTransferData> response) {
    
                Toast.makeText(RegisteredVehicles.this, response.body().get(0).getVehicleReg(),Toast.LENGTH_LONG).show();
            }
    
            @Override
            public void onFailure(Call<VehicleTransferData> call, Throwable t) {
                Toast.makeText(RegisteredVehicles.this, t.getMessage(),Toast.LENGTH_LONG).show();
            }
        });