在线程中使用 httpGet 将最新更新发送到网站

发布于 12-15 18:58 字数 1025 浏览 2 评论 0原文

我有以下问题:

  • 我的应用程序需要尽快通知网站更改
  • 这可能发生得如此之快,以至于之前的 Web 请求尚未完全处理
  • 应用程序应始终至少发送最后一个 Web 请求(它不会)如果之前的丢失也没关系)。

我不确定如何以最佳方式做到这一点。我当前的方法如下,但给了我一个警告(“SingleClientConnManager 的使用无效:连接仍然分配。”)。我怀疑我可以重用这个连接,但不知道如何重用。使用 threadSafeConnManager 似乎不是解决方案,因为我只需要一个连接(我认为:))。

我应该如何根据我的需求优化我的代码?

下面代码中的可运行对象位于线程 (webThread) 中,而 webrequest 是一个设置为某个 url 的全局变量。设置变量后,webThread.run() 被触发。

private Runnable mSyncInternet = new Runnable() {
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();

    public void run() {

        HttpGet httpGet = new HttpGet(webrequest);
        try {
            HttpResponse response = httpClient.execute(httpGet, localContext);
        } catch (ClientProtocolException e) {
            sendMessageToUI(MSG_NO_INTERNET, 1);
        } catch (IOException e) {
            sendMessageToUI(MSG_NO_INTERNET, 1);
        }
    }
};

非常感谢!

I have the following problem:

  • My application needs to inform a website of a change as fast as possible
  • This can happen so fast that the previous webrequest hasn't been completely dealt with
  • The application should always send at least the last webrequest (it doesn't matter if previous ones are lost).

I'm not sure how to do this optimally. My current method is below, but gives me a warning ("Invalid use of SingleClientConnManager: connection still allocated."). I suspect I can reuse this connection, but have no clue how. Using threadSafeConnManager doesn't seem to be the solution, since I only need one connection (I think :) ).

How should I optimize my code for my needs?

The runnable in the code below is in a thread (webThread) and webrequest is a global variable that gets set to a certain url. After setting the variable, webThread.run() is fired.

private Runnable mSyncInternet = new Runnable() {
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();

    public void run() {

        HttpGet httpGet = new HttpGet(webrequest);
        try {
            HttpResponse response = httpClient.execute(httpGet, localContext);
        } catch (ClientProtocolException e) {
            sendMessageToUI(MSG_NO_INTERNET, 1);
        } catch (IOException e) {
            sendMessageToUI(MSG_NO_INTERNET, 1);
        }
    }
};

Thanks so much in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慈悲佛祖2024-12-22 18:58:03

就在今天,我发现了一个 博客,其中包含 SingleClientConnManager 问题的解决方案:

public static DefaultHttpClient getThreadSafeClient() {
    DefaultHttpClient client = new DefaultHttpClient();
    ClientConnectionManager mgr = client.getConnectionManager();
    HttpParams params = client.getParams();

    client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, 
            mgr.getSchemeRegistry()), params);

    return client;
}

我尝试过,效果非常好!

Just today I found a blog with the solution to the SingleClientConnManager issue:

public static DefaultHttpClient getThreadSafeClient() {
    DefaultHttpClient client = new DefaultHttpClient();
    ClientConnectionManager mgr = client.getConnectionManager();
    HttpParams params = client.getParams();

    client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, 
            mgr.getSchemeRegistry()), params);

    return client;
}

I tried it, and it worked perfectly!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文