XML从Server.TimeOut下载不起作用



我正在尝试通过此代码下载一些XML文件。当可用网络时,一切都很好。当网络根本不存在时,我已经处理了问题。但是,现在面临的问题是,当网络慢时,下载未超时。

我认为,超时根本不起作用。如何使其起作用?

            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]);

在OnClick whendload按钮中在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();
            }
        };

下载任务的开始

count_Timer.start();

在doinbackground的最后一行

count_Timer.cancel();

和执行后方法

if (pd.isShowing())
                pd.dismiss();
            if (b) {
                dialog.dismiss();
                simplealert(result.replace(""", ""));
            } else {
            }

相关内容

  • 没有找到相关文章

最新更新