Android: handle unexpected internet disconnect while downloading data

Posted by M.A. Cape on Stack Overflow See other posts from Stack Overflow or by M.A. Cape
Published on 2010-03-31T02:26:10Z Indexed on 2010/03/31 2:33 UTC
Read the original article Hit count: 358

Filed under:
|

Hi, I have here a function that downloads data from a remote server to file. I am still not confident with my code. My question is, what if while reading the stream and saving the data to a file and suddenly I was disconnected in the internet, will these catch exceptions below can really catch that kind of incident? If not, can you suggest how to handle this kind of incident?

Note: I call this function in a thread so that the UI won't be blocked.

public static boolean getFromRemote(String link, String fileName, Context context){ 
        boolean dataReceived = false;
        ConnectivityManager connec =  (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

            if (connec.getNetworkInfo(0).isConnected() || connec.getNetworkInfo(1).isConnected()){
                try {
                        HttpClient httpClient = new DefaultHttpClient();
                        HttpGet httpGet = new HttpGet(link);
                        HttpParams params = httpClient.getParams();
                        HttpConnectionParams.setConnectionTimeout(params, 30000);
                        HttpConnectionParams.setSoTimeout(params, 30000);
                        HttpResponse response;
                        response = httpClient.execute(httpGet);
                        int statusCode = response.getStatusLine().getStatusCode();
                        if (statusCode == 200){
                            HttpEntity entity = response.getEntity();



                            InputStream in = null;
                            OutputStream output = null;

                            try{
                                in = entity.getContent();

                                String secondLevelCacheDir = context.getCacheDir() + fileName;

                                File imageFile = new File(secondLevelCacheDir);

                                output= new FileOutputStream(imageFile);
                                IOUtilities.copy(in, output);
                                output.flush();
                            } catch (IOException e) {
                                Log.e("SAVING", "Could not load xml", e);
                            } finally {
                                IOUtilities.closeStream(in);
                                IOUtilities.closeStream(output);
                                dataReceived = true;

                            }
                        }
                    }catch (SocketTimeoutException e){  
                        //Handle not connecting to client !!!!
                        Log.d("SocketTimeoutException Thrown", e.toString());
                        dataReceived = false;

                    } catch (ClientProtocolException e) {
                        //Handle not connecting to client !!!!
                        Log.d("ClientProtocolException Thrown", e.toString());
                        dataReceived = false;

                    }catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        dataReceived = false;
                        Log.d("MalformedURLException Thrown", e.toString());
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        dataReceived = false;
                        Log.d("IOException Thrown", e.toString());
                    } 
                }
            return dataReceived;

        }

© Stack Overflow or respective owner

Related posts about android

Related posts about java