2015-12-16 86 views
3

我正在使用改造與我寧靜的Web服務進行通信。 GET請求工作正常,問題是當我嘗試發出POST請求時。POST與改造2.0

在我的web服務我有這樣的方法:

@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 -The request sent by the client was syntactically incorrect.

有人知道我必須做什麼?

編輯:

使用改裝的日誌,這是輸出:

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) 
+0

你能重新檢查路徑和帖子路徑 – g90

+0

我已經這樣做了。我使用一個基本的URL('http:// IP_ADDRESS:8080/project/rest /'),併爲獲取請求,它工作正常。在帖子路徑中,我已經嘗試過'produtividade/byPeriodo'和'produtividade/byPeriodo /'。在兩個我得到相同的錯誤。 – Luiz

+0

你的任何錯誤android日誌? – g90

回答

0

我終於解決了這個問題。我發送類型java.util.Date作爲參數,我決定改變類型Long(或long)。所以,我的web服務方法現在是:

@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)