我有以下代码来从我的android应用程序连接到zappos api服务器并搜索一些东西。但它要么返回error 404 or We are unable to process the request from the input feilds given。
当我执行相同的查询时,它可以在web浏览器上运行。
查询为:
http://api.zappos.com/Search&term=boots&key=<my_key_inserted_here>
代码:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://api.zappos.com/Search");
NameValuePair keypair = new BasicNameValuePair("key",KEY);
NameValuePair termpair = new BasicNameValuePair("term",data);
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(keypair);
params.add(termpair);
post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(post);
String str;
StringBuilder sb = new StringBuilder();
HttpEntity entity =response.getEntity();
if (entity != null) {
DataInputStream in = new DataInputStream(entity.getContent());
while (( str = in.readLine()) != null){
sb.append(str);
}
in.close();
}
Log.i("serverInterface","response from server is :"+sb.toString());我做错了什么?
发布于 2014-02-27 08:10:45
如果我是正确的,那么您想要做的是一个带有参数的GET请求。
然后,代码将如下所示:
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://api.zappos.com/Search");
HttpParams params = new BasicHttpParams();
params.setParameter("key", "KEY");
params.setParameter("term", "data");
get.setParams(params);
HttpResponse response;
response = client.execute(get);
String str;
StringBuilder sb = new StringBuilder();
HttpEntity entity = response.getEntity();
if (entity != null) {
DataInputStream in;
in = new DataInputStream(entity.getContent());
while ((str = in.readLine()) != null) {
sb.append(str);
}
in.close();
}
Log.i("serverInterface", "response from server is :" + sb.toString());发布于 2014-02-27 09:12:33
在你的帮助下,我找到了这个问题的答案。我得到的提示是,我必须搜索如何连接到REST服务,并且我还使用了this结果。这正是我想要的结果。可悲的是,它与我试图实现的目标太相似了,我认为无论是谁问它,都可能适用于相同的职位:
https://stackoverflow.com/questions/22053689
复制相似问题