14

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!

1
  • 1
    +1 -- Same issue here...switched to using ArrayList<NameValuePair> and everything is working now...don't understand why the same logic with HttpParams doesn't work!
    – Vladimir
    Apr 12, 2011 at 6:11

2 Answers 2

2

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.

1
  • 3
    Yeah your are right with that, but I don't get it. Why is there a API for HttpParams if it isn't obviously not working at all? Dec 13, 2010 at 18:05
1

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:

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.