改造体返回null

时间:2018-03-09 08:52:07

标签: android gson retrofit retrofit2

我发布了一个请求并在postman上测试它,响应在postman和android logcat上正确返回,但是在改造时它在body中返回null

这是响应模型

public class DriverResponse implements Serializable{
@SerializedName("errors")
@Expose
public Errors errors;
@SerializedName("driversapi")
@Expose
public Driversapi driversapi;
}


public class Errors implements Serializable {

@SerializedName("driversapi")
@Expose
public List<String> driversapi = null;
}

public class Driversapi implements Serializable{
public String driverid ;
public String status ;
public String authtoken ;
}

这是获取数据的代码

public void acceptOrder(String auth, AcceptOrderModel acceptOrderModel) {
    ApiInterface service = ApiClient.getClient().create(ApiInterface.class);
    Call<DriverResponse> connection = service.acceptOrder(auth, acceptOrderModel);
    if (connection != null) {
        connection.enqueue(new Callback<DriverResponse>() {
            @Override
            public void onResponse(Call<DriverResponse> call, Response<DriverResponse> response) {
                String reponseStr = response.toString().replaceAll("\\\\", "");
                Log.e("new String", reponseStr);
                apiResponse.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<DriverResponse> call, Throwable t) {

                Log.e("QR ", " " + t.getLocalizedMessage());
                apiResponse.onFailed(t.getLocalizedMessage());
            }
        });
    }
}


@Headers({"Content-Type: application/json"})
@POST("/api/drivers/driveracceptorder")
Call<DriverResponse> acceptOrder(@Header("Authorization") String Authorization, @Body AcceptOrderModel acceptOrderModel);

当我发送请求时,响应得到空响应,错误主体内容得到这个

[size = 99 text = {\ r \ n&#34;错误&#34;:{\ r \ n&#34; driversapi&#34;:[\ r \ n&#34;命令alredy assig ... ]

这里是应该返回并且已经在logcat和postman中返回的预期json

{
"errors": {
  "driversapi": [
    "driver not found"
   ]
  }
}

编辑

这是我的LogCat

 POST http://site.example.com/api/drivers/driveracceptorder http/1.1
03-09 10:57:17.533 8128-17940/com.trend.driver D/OkHttp: Content-Type: application/json
03-09 10:57:17.533 8128-17940/com.trend.driver D/OkHttp: Content-Length: 47
03-09 10:57:17.533 8128-17940/com.trend.driver D/OkHttp: Authorization: Ber z3rlF3cUYICZUm49HeCkpOU8TEjITCqq5tplXDZU4pxvRlzTScUo-0wZr52trM9kWinOnqzaJBwgMB9pp0tk4GTx7BGDoX-u5H9IfQWHlJ_iYpWOIkTiZOb4JuovPPPTfta4MJlRJO6IjasBKpNTvT7KRqp4gHrw3xs9JWa9RTCuY83JhaBf2weyQ8RwsT_SVTg
03-09 10:57:17.535 8128-17940/com.trend.driver D/OkHttp: {"accept":"1","driverid":"2112","orderid":"46"}
03-09 10:57:17.535 8128-17940/com.xxxx.driver D/OkHttp: --> END POST (47-byte body)
03-09 10:57:17.535 8128-8128/com.trend.driver I/Choreographer: Skipped 115 frames!  The application may be doing too much work on its main thread.
03-09 10:57:18.290 8128-17940/com.trend.driver D/OkHttp: <-- 404 Not Found http://site.example.com/api/drivers/driveracceptorder (754ms)
03-09 10:57:18.291 8128-17940/com.trend.driver D/OkHttp: Cache-Control: private
03-09 10:57:18.291 8128-17940/com.trend.driver D/OkHttp: Content-Length: 99
03-09 10:57:18.291 8128-17940/com.trend.driver D/OkHttp: Content-Type: application/json
03-09 10:57:18.291 8128-17940/com.trend.driver D/OkHttp: Server: Microsoft-IIS/8.5
03-09 10:57:18.291 8128-17940/com.trend.driver D/OkHttp: Set-Cookie: Nop.customer=9df71aba-df96-4aa4-a8f5-daab40d91d5e; expires=Sat, 09-Mar-2019     08:57:18 GMT; path=/; HttpOnly
03-09 10:57:18.291 8128-17940/com.trend.driver D/OkHttp: Date: Fri, 09 Mar 2018 08:57:18 GMT
03-09 10:57:18.294 8128-17940/com.trend.driver D/OkHttp: {
03-09 10:57:18.295 8128-17940/com.trend.driver D/OkHttp:   "errors": {
03-09 10:57:18.295 8128-17940/com.trend.driver D/OkHttp:     "driversapi": [
03-09 10:57:18.295 8128-17940/com.trend.driver D/OkHttp:       "order alredy assigned to other driver"
03-09 10:57:18.295 8128-17940/com.trend.driver D/OkHttp:     ]
03-09 10:57:18.295 8128-17940/com.trend.driver D/OkHttp:   }
03-09 10:57:18.295 8128-17940/com.trend.driver D/OkHttp: }
03-09 10:57:18.295 8128-17940/com.trend.driver D/OkHttp: <-- END HTTP (99-byte body)

2 个答案:

答案 0 :(得分:1)

在OnResponse回调中,您应该检查响应是否成功。如果响应不成功,则响应正文将为null,但errorBody将具有内容。这是一个例子:

Call<ArrayList<Town>> callGetTowns = apiCalls.getTownsInState(stateId);
callGetTowns.enqueue(new Callback<ArrayList<Town>>() {

    @Override
    public void onResponse(Call<ArrayList<Town>> call, Response<ArrayList<Town>> response) {
    if (response.isSuccessful()){
        //response is successful so response.body() won't be null
        ArrayList<Town> towns = response.body();

    }else{
        //response is not successful so response.body is null but response.errorBody is not null
        String message = "Server Error!";
        try {
            String errorBody = response.errorBody().string();

            //parse the errorBody string to json and retrieve whichever attribute you want
            JsonObject jsonObject = gson.fromJson(errorBody, JsonObject.class);
            if (jsonObject!=null && jsonObject.has("message") && !jsonObject.get("message").isJsonNull()) {
                message =  jsonObject.get("message").getAsString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
       }
   }

   @Override
   public void onFailure(Call<ArrayList<Town>> call, Throwable t) {
        //request timedout
   }});

答案 1 :(得分:0)

您应该返回DriverResponse而不是ResponseTest,请参阅下面的代码

 public void acceptOrder(String auth, AcceptOrderModel acceptOrderModel) {
    ApiInterface service = ApiClient.getClient().create(ApiInterface.class);
    Call<DriverResponse> connection = service.acceptOrder(auth, acceptOrderModel);
    if (connection != null) {
        connection.enqueue(new Callback<DriverResponse>() {
            @Override
            public void onResponse(Call<DriverResponse> call, Response<DriverResponse> response) {
                List<String>=response.getErrors().getDriversapi();
                apiResponse.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<DriverResponse> call, Throwable t) {

                Log.e("QR ", " " + t.getLocalizedMessage());
                apiResponse.onFailed(t.getLocalizedMessage());
            }
        });
    }
}


@Headers({"Content-Type: application/json"})
@POST("/api/drivers/driveracceptorder")
Call<DriverResponse> acceptOrder(@Header("Authorization") String Authorization, @Body AcceptOrderModel acceptOrderModel);

使用getter更新模型类,如下所示

     public class DriverResponse {

    @SerializedName("errors")
@Expose
private Errors errors;

public Errors getErrors() {
return errors;
}

public void setErrors(Errors errors) {
this.errors = errors;
}

    }

    public class Errors {

@SerializedName("driversapi")
@Expose
private List<String> driversapi = null;

public List<String> getDriversapi() {
return driversapi;
}

public void setDriversapi(List<String> driversapi) {
this.driversapi = driversapi;
}

}

现在在onResponse内,您可以获得一个字符串数组

List<String>=response.getErrors().getDriversapi();