POST改造2.0

时间:2015-12-16 17:11:37

标签: java android retrofit

我正在使用改造来与我的宁静网络服务进行通信。 GET请求工作正常,问题出在我尝试发出POST请求时。

在我的网络服务中,我有这种方法:

@POST
    @Path("/byPeriodo")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public List<ItemProdutividade> getProdutividadeByPeriodo(@FormParam("dataInicial")
    Date dataInicial, @FormParam("dataFinal") Date dataFinal,
    @FormParam("cpf") long cpf){
        return service.getProdutividadeByPeriodo(DateUtils.toLocalDate(dataInicial),
                DateUtils.toLocalDate(dataFinal), cpf);

    }

在Android方面我有这个:

@FormUrlEncoded
@POST("produtividade/byPeriodo/")
Call<List<ItemProdutividade>> getProdutividadeByPeriodo(@Field("dataInicial") Date dataInicial,
                                                        @Field("dataFinal") Date dataFinal,
                                                        @Field("cpf") long cpf);

当我执行时,我收到以下错误:

  

HTTP STATUS 400 - BAD REQUEST - 客户端发送的请求是   语法不正确。

有人知道我该做什么吗?

编辑:

使用改装日志,这是输出:

12-16 20:06:33.521 7333-8993/br.com.empresa.oprojeto D/OkHttp: --> POST /oprojeto.webservice/rest/produtividade/byPeriodo/ HTTP/1.1
12-16 20:06:33.521 7333-8993/br.com.empresa.oprojeto D/OkHttp: dataInicial=Tue%20Dec%2016%2020%3A06%3A33%20BRST%202014&dataFinal=Wed%20Dec%2016%2020%3A06%3A33%20BRST%202015&cpf=12345678987
12-16 20:06:33.521 7333-8993/br.com.empresa.oprojeto D/OkHttp: --> END POST (125-byte body)
12-16 20:06:33.581 7333-8993/br.com.empresa.oprojeto D/OkHttp: <-- HTTP/1.1 400 Bad Request (64ms)
12-16 20:06:33.581 7333-8993/br.com.empresa.oprojeto D/OkHttp: Server: Apache-Coyote/1.1
12-16 20:06:33.581 7333-8993/br.com.empresa.oprojeto D/OkHttp: Content-Type: text/html;charset=utf-8
12-16 20:06:33.581 7333-8993/br.com.empresa.oprojeto D/OkHttp: Content-Language: en
12-16 20:06:33.581 7333-8993/br.com.empresa.oprojeto D/OkHttp: Content-Length: 1033
12-16 20:06:33.581 7333-8993/br.com.empresa.oprojeto D/OkHttp: Date: Wed, 16 Dec 2015 22:06:31 GMT
12-16 20:06:33.581 7333-8993/br.com.empresa.oprojeto D/OkHttp: Connection: close
12-16 20:06:33.581 7333-8993/br.com.empresa.oprojeto D/OkHttp: OkHttp-Selected-Protocol: http/1.1
12-16 20:06:33.581 7333-8993/br.com.empresa.oprojeto D/OkHttp: OkHttp-Sent-Millis: 1450303593575
12-16 20:06:33.581 7333-8993/br.com.empresa.oprojeto D/OkHttp: OkHttp-Received-Millis: 1450303593591
12-16 20:06:33.581 7333-8993/br.com.empresa.oprojeto D/OkHttp: <!DOCTYPE html><html><head><title>Apache Tomcat/8.0.29 - Error report</title><style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style> </head><body><h1>HTTP Status 400 - Bad Request</h1><div class="line"></div><p><b>type</b> Status report</p><p><b>message</b> <u>Bad Request</u></p><p><b>description</b> <u>The request sent by the client was syntactically incorrect.</u></p><hr class="line"><h3>Apache Tomcat/8.0.29</h3></body></html>
12-16 20:06:33.581 7333-8993/br.com.empresa.oprojeto D/OkHttp: <-- END HTTP (1033-byte body)

1 个答案:

答案 0 :(得分:0)

我终于解决了这个问题。我发送类型java.util.Date作为参数,我决定更改为类型Long(或长)。所以,我的网络服务方法现在是:

@POST
    @Path("/byPeriodo")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public List<ItemProdutividade> getProdutividadeByPeriodo(@FormParam("dataInicial")
    long dataInicial, @FormParam("dataFinal") long dataFinal,
    @FormParam("cpf") long cpf){
        return service.getProdutividadeByPeriodo(DateUtils.toLocalDate(new Date(dataInicial)),
                DateUtils.toLocalDate(new Date(dataFinal)), cpf);
    }

秘诀是将类型日期更改为长并执行此操作:new Date(long)