冒着11次问这个问题的风险,我遇到问题处理来自POST请求的响应。并且无法找到stackoverflow的任何示例。
我向我的后端发出一个简单的请求,它返回一个 json对象数组。
我得到的错误是。点。
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
这正是我正在做的事情
WebAPIService.class
@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"
}
}
]}
答案 0 :(得分:1)
如果真的是服务器的响应,那么你需要创建一个如下所示的类:
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();
}
});
答案 1 :(得分:-1)
您可以在VehicleTransferData类中发布代码吗?
也许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();
}
});