我需要为用户登录创建发布请求。我设置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头它给'内容长度标题已经存在',并且当删除内容长度标题时它给出'长度是必需的'
谢谢。
与HttpURLConnection的inntead DefaultHttpClient和HttpPost解决。谢谢 – dracula 2013-05-14 11:20:46