gpt4 book ai didi

java - 如何从Android应用程序向服务器设置参数?

转载 作者:行者123 更新时间:2023-12-01 13:20:31 25 4
gpt4 key购买 nike

我有以下代码可以从我的 Android 应用程序连接到 zappos api 服务器并搜索一些内容。但它要么返回 error 404 or We are unable to process the request from the input feilds given .

当我执行相同的查询时,它会在网络浏览器上运行。

查询是:

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());

我做错了什么?

最佳答案

如果我是对的,那么您想要做的是带有参数的 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());

关于java - 如何从Android应用程序向服务器设置参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22053689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com