下载内容时管理网络更改

时间:2012-06-14 15:53:41

标签: android ssl httpclient

我正在开发的应用程序支持Android 2.3向上。由于下载管理器不支持2.3中的https(我无法理解为什么),我正在实现自己的版本。

我遇到的问题是如果android在下载内容时更改网络(wifi到3g等)我收到以下错误。

06-14 17:26:48.770: W/System.err(15648): javax.net.ssl.SSLException: Read error: ssl=0x270420: I/O error during system call, Connection timed out
06-14 17:26:48.770: W/System.err(15648):    at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_read(Native Method)
06-14 17:26:48.770: W/System.err(15648):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:788)
06-14 17:26:48.770: W/System.err(15648):    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
06-14 17:26:48.770: W/System.err(15648):    at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134)
06-14 17:26:48.770: W/System.err(15648):    at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:174)
06-14 17:26:48.770: W/System.err(15648):    at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:188)
06-14 17:26:48.770: W/System.err(15648):    at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:178)
06-14 17:26:48.770: W/System.err(15648):    at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:140)
06-14 17:26:48.770: W/System.err(15648):    at java.io.BufferedInputStream.read(BufferedInputStream.java:324)
06-14 17:26:48.770: W/System.err(15648):    at java.io.FilterInputStream.read(FilterInputStream.java:133)
06-14 17:26:48.770: W/System.err(15648):    at net.doo.download.ManualDocDownloader$AsyncDocDownloader.doInBackground(ManualDocDownloader.java:126)
06-14 17:26:48.770: W/System.err(15648):    at net.doo.download.ManualDocDownloader$AsyncDocDownloader.doInBackground(ManualDocDownloader.java:86)
06-14 17:26:48.770: W/System.err(15648):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-14 17:26:48.770: W/System.err(15648):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
06-14 17:26:48.780: W/System.err(15648):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
06-14 17:26:48.780: W/System.err(15648):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
06-14 17:26:48.840: W/System.err(15648):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
06-14 17:26:48.845: W/System.err(15648):    at java.lang.Thread.run(Thread.java:1019)

遇到问题的代码看起来像这样......

public class AsyncDocDownloader extends AsyncTask<String, Integer, Boolean> {

        private int id;
        private Notification notification;

        public AsyncDocDownloader(int id, Notification notification) {
            this.id = id;
            this.notification = notification;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {

            notification.contentView.setProgressBar(R.id.status_progress, 100, values[0], false);

            notificationManager.notify(id, notification);
        }

        @Override
        protected Boolean doInBackground(String... values) {
            try {
                HttpGet get = new HttpGet(values[0]);
                HttpResponse resp = httpClientProvider.get().execute(get, context);
                    File dooDirectory = directoryProvider.get();
                    File fileName = new File(dooDirectory, values[1]);
                    InputStream input = new BufferedInputStream(resp.getEntity().getContent());
                    OutputStream output = new FileOutputStream(fileName);

                    int fileLength = Integer.valueOf(resp.getHeaders("Content-Length")[0].getValue());
                    int step = fileLength/20;
                    int counter = 0;
                    int progress = 0;


                    byte data[] = new byte[1024];
                    long total = 0;
                    int count;

                    while ((count = input.read(data)) != -1) {
                        total += count;
                        if(total > counter){
                            progress = progress + 5;
                            publishProgress(progress);
                            counter = step + counter;
                        }
                        output.write(data, 0, count);
                    }

                    output.close();
                    input.close();
                Log.d("DownloadDoc", "Completed download");
                return true;
            } catch (IOException e) {
                Log.d("DownloadDoc", "IOException");
                e.printStackTrace();
                return false;
            }
        }

        @Override
        protected void onPostExecute(Boolean success) {
            Log.d("DownloadDoc", "onPostExecute Called");
            if (success) {
                Intent i = new Intent(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
                application.sendBroadcast(i);
            } else {
                notification.flags = notification.flags | Notification.DEFAULT_ALL;
                notification.contentView.setTextViewText(R.id.status_text, "failed");
                notificationManager.notify(id, notification);
            }

        }
    }

通过提供程序传递的httpClient配置如下(提供程序是因为我使用的是roboguice)

HttpParams params = new BasicHttpParams();

HttpRequestRetryHandler retryhandler = new DefaultHttpRequestRetryHandler(6, true);
// The params are read in the ctor of the pool constructed by
// ThreadSafeClientConnManager, and need to be set before constructing it.
ConnManagerParams.setMaxTotalConnections(params, 200);
ConnPerRoute cpr = new ConnPerRoute() {
    @Override
    public int getMaxForRoute(HttpRoute httpRoute) {
        return 50;
    }
};
ConnManagerParams.setMaxConnectionsPerRoute(params, cpr);

SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();


SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(
        new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", SSLCertificateSocketFactory.getHttpSocketFactory(50000,
        new SSLSessionCache(context)), 443));


ClientConnectionManager conManager = new ThreadSafeClientConnManager(params, schemeRegistry);
DefaultHttpClient httpClient = new DefaultHttpClient(conManager, new BasicHttpParams());
httpClient.setHttpRequestRetryHandler(retryhandler);

感谢您的任何建议

1 个答案:

答案 0 :(得分:2)

06-14 17:26:48.770: W/System.err(15648):    at net.doo.download.ManualDocDownloader$AsyncDocDownloader.doInBackground(ManualDocDownloader.java:126)
06-14 17:26:48.770: W/System.err(15648):    at net.doo.download.ManualDocDownloader$AsyncDocDownloader.doInBackground(ManualDocDownloader.java:86)

这表示您的代码抛出了异常,然后try {} catch()会向上传播。

您应该尝试捕获SSLException以及IOException,以便您可以优雅地恢复并重新启动AsyncTask