我需要创建一个HTTP POST请求并带上参数。我知道有很多示例,我已经尝试使用HTTPparams,NameValuePair等,但是似乎无法得到正确的格式以便于服务器进行处理。
服务器类型:使用JSON进行数据传输的REST API
内容类型:application/json
接受类型:application/json
内容长度:47
{"username":"abcd","password":"1234"}
我可以传递这些标头,但是我似乎无法传递这些参数“username”和“password”。这里是我的代码:
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
HTTP.UTF_8
。 - Richard Le Mesurier