【问题标题】:App freezes during internet operation httpclient or HttpURLConnection互联网操作 httpclient 或 HttpURLConnection 期间应用程序冻结
【发布时间】:2012-11-20 05:11:44
【问题描述】:

我的应用有一个小部件。我使用 HttpURLConnection 下载内容(每 15 分钟自动下载一次)。下载内容通常需要 10 秒。

问题是在使用应用程序时,当此更新操作在后台进行时,它会冻结/挂起。我正在使用我的小部件类中 updateAppWidget 方法的 handler.postDelayed 。即使我使用的是后台线程,应用程序也会暂时冻结。我想也许 httpConn.connect();可能是问题并使用了 DefaultHttpClient。还是一样的冻结效果。

有人可以提供一些关于这个问题的见解吗?

谢谢...

来自使用此处理程序的小部件类...

Handler handler = new Handler();

handler.postDelayed(new Runnable() {

   public void run() {

    //download and update widget UI here.....   

   }

}, 1000);

private String download1(String urlString) {

InputStream in = null;
byte[] data = null;
URLConnection conn = null;
try
 {
    URL url = new URL(urlString);
    conn = url.openConnection();

    if ((conn instanceof HttpURLConnection))
    {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setConnectTimeout(30000);
        httpConn.setReadTimeout(30000);
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK)
        {
            in = httpConn.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int c;
            while((c = in.read()) > -1){
                        baos.write(c);
        }
                    data = baos.toByteArray();
                    baos.close();
                    in.close();
                    String str = new String(data);
                    System.out.println(str);
                    httpConn.disconnect();
                    ((HttpURLConnection) conn).disconnect();
                    return str;
        }
        else
            {
                    httpConn.disconnect();
                    ((HttpURLConnection) conn).disconnect();
            return("Error: Invalid data");
    }


    }
}
catch (Exception ex)
{
    Log.e("TAG",ex.getMessage().toString());
    return("Error: No connection");
}
finally
{
    try
    {
        if (conn != null)
        {
            conn = null;
        }
        if (in != null)
        {
            in.close();
            in = null;
        }

    }catch(IOException ex)
    {
        return("Error: "+ex.getMessage());
    }
}
return null;

}

【问题讨论】:

  • 你怎么打电话给download1()
  • 添加了处理程序调用...请您提出一些建议。我使用 postdelayed 的原因是我认为它需要在 UI 线程上运行才能更新小部件。

标签: android httpclient httpurlconnection


【解决方案1】:

当您使用handler.postDelayed 时,您发布的Runnable 将在UI 线程上运行。您需要创建一个Thread(或AsyncTask,或ScheduledThreadPoolExecutor 等)以使网络活动脱离UI 线程。

如果没有看到您的代码,很难就如何重构它提供具体建议。关键是Handler不会将工作移出 UI 线程。事实上,它通常用于完全相反的情况:作为后台线程在 UI 线程上运行的一种方式。

【讨论】:

  • @lumpawire - 在您发布到处理程序的Runnable 中,启动一个调用download1() 的单独线程。当它返回时,Runnable 应该使用处理程序发布另一个Runnable 来更新 UI(在 UI 线程上)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-02
  • 2014-07-18
  • 2018-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多