Question

I am trying to download some xml files through this code. Everything works fine when network is available.I have already handled the issue when network is not at all there. But the issue m facing right now is that ,when network is slow ,the download doesnt timeout.

Time out is not at all working ,I think.How to make it work?

            URL mUrl = new URL(url[i]);
            URLConnection conexion = mUrl.openConnection();
            response[i] = new DownloadResponse();
            response[i].setSuccessful(false);
            conexion.connect();
            conexion.setConnectTimeout(10000);
            // this will be useful so that you can show a typical 0-100% progress bar
            ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
            if (outputFile != null) { // one output file specified in constructor
                output = new BufferedOutputStream(new FileOutputStream(
                        outputFile));
            } else if (outputFiles != null) { // an array of output files specified
                output = new BufferedOutputStream(new FileOutputStream(
                        outputFiles[i]));
            } else {// no output file specified
                output = new BufferedOutputStream(outBytes);
            }
            InputStream input = new BufferedInputStream(mUrl.openStream());
            byte data[] = new byte[1024];
            long total = 0;
            while ((count = input.read(data)) != -1) {
                total += count;
                output.write(data, 0, count);
            }
            output.flush();
            response[i].setSuccessful(true);
            response[i].count = i;
            response[i].setResponseText(outBytes.toString());
            if (total < 32 && outBytes.toString().indexOf("Invalid") < -1){
                response[i].setSuccessful(false);
            }

            output.close();
            input.close();
            publishProgress(response[i]);
Was it helpful?

Solution

in onclick whendownload button define globally in class boolean b = true;

count_Timer = new CountDownTimer(20000, 1000) {
            @Override
            public void onTick(long arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onFinish() {

                b = false;
                pd.dismiss();
                dialog.dismiss();
                AlertDialog.Builder builder1 = new AlertDialog.Builder(
                        AppSetting.this);
                builder1.setTitle("appname");
                builder1.setMessage("You can't Continue because internet is Very slow.");
                builder1.setNeutralButton("OK",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {

                            }
                        });
                builder1.show();

            }
        };

on beginning of download task

count_Timer.start();

on last line of doinbackground

count_Timer.cancel();

and on postexecute method

if (pd.isShowing())
                pd.dismiss();

            if (b) {

                dialog.dismiss();
                simplealert(result.replace("\"", ""));

            } else {

            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top