2013-05-14 61 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