2013-05-14 95 views
0

我需要爲用戶登錄創建發佈請求。我設置Content-Length頭,但是當我POST請求它給org.apache.http.client.HttpResponseException:必要的長度android - http post content-length(json post)

我的代碼

 DefaultHttpClient httpclient = new DefaultHttpClient(); 

     JSONObject data = new JSONObject(); 
     data.put("UserName", username); 
     data.put("Password", password); 
     data.put("RememberMe", false); 

     HttpPost httpost = new HttpPost(LOGIN_PATH); 
     StringEntity se = new StringEntity(data.toString()); 
     httpost.setEntity(se); 

     httpost.setHeader("Accept", "application/json"); 
     httpost.setHeader("Content-Type", "application/json"); 
     httpost.setHeader("Content-Length",""+se.getContentLength()); 

     ResponseHandler responseHandler = new BasicResponseHandler(); 
     return httpclient.execute(httpost, responseHandler); 

當我設置Content-Length頭它給'內容長度標題已經存在',並且當刪除內容長度標題時它給出'長度是必需的'

謝謝。

+0

與HttpURLConnection的inntead DefaultHttpClient和HttpPost解決。謝謝 – dracula 2013-05-14 11:20:46

回答

2

變化

httpost.setHeader("Content-Lenght",""+se.getContentLength()); 

httpost.setHeader("Content-Length",""+se.getContentLength()); 

,因爲目前你逝去的Content-Lenght(長度拼寫錯誤),而不是Content-Length作爲關鍵setHeader方法

+0

對不起。我錯過了。當我設置Content-Length頭時,它給出'Content-Length頭已經存在',並且當刪除content-length頭時,它給出'Length is required' – dracula 2013-05-14 09:09:08

+0

@dracula,你是否能夠解決'Content-Length header already present'問題?我也一樣。在這裏幫助我。 – Gopinath 2013-07-31 16:47:16

+0

@Gopinath,是的我已經解決了HttpURLConnection並設置RequestProperties的連接 – dracula 2013-08-01 06:26:11