无法让 HttpParams 与 Postrequest 一起使用

发布于 2024-10-07 06:27:53 字数 782 浏览 4 评论 0原文

我无法从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

記憶穿過時間隧道 2024-10-14 06:27:53

一旦我遇到了同样的问题,我就按照与你相同的方式解决了它......我记得我发现了一些关于为什么它不起作用的主题。这是关于 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.

洒一地阳光 2024-10-14 06:27:53

尝试以第一种方式使其工作,但似乎 HttpParams 接口并不是为此构建的。在谷歌搜索了一段时间后,我发现这个答案解释了它:

HttpParams 接口不是用于指定查询字符串参数,而是用于指定 HttpClient 对象的运行时行为。

不过,该文档并不那么具体:

HttpParams 接口表示定义组件运行时行为的不可变值的集合。

为了设置连接和请求超时,我混合使用了 HttpParamsList,它功能齐全并使用 AndroidHttpClient 类,可从 API 8 获取:

public HttpResponse securityCheck(String loginUrl, String name, String password) {
    AndroidHttpClient client = AndroidHttpClient.newInstance(null);
    HttpPost requestLogin = new HttpPost(
            loginUrl + "?");

    //Set my own params using NamaValuePairs
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("j_username", name));
    params.add(new BasicNameValuePair("j_password", password));

    //Set the timeouts using the wrapped HttpParams
    HttpParams httpParameters = client.getParams();
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    int timeoutSocket = 5000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    try {
        requestLogin
                .setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        HttpResponse response = client.execute(requestLogin);
        return response;
    } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
        return null;
    }finally {
        client.close();
    }
}

另请参阅:

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 HttpParams interface isn't there for specifying query string parameters, it's for specifying runtime behaviour of the HttpClient object.

The documentation isn't so specific, though:

HttpParams interface represents a collection of immutable values that define a runtime behavior of a component.

For setting connection and request timeouts, I've used a mix of both HttpParams and List<NameValuePair>, which is fully functional and uses the AndroidHttpClient class, available from API 8:

public HttpResponse securityCheck(String loginUrl, String name, String password) {
    AndroidHttpClient client = AndroidHttpClient.newInstance(null);
    HttpPost requestLogin = new HttpPost(
            loginUrl + "?");

    //Set my own params using NamaValuePairs
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("j_username", name));
    params.add(new BasicNameValuePair("j_password", password));

    //Set the timeouts using the wrapped HttpParams
    HttpParams httpParameters = client.getParams();
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    int timeoutSocket = 5000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    try {
        requestLogin
                .setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        HttpResponse response = client.execute(requestLogin);
        return response;
    } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
        return null;
    }finally {
        client.close();
    }
}

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文