0

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.

4
  • could it simply be that your server is not responding ? Commented Oct 4, 2013 at 10:25
  • But just after this post, i put an image and suddenly server starts responding? Noep, don't think so... There should be something else. Commented Oct 4, 2013 at 10:31
  • may be your server crashes because the image field is empty Commented Oct 4, 2013 at 12:08
  • Noep, it wasn't because of that. Check my answer below, i have already found the error and solved it. Commented Oct 6, 2013 at 16:36

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

How do you handle your file on the server side? I'm trying to implement the same functionality, but i can not catch my image file on the server (asp.net) - i see all request params except of file...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.