【发布时间】:2012-11-15 18:05:25
【问题描述】:
我发现了很多请求 REST API 的示例,但所有示例都令人困惑,谁能解释一下使用 http 请求的方法。
我的要求是,我想通过提供用户名、密码和密钥从 REST API 获取数据。
我用的是,
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("REST API url");
post.setHeader("Content-type", "application/json");
JSONObject obj = new JSONObject();
obj.put("username", "un");
obj.put("pwd", "password");
obj.put("key","123456");
post.setEntity(new StringEntity(obj.toString(), "UTF-8"));
HttpResponse response = client.execute(post);
但是响应始终为空,并且在使用浏览器工具通过发布相同的数据进行测试时,这些工作正常。我的方法有问题吗?请建议我正确的方法。谢谢你
【问题讨论】:
标签: java android apache-httpclient-4.x