2014-01-20 69 views
1

我想通過這段代碼下載一些xml文件。 當網絡可用時,一切工作正常。當網絡完全不存在時,我已經處理了這個問題。但是現在面臨的問題是,當網絡速度很慢時,下載不會超時。Xml從server.Timeout不能正常工作

超時根本不起作用,我想。如何使它工作?

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

這對您有幫助嗎? :http://stackoverflow.com/a/4089528/1405983 –

+0

@Prince不,它檢查互聯網連接。我想檢查慢互聯網。想要取消下載,如果它需要很長時間 – Payal

+0

Okey!只是讀了這個:http://stackoverflow.com/a/11942965/1405983 –

回答

1

中的onclick whendownload按鈕 在課堂上doinbackground的最後一行下載任務

count_Timer.start(); 

的開始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.cancel(); 

和postexecute方法

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

      if (b) { 

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

      } else { 

      } 
+0

黑客類..但爲我工作..!謝謝.. :) – Payal

+0

plz upvote this –