【发布时间】:2012-03-21 13:10:24
【问题描述】:
我正在尝试调用一个私有网络服务,其中有一个我必须使用GET 方法访问的链接。在浏览器上使用直接 URL(需要先登录)时,我以 JSON 格式获取数据。我调用的 URL 是这样的
http://www.example.com/trip/details/860720?format=json
url 工作正常,但是当我使用HttpGet 调用它时,我得到的是网页的 HTML 编码,而不是 JSON 字符串。我使用的代码如下:
private String runURL(String src,int id) { //src="http://www.example.com/trip/details/"
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(src);
String responseBody="";
BasicHttpParams params=new BasicHttpParams();
params.setParameter("domain", token); //The access token I am getting after the Login
params.setParameter("format", "json");
params.setParameter("id", id);
try {
httpget.setParams(params);
HttpResponse response = httpclient.execute(httpget);
responseBody = EntityUtils.toString(response.getEntity());
Log.d("runURL", "response " + responseBody); //prints the complete HTML code of the web-page
} catch (Exception e) {
e.printStackTrace();
}
return responseBody;
}
你能告诉我我在这里做错了什么吗??
【问题讨论】:
-
如果您尝试使用浏览器或 curl 访问该 url,内容是什么
-
它是一个 json 字符串,如果我尝试通过浏览器调用它,它也应该在 Android 上。
标签: android web-services http-get