Skip to content

Run on current thread if no looper is present in Async response #492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

danielrhodes
Copy link

The implementation before essentially means that you can't use this library from short-lived threads. It also starts a looper on a thread not owned by the HTTP library, which is unexpected.

  1. What it would effectively mean is that if you wanted to make an HTTP request from a short-lived thread, you would never receive a response. The expectation of an Async library is that even if the parent thread dies, you would still receive a response.
  2. Declaring a looper inside is very problematic and unexpected behavior.

The following fix does the following:

  • If there is a looper present, the runnable will be run on that thread. Thus, for long-lived threads, you can expect the runnable to be executed on the calling thread.
  • If the looper is not present, the runnable will be run on the current thread (I assume a thread from the current HTTP thread pool).

@gongzunpan
Copy link

印象笔记无法提交笔记,原因如下:

本月帐户上传流量已经达到上限。

原消息详情:
来自:Daniel Rhodes <notifications@github.com>
发送到:gongzunpan.e099425@m.yinxiang.com
全部收件人:loopj/android-async-http <android-async-http@noreply.github.com>
主题:[android-async-http] Run on current thread if no looper is present in Async response (#492)

为了防止邮件过多,接下来的360分钟内,你将不会收到报错回复。

升级到印象笔记高级帐户,可以发送的邮件数量将从50封提升到200封。
https://app.yinxiang.com/Checkout.action?origin=email%2Dcommerce

  • 印象笔记团队

@@ -30,6 +30,8 @@
import org.apache.http.client.HttpResponseException;
import org.apache.http.util.ByteArrayBuffer;

import com.heyzap.http.AsyncHttpResponseHandler.ResponderHandler;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean com.loopj.http.AsyncHttpResponseHandler.ResponderHandler; right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love it when Eclipse does that.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

印象笔记无法提交笔记,原因如下:

本月帐户上传流量已经达到上限。

原消息详情:
来自:Daniel Rhodes <notifications@github.com>
发送到:gongzunpan.e099425@m.yinxiang.com
全部收件人:loopj/android-async-http <android-async-http@noreply.github.com>; zunpan <gongzunpan@gmail.com>
主题:Re: [android-async-http] Run on current thread if no looper is present in Async response (#492)

为了防止邮件过多,接下来的360分钟内,你将不会收到报错回复。

升级到印象笔记高级帐户,可以发送的邮件数量将从50封提升到200封。
https://app.yinxiang.com/Checkout.action?origin=email%2Dcommerce

  • 印象笔记团队

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this allow me to make async-http calls from within an AsyncTask<..>,
specifically the doInBackground(..) function?

One issue I have is I'm migrating an existing project that uses AsyncTasks
everywhere to do lots of networking calls + other background stuff like
user authentication with Google+.

In doing the migration, I'm trying to keep some of the functionality in the
existing AsyncTask implementations-- but as it stands now I can't make
calls to async-http from within an async task.

On Fri, Mar 14, 2014 at 10:23 AM, Daniel Rhodes notifications@github.comwrote:

In
library/src/main/java/com/loopj/android/http/AsyncHttpResponseHandler.java:

@@ -30,6 +30,8 @@
import org.apache.http.client.HttpResponseException;
import org.apache.http.util.ByteArrayBuffer;

+import com.heyzap.http.AsyncHttpResponseHandler.ResponderHandler;

I love it when Eclipse does that.

Reply to this email directly or view it on GitHubhttps://github.com//pull/492/files#r10617406
.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

印象笔记无法提交笔记,原因如下:

本月帐户上传流量已经达到上限。

原消息详情:
来自:Suneet Shah <notifications@github.com>
发送到:gongzunpan.e099425@m.yinxiang.com
全部收件人:loopj/android-async-http <android-async-http@noreply.github.com>; zunpan <gongzunpan@gmail.com>
主题:Re: [android-async-http] Run on current thread if no looper is present in Async response (#492)

为了防止邮件过多,接下来的360分钟内,你将不会收到报错回复。

升级到印象笔记高级帐户,可以发送的邮件数量将从50封提升到200封。
https://app.yinxiang.com/Checkout.action?origin=email%2Dcommerce

  • 印象笔记团队

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most likely. Without this change, here is what is happening:

  1. You make request on your own thread.
  2. Http Async library uses its own thread to make the request.
  3. It tries to run the callback on original thread. Sees it is gone, and bails.

This can be mitigated by starting a looper which keeps your original thread alive (and thus the handler can execute the Runnable).

In my change, if the original thread is gone, it executes the callback on the HTTP thread. So in your AsyncTask thing, it would likely do the same thing. Try it out.

@fineswap
Copy link
Contributor

@danielrhodes I believe your case is a rare one, why not just override that particular method and create your own response handler? Cleaner, I think, and would better suite your specific needs.

@smarek
Copy link
Member

smarek commented Mar 20, 2014

I like the change, thank you

smarek added a commit that referenced this pull request Mar 20, 2014
@smarek smarek closed this Mar 20, 2014
@smarek smarek added this to the 1.4.5 milestone Mar 20, 2014
@smarek smarek self-assigned this Mar 20, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants