2013-02-11 88 views
0

我有一个按钮和一个标签的活动。 按钮点击我的应用程序必须下载几个文件(约9000)。 如果用户再次点击按钮,则必须停止下载,并在另一次点击时必须从头开始。如何启动和停止下载多个文件在android

所以这是我做的:

在活动:

file.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Button b = (Button)v; 
      if(canFile){ 
       b.setText("Stop download"); 
       changeLabelInfo("Getting file list..."); 
       labelFile.setVisibility(View.VISIBLE); 
       fileTask.start(); 
      }else{ 
       b.setText("Download files"); 
       if(fileTask.isAlive()){ 
        fileTask.interrupt(); 
        fileTask = null; 
        fileTask = new UpdateFilesThread(this); 
       } 
       labelFile.setVisibility(View.INVISIBLE); 
       Kernel.setManualUpdate("file",false); 
      } 
      canFile = !canFile; 
     } 
    }); 

必须下载文件的主题是UpdateFilesThread

public class UpdateFilesThread extends Thread{ 
    private MainActivity activity; 
    private final String rootPath = "/mnt/local/"; 
    public UpdateFilesThread(MainActivity activity){ 
     this.activity = activity; 
    } 


    public void run(){ 
     String json = getFilesURL(); 
     JSONObject a = (JSONObject)JSONValue.parse(json); 
     boolean isZip = false,canDownload = true; 
     String[] keys = new String[]{"key1","key2","key3","key4"}; 

     for(String key:keys){ 
      Object folder = (Object)a.get(key); 
      if(folder instanceof JSONObject){ 
       JSONObject fold = (JSONObject)folder; 
       for(Object path_o:fold.keySet()){ 
        path = path_o.toString().replace(" ", "%20"); 
        if(local.endsWith(".php")){ 
         isZip = true; 
         try { 
          Jsoup.connect(mywebserviceURL).data("path",path).timeout(0).post(); // If php generate zip containing php file 
         } catch (IOException e) { 
          canDownload = false; 
         } 
        } 
        if(canDownload){ 
         try{ 
          if(downloadFromUrl(path,isZip)) 
           //SAVE URL DOWNLOADED 
         }catch(Exception e){ 
          e.printStackTrace(); 
         } 
        } 
        canDownload = true; 
        isZip = false; 
       } 
      } 
     } 
     a.remove(key); 
    } 

    private String getFilesURL(){ 
     try { 

      MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
      entity.addPart("type", new StringBody("all")); 
      HttpPost post = new HttpPost("mywebserviceURL"); 
      post.setEntity(entity); 
      HttpClient client = new DefaultHttpClient(); 
      HttpResponse response = client.execute(post); 

      return EntityUtils.toString(response.getEntity()); 
     } catch (UnsupportedEncodingException e) { 
      Support.writeError(e, null); 
      e.printStackTrace(); 
      return ""; 
     } catch (ClientProtocolException e) { 
      Support.writeError(e, null); 
      e.printStackTrace(); 
      return ""; 
     } catch (ParseException e) { 
      Support.writeError(e, null); 
      e.printStackTrace(); 
      return ""; 
     } catch (IOException e) { 
      Support.writeError(e, null); 
      e.printStackTrace(); 
      return ""; 
     } 
    } 
    public boolean downloadFromUrl(String path,boolean isZip){ 
     InputStream is = null; 
     FileOutputStream fos = null; 
     String localFilename = rootPath+path; 
     String local = isZip?rootPath+"tmp.zip":localFilename; 


     boolean return_ = false; 
     try { 
      URL url = new URL(isZip?mywebserviceURLZip:mywebserviceURLZip+path); 
      URLConnection urlConn = url.openConnection(); 
      urlConn.setReadTimeout(0); 
      is = urlConn.getInputStream(); 
      fos = new FileOutputStream(local); 

      byte[] buffer = new byte[51200]; 
      int len; 

      while ((len = is.read(buffer)) > 0) { 
       fos.write(buffer, 0, len); 
      } 
      fos.close(); 
      is.close(); 
      if(isZip){ 
       ZipFile zip = new ZipFile(local); 
       zip.extractAll(rootPath); 
       new File(local).delete(); 
      } 

      return_= true; 
     }catch(Exception e){ 
      e.printStackTrace(); 
      return false; 
     } 
     return return_; 
    } 
} 

当用户点击两个时间我的问题,新生儿按钮(停止下载并重新开始)。 提示错误说该线程已经开始并在运行..我该如何解决它?我知道asyncTask应该更好,但我的问题导致我的应用程序有这么多的线程运行和设备是如此糟糕的形成.. 有可能停止definitelly线程?有其他更好的解决方案吗?

回答

0

你运行线程需要偶尔检查isInterrupted()并退出,如果它返回true。否则,线程将永远不会被取消。

我觉得你的整个架构是错的。将9000个文件下载到手机上?即使每个文件只有1KB,这对于移动应用程序来说也是一个巨大的内存。至少你应该将这些数据压缩并下载一个zip文件,这是为了你自己的理智。

+0

这是没有问题的..这个程序将围绕我市许多平板电脑上运行,需要只执行这个程序...那么ü有一个更好的架构的提醒? – JackTurky 2013-02-11 16:56:29