【问题标题】:Retrofit return null in body() method in Android在 Android 的 body() 方法中改造返回 null
【发布时间】:2016-08-30 04:09:33
【问题描述】:

我在这些代码行中找不到语义错误的位置:

PuntoGpsRecorridoDTO puntoGpsRecorridoDto = new PuntoGpsRecorridoDTO();
puntoGpsRecorridoDto.setDescripcion(puntoGpsRecorrido.getDescripcion());
puntoGpsRecorridoDto.setIdRecorrido(37);            
puntoGpsRecorridoDto.setDemoraSeg(2);
puntoGpsRecorridoDto.setPrecisionMts(10); 
puntoGpsRecorridoDto.setEstado(puntoGpsRecorrido.getEstado());
puntoGpsRecorridoDto.setFechaHora(puntoGpsRecorrido.getFechaHora());
puntoGpsRecorridoDto.setIdDispositivo("943953977-OFICINA");
puntoGpsRecorridoDto.setLatitud(puntoGpsRecorrido.getLatitud());
puntoGpsRecorridoDto.setLongitud(puntoGpsRecorrido.getLongitud());
puntoGpsRecorridoDto.setPrecisionMts(puntoGpsRecorrido.getPrecisionMts());
Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDateDeserializer()).create();
Retrofit retrofit = new Retrofit.Builder().baseUrl(Util.URL_WS).addConverterFactory(GsonConverterFactory.create(gson)).build();
LocationService locationService = retrofit.create(LocationService.class);
Call<EstadoDTO> callEstadoDto = locationService.enviarPuntoGpsRecorrido(puntoGpsRecorridoDto);
Response<EstadoDTO> exec = callEstadoDto.execute();
estadoDto = exec.body(); // <<<------ body() return NULL  

改造客户端的LocationService接口:

public interface LocationService
{
    @POST("recorrido/sending")
    Call<EstadoDTO> enviarPuntoGpsRecorrido(@Body PuntoGpsRecorridoDTO puntoGpsRecorridoDto);
}

连接到服务? ---> 是的,

它以另一种方式工作? --> 是的,通过 SoapUI 测试,

服务器? ---> Apache tomcat + Mysql + Hibernate

PuntoGpsRecorridoDTO 类:

public class PuntoGpsRecorridoDTO 
{
    private Integer idRecorrido;
    private String idDispositivo;
    private Double latitud;
    private Double longitud;
    private Boolean estado;
    private String descripcion;
    private Integer precisionMts;
    private Integer demoraSeg;
    private Date fechaHora;

    public PuntoGpsRecorridoDTO()
    {
    } 
}

PuntoGpsRecorrido 类:

@DatabaseTable(tableName = "PuntoGpsRecorrido")
public class PuntoGpsRecorrido
{
    @DatabaseField(generatedId = true)
    private Integer idRecorrido;

    @DatabaseField(foreign = true, canBeNull = false)
    private Dispositivo dispositivo;
    @DatabaseField
    private Double latitud;
    @DatabaseField
    private Double longitud;
    @DatabaseField
    private Boolean estado;
    @DatabaseField
    private String descripcion;
    @DatabaseField
    private Integer precisionMts;
    @DatabaseField
    private Integer demoraSeg;
    @DatabaseField
    private Date fechaHora;

    public PuntoGpsRecorrido()
    {}
}

EstadoDTO 类:

public class EstadoDTO
{
    public static final String EXITO="001";
    public static final String ERROR="000";

    private String code;
    private String msg;
    private String extra;

    public EstadoDTO()
    {}

}

错误:

用 SoapUI 测试,很顺利:

我做错了什么?如果您需要更多信息,请告诉我 信息需求。提前致谢。

【问题讨论】:

  • 响应中的 httpstatus 代码是什么?我想知道它不在 200~300 范围内
  • 感谢 Long Ranger,httpstatus 代码是:500 内部服务器错误。
  • body()函数只有在http状态码在200~300范围内才会返回结果。在这里查看我的答案stackoverflow.com/questions/39097680/…
  • 我明白了,但是如果一切顺利,服务器会返回 500?不管怎样,你知道我在哪里可以看到 Apache Tomcat 中该错误的详细信息吗?
  • 您应该检查 apache 主位置中的日志文件夹。如果有任何错误并且您有用于登录 Web 应用程序的代码,则日志文件应该在其中。 errorBody 是否包含您需要的所有信息?

标签: android tomcat android-studio retrofit retrofit2


【解决方案1】:

使用下面的代码,它将处理来自服务器的各种故障,您可能需要升级到最新版本的 Retrofit。

调用 callEstadoDto = locationService.enviarPuntoGpsRecorrido(puntoGpsRecorridoDto);

            callEstadoDto.enqueue(new Callback<EstadoDTO>() {
                @Override
                public void onResponse(Response<EstadoDTO> response, Retrofit retrofit) {
                    hideLoader();
                    if(response.isSuccess()){

                      //do your thing

                    }else{

                        String error=response.errorBody().string();
                    }
                }

                @Override
                public void onFailure(Throwable t) {


                    Log.e("error " , ""+t.toString());
                }
            });

【讨论】:

    【解决方案2】:

    这就是解决方案!

    1. 检测到错误:属性中的错误序列化日期数据类型 对象的 PuntoGpsRecorridoDTO 发送 POJO Web 服务。

      Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDateDeserializer()).create();

    解决方案:替换为:

    Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDateSerializer()).create();
    

    Aqui las clases:

    JsonDateDeserializer 类:

    public class JsonDateDeserializer implements JsonDeserializer<Date>
    {
        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
        {
            String s = json.getAsJsonPrimitive().getAsString();
            long l = Long.parseLong(s); //long l = Long.parseLong(s.substring(6, s.length() - 2));
            Date d = new Date(l);
            return d;
        }
    }
    

    JsonDateSerializer 类:

    public class JsonDateSerializer implements JsonSerializer<Date>
    {
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context)
        {
            long l = src.getTime();
            JsonElement json = new JsonPrimitive(l);
            return json;
        }
    }
    

    编程愉快...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-10
      • 1970-01-01
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      • 2015-12-24
      相关资源
      最近更新 更多