Question

I am currently using android-async-http library to send a post/get requests. I didn't have any problem before but now i realize that it gives me timeout error if i send this request without image data. (There is no error if i send exact same request by putting image data as well.)

RequestParams params = new RequestParams();
params.add("mail", mail.getText().toString());
params.add("password", pass.getText().toString());

try {
if (!TextUtils.isEmpty(imagePath))
    params.put("image", new File(imagePath));
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(60000);
client.post("some_url", params, myResponseHandler);

What is the reason of this? Thanks in advance.

Was it helpful?

Solution

After comparing requests and responses, i found out that the case was content-type. With image it was posting multipart, and without it something else.

So i got into RequestParams class in library, and made these changes. Now it works fine. For further troubles i am posting changes that i've made.

I put a flag to determine this request should post as multipart or not:

private boolean shouldUseMultiPart = false;

I created a constructor to set this parameter:

public RequestParams(boolean shouldUseMultiPart) {
    this.shouldUseMultiPart = shouldUseMultiPart;
    init();
}

And then on getEntity() method i applied these lines:

/**
 * Returns an HttpEntity containing all request parameters
 */
public HttpEntity getEntity() {
    HttpEntity entity = null;

    if (!fileParams.isEmpty()) {
        ...
    } else {
        if (shouldUseMultiPart) {
            SimpleMultipartEntity multipartEntity = new SimpleMultipartEntity();

            // Add string params
            for (ConcurrentHashMap.Entry<String, String> entry : urlParams
                    .entrySet()) {
                multipartEntity.addPart(entry.getKey(), entry.getValue());
            }

            // Add dupe params
            for (ConcurrentHashMap.Entry<String, ArrayList<String>> entry : urlParamsWithArray
                    .entrySet()) {
                ArrayList<String> values = entry.getValue();
                for (String value : values) {
                    multipartEntity.addPart(entry.getKey(), value);
                }
            }

            entity = multipartEntity;
        } else {
            try {
                entity = new UrlEncodedFormEntity(getParamsList(), ENCODING);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }

    return entity;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top