{"id":6195,"date":"2014-04-13T06:38:29","date_gmt":"2014-04-13T06:38:29","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/managing-network-changes-when-downloading-content-collection-of-common-programming-errors\/"},"modified":"2014-04-13T06:38:29","modified_gmt":"2014-04-13T06:38:29","slug":"managing-network-changes-when-downloading-content-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/managing-network-changes-when-downloading-content-collection-of-common-programming-errors\/","title":{"rendered":"Managing Network Changes When Downloading Content-Collection of common programming errors"},"content":{"rendered":"<p>The app I am working on is supporting android 2.3 upwards. As the download manager does not support https in 2.3(I can&#8217;t comprehend why), I am implementing my own version.<\/p>\n<p>The problem I have is if android changes network (wifi to 3g etc) when downloading content I am getting the following error.<\/p>\n<pre><code>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\n06-14 17:26:48.770: W\/System.err(15648):    at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_read(Native Method)\n06-14 17:26:48.770: W\/System.err(15648):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:788)\n06-14 17:26:48.770: W\/System.err(15648):    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)\n06-14 17:26:48.770: W\/System.err(15648):    at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134)\n06-14 17:26:48.770: W\/System.err(15648):    at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:174)\n06-14 17:26:48.770: W\/System.err(15648):    at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:188)\n06-14 17:26:48.770: W\/System.err(15648):    at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:178)\n06-14 17:26:48.770: W\/System.err(15648):    at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:140)\n06-14 17:26:48.770: W\/System.err(15648):    at java.io.BufferedInputStream.read(BufferedInputStream.java:324)\n06-14 17:26:48.770: W\/System.err(15648):    at java.io.FilterInputStream.read(FilterInputStream.java:133)\n06-14 17:26:48.770: W\/System.err(15648):    at net.doo.download.ManualDocDownloader$AsyncDocDownloader.doInBackground(ManualDocDownloader.java:126)\n06-14 17:26:48.770: W\/System.err(15648):    at net.doo.download.ManualDocDownloader$AsyncDocDownloader.doInBackground(ManualDocDownloader.java:86)\n06-14 17:26:48.770: W\/System.err(15648):    at android.os.AsyncTask$2.call(AsyncTask.java:185)\n06-14 17:26:48.770: W\/System.err(15648):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)\n06-14 17:26:48.780: W\/System.err(15648):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)\n06-14 17:26:48.780: W\/System.err(15648):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)\n06-14 17:26:48.840: W\/System.err(15648):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)\n06-14 17:26:48.845: W\/System.err(15648):    at java.lang.Thread.run(Thread.java:1019)\n<\/code><\/pre>\n<p>The code that is having the problem looks like this&#8230;<\/p>\n<pre><code>public class AsyncDocDownloader extends AsyncTask {\n\n        private int id;\n        private Notification notification;\n\n        public AsyncDocDownloader(int id, Notification notification) {\n            this.id = id;\n            this.notification = notification;\n        }\n\n        @Override\n        protected void onProgressUpdate(Integer... values) {\n\n            notification.contentView.setProgressBar(R.id.status_progress, 100, values[0], false);\n\n            notificationManager.notify(id, notification);\n        }\n\n        @Override\n        protected Boolean doInBackground(String... values) {\n            try {\n                HttpGet get = new HttpGet(values[0]);\n                HttpResponse resp = httpClientProvider.get().execute(get, context);\n                    File dooDirectory = directoryProvider.get();\n                    File fileName = new File(dooDirectory, values[1]);\n                    InputStream input = new BufferedInputStream(resp.getEntity().getContent());\n                    OutputStream output = new FileOutputStream(fileName);\n\n                    int fileLength = Integer.valueOf(resp.getHeaders(\"Content-Length\")[0].getValue());\n                    int step = fileLength\/20;\n                    int counter = 0;\n                    int progress = 0;\n\n\n                    byte data[] = new byte[1024];\n                    long total = 0;\n                    int count;\n\n                    while ((count = input.read(data)) != -1) {\n                        total += count;\n                        if(total &gt; counter){\n                            progress = progress + 5;\n                            publishProgress(progress);\n                            counter = step + counter;\n                        }\n                        output.write(data, 0, count);\n                    }\n\n                    output.close();\n                    input.close();\n                Log.d(\"DownloadDoc\", \"Completed download\");\n                return true;\n            } catch (IOException e) {\n                Log.d(\"DownloadDoc\", \"IOException\");\n                e.printStackTrace();\n                return false;\n            }\n        }\n\n        @Override\n        protected void onPostExecute(Boolean success) {\n            Log.d(\"DownloadDoc\", \"onPostExecute Called\");\n            if (success) {\n                Intent i = new Intent(DownloadManager.ACTION_DOWNLOAD_COMPLETE);\n                application.sendBroadcast(i);\n            } else {\n                notification.flags = notification.flags | Notification.DEFAULT_ALL;\n                notification.contentView.setTextViewText(R.id.status_text, \"failed\");\n                notificationManager.notify(id, notification);\n            }\n\n        }\n    }\n<\/code><\/pre>\n<p>The httpClient that is passed via the provider is configured like so (the provider is because I am using roboguice)<\/p>\n<pre><code>HttpParams params = new BasicHttpParams();\n\nHttpRequestRetryHandler retryhandler = new DefaultHttpRequestRetryHandler(6, true);\n\/\/ The params are read in the ctor of the pool constructed by\n\/\/ ThreadSafeClientConnManager, and need to be set before constructing it.\nConnManagerParams.setMaxTotalConnections(params, 200);\nConnPerRoute cpr = new ConnPerRoute() {\n    @Override\n    public int getMaxForRoute(HttpRoute httpRoute) {\n        return 50;\n    }\n};\nConnManagerParams.setMaxConnectionsPerRoute(params, cpr);\n\nSSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();\n\n\nSchemeRegistry schemeRegistry = new SchemeRegistry();\nschemeRegistry.register(\n        new Scheme(\"http\", PlainSocketFactory.getSocketFactory(), 80));\nschemeRegistry.register(new Scheme(\"https\", SSLCertificateSocketFactory.getHttpSocketFactory(50000,\n        new SSLSessionCache(context)), 443));\n\n\nClientConnectionManager conManager = new ThreadSafeClientConnManager(params, schemeRegistry);\nDefaultHttpClient httpClient = new DefaultHttpClient(conManager, new BasicHttpParams());\nhttpClient.setHttpRequestRetryHandler(retryhandler);\n<\/code><\/pre>\n<p>Thanks for any advice<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The app I am working on is supporting android 2.3 upwards. As the download manager does not support https in 2.3(I can&#8217;t comprehend why), I am implementing my own version. The problem I have is if android changes network (wifi to 3g etc) when downloading content I am getting the following error. 06-14 17:26:48.770: W\/System.err(15648): [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6195","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6195"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6195\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}