我需要使用參數創建HTTP POST請求。我知道有很多例子,我嘗試使用HTTPparams,NameValuePair等,但似乎無法得到正確的服務器格式。Android:參數不起作用的Http post
Server Type: REST based API utilizing JSON for data transfer
Content-type: application/json
Accept: application/json
Content-length: 47
{"username":"abcd","password":"1234"}
我可以通過這些信息,但是我似乎無法通過這些PARAMS「用戶名」,「密碼」。這裏是我的代碼:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.mymi5.net/API/auth/login");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("username","abcd"));
pairs.add(new BasicNameValuePair("password","1234"));
post.setHeader("Content-type", "application/json");
post.setHeader("Accept", "application/json");
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs,"UTF-8");
post.setEntity(entity);
HttpResponse response = client.execute(post);
我試圖調試,但不能看到,如果實體正確或不附...什麼是我做錯了什麼?
在此先感謝。 瑪斯
我需要的!非常感謝。 :) – Maaz 2011-05-11 02:30:05
完美 - 添加,我喜歡使用定義的常量:'HTTP.UTF_8' – 2013-02-06 13:27:28
爲什麼這個工作和OP的代碼不?我面臨類似的問題,但只有10個服務器中有1個。 – 2014-10-24 04:12:53