我需要使用参数创建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