取消正在进行的连接时,用户presses回 [英] Cancel an ongoing connection when user presses back

查看:147
本文介绍了取消正在进行的连接时,用户presses回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找关于这一点,但我没有找到我的具体问题。据我所知, AskyncTask 可以使用被取消 .cancel(真)但这只是如果我有一个循环,其中我可以检查值 isCanceled()

I have been searching about this but I did not find my specific question. I understand that AskyncTask can be canceled using .cancel(true) but this only happens if I have a loop in which I can check the value isCanceled().

但在这里我的问题是...我怎么能取消的AsyncTask (即停留在 httpclient.execute())当用户presses回来?如果用户离开该活动,去到另一个我不希望有AsyncTask的运行,因为这可能会导致内存问题,用户可以导航来回创建任务的人数不详不受控制的数量。这就是我为什么要关闭它们。任何人都知道的方法吗?我张贴了code口用于连接:

But my question here is.. how can I cancel an AsyncTask (that is stuck in httpclient.execute() ) when the user presses back? If the user navigates away from that Activity and goes to another I dont want to have an uncontrolled number of AsyncTask running because this may lead to memory problems, the user could navigate back and forth and create undetermined number of tasks. That is why I want to close them. Anyone knows a way? I post the code I use to connect:

public class Test extends Activity {

@Override
protected void onStart() {
    super.onStart();

    new ConnectionTask().execute("https://www.mywebserver.com/webservice.php?param1=test");
}

private class ConnectionTask extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... params) {
        try {

            HttpClient httpclient = DefaultHttpClient(params,clientConnectionManager);
            HttpPost httpPost = new HttpPost(params[0]);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            if(httpEntity != null)
                return EntityUtils.toString(httpEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;        
    }

  }

}

你知道我应该加入的onStop()取消正在进行的httpClient.execute()函数?有时卡住几乎永远。

Do you know what I should add in onStop() to cancel the ongoing httpClient.execute() function? Sometimes gets stuck almost forever.

我真的AP preciate你的帮助,非常感谢你提前。

I would really appreciate your help, thank you very much in advance.

如果我关闭的ConnectionManager 我必须重新做握手对于HTTPS,对不对?看看这个code,当我创建的HttpClient ,我用这个为HTTPS:

If I close the ConnectionManager I have to do the handshaking for https again, right? Look at this code when I create the httpClient, I use this for https:

HttpClient httpclient = DefaultHttpClient(params,clientConnectionManager);

感谢大家的快速反应和各种这里暴露的解决方案。我会尝试使用超时(不不必等待太)+的取消()功能,避免处理 onPostExecute 。我会告诉我们的结果如预期!非常感谢大家!

Thank you all for your quick responses and the variety of solutions exposed here. I will try to use the timeouts (for not having to wait too much) + the cancel() function to avoid processing onPostExecute. I will tell if the results are as expected! Thank you very much you all!

推荐答案

有一个取消()的方法的AsyncTask 类。维护会员的AsyncTask,并取消其在的onDestroy()。然后设置为null成员。

There is a cancel() method in AsyncTask class. Maintain a member to asynctask and cancel it in onDestroy(). then set the member to null.

使用 ClientConnectionManager 来关闭连接。

http://developer.android.com/reference/org/apache/http/conn/ClientConnectionManager.html

检查此链接,设置超时时间为您的连接。

Check this link to set the timeout for your connection.

<一个href="http://stackoverflow.com/questions/693997/how-to-set-htt$p$psponse-timeout-for-android-in-java">How在Java中设定的Htt presponse超时Android版

How to set HttpResponse timeout for Android in Java

这篇关于取消正在进行的连接时,用户presses回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆