我需要爲用戶登錄創建發佈請求。我設置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