Maa*_*aaz 10 android http-post
我需要使用参数创建HTTP POST请求.我知道有很多例子,我尝试过使用HTTPparams,NameValuePair等,但似乎无法获得服务器的正确格式.
Server Type: REST based API utilizing JSON for data transfer
Content-type: application/json
Accept: application/json
Content-length: 47
{"username":"abcd","password":"1234"}
我可以通过这些标题但我似乎无法通过这些参数"用户名","密码".这是我的代码:
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);
Run Code Online (Sandbox Code Playgroud)
我试图调试,但无法看到实体是否正确连接...我做错了什么?
提前致谢.玛斯
Fem*_*emi 15
试试这个:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.mymi5.net/API/auth/login");
post.setHeader("Content-type", "application/json");
post.setHeader("Accept", "application/json");
JSONObject obj = new JSONObject();
obj.put("username", "abcd");
obj.put("password", "1234");
post.setEntity(new StringEntity(obj.toString(), "UTF-8"));
HttpResponse response = client.execute(post);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30181 次 |
| 最近记录: |