无法让 HttpParams 与 Postrequest 一起使用
我无法从 Android-API 中获取 HttpParams-stuff。
我只是不想通过我的 Postrequest 发送一些简单的参数。除了参数之外,一切都工作正常。将参数设置为 postrequest 的代码:
HttpParams params = new BasicHttpParams();
params.setParameter("password", "secret");
params.setParameter("name", "testuser");
postRequest.setParams(params);
似乎此代码根本没有添加任何参数,因为服务器总是回答我的请求缺少“名称”参数。
实际按预期工作的示例:
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("name", "testuser"));
postParameters.add(new BasicNameValuePair("password", "secret"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
postRequest.setEntity(formEntity);
但我想使用第一个示例的版本,因为它更容易阅读和理解。
任何提示真的很感激!
I can't get the HttpParams-stuff from the Android-API working.
I just wan't to send some simple Parameters with my Postrequest. Everything is working fine, except for the parameters. The code to set the parameters to the postrequest:
HttpParams params = new BasicHttpParams();
params.setParameter("password", "secret");
params.setParameter("name", "testuser");
postRequest.setParams(params);
It seems that this code isn't adding any parameter at all, as the server always answer, that my request is missing the "name"-parameter.
An example of what is actually working as expected:
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("name", "testuser"));
postParameters.add(new BasicNameValuePair("password", "secret"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
postRequest.setEntity(formEntity);
But I would like to use a version of the first example, as it is easier to read and understand.
Any hint is really appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一旦我遇到了同样的问题,我就按照与你相同的方式解决了它......我记得我发现了一些关于为什么它不起作用的主题。这是关于 Apache 在服务器端的库实现的一些事情。
不幸的是,我现在找不到这个主题,但如果我是你,我会让它继续工作,不会太担心代码的“优雅”,因为你可能无能为力,如果你可以,这根本不实用。
Once I had the same issue, and I solved it the same way as you did... I remember I found some topic about why that wasn't working. It was something about Apache's library implementation on the server side.
Unfortunately I can't find that topic now, but if I were you I would just leave it working and wouldn't worry so much about the "elegance" of the code, cause probably there isn't much you can do, and if you can, it's not practical at all.
尝试以第一种方式使其工作,但似乎
HttpParams
接口并不是为此构建的。在谷歌搜索了一段时间后,我发现这个答案解释了它:不过,该文档并不那么具体:
为了设置连接和请求超时,我混合使用了
HttpParams
和List
,它功能齐全并使用AndroidHttpClient
类,可从 API 8 获取:另请参阅:
Tried to get it work the first way, but it seems
HttpParams
interface isn't intended to be built for that. Having Googled for a while, I found this SO answer explaining it:The documentation isn't so specific, though:
For setting connection and request timeouts, I've used a mix of both
HttpParams
andList<NameValuePair>
, which is fully functional and uses theAndroidHttpClient
class, available from API 8:See also: