2013-11-03 6 views
1

BLOB-store에서 zip 파일을 제공하는 appengine 애플리케이션이 있습니다. 나는이 URL + 쿼리를 사용하는 경우 : 크롬이나 파이어 폭스에서android app에서 Google Appengine에서 BLOB을 다운로드하는 방법

http://spiceappcloud.appspot.com/bundle-service?spice_category=Education&spice_sub_category0=Topography&spice_sub_category1=Outdoor%20Skills&spice_name=Topo.zip

이 파일이 다운로드됩니다를! Android 앱 내의 서비스에서 동일한 파일을 어떻게 다운로드합니까?

코드를 고맙게 생각합니다.

+1

URL 연결은 내 PC [HFS]의 FTP에는 완벽하게 작동하지만 appengine blob에는 적합하지 않습니다. 마침내 HttpClient 및 HttpGet을 사용하여 작동하도록했습니다. 자세한 내용은 답변을 참조하십시오. –

답변

0

확인 HttpClient 인 HttpClient &을 사용하여 시행 착오를 거쳤습니다.

private boolean downloadFile(String query) 
    { 
     Log.d(TAG, "trying to download file"); 

     boolean downloadSuccessful = false; 

     if(!fileName.endsWith(".zip")) 
     { 
      fileName += ".zip"; 
      Log.d(TAG, "added zip to file name"); 
     }  

     is = null; 
     bis = null; 
     fos = null; 
     bos = null; 
     outFile = null; 

     int counter = 0; 

     while(!downloadSuccessful && counter < 5) 
     { 
      Log.d(TAG + ".downloadFile()", "attempt number: " + counter); 

      try 
      { 
       //connect(query); 
       HttpClient client = new DefaultHttpClient(); 

       HttpGet httpGet = new HttpGet(query); 
       HttpResponse response = null; 

       try 
       { 
        response = client.execute(httpGet); 

        StatusLine statusLine = response.getStatusLine(); 
        int statusCode = statusLine.getStatusCode(); 

        Log.d(TAG, "statusCode: " + statusCode); 

        if (statusCode == 200) 
        { 
        HttpEntity entity = response.getEntity(); 

        is = entity.getContent(); 
        bis = new BufferedInputStream(is); 
        } 
        else 
        { 
        Log.e(TAG, "Failed to download file"); 
        } 
       } 
       catch (ClientProtocolException e) 
       { 
        Log.e(TAG, e.getMessage()); 
       } 
       catch (IOException e) 
       { 
        Log.e(TAG, e.getMessage()); 
       } 


       Header[] hs = response.getAllHeaders(); 

       if(hs == null) 
       { 
        Log.e(TAG, "Headers are null "); 
        return false; 
       } 

       for(Header h: hs) 
       { 
        Log.d(TAG, "Header values: " + h.getValue()); 
       } 



       fileSize = 200000; 

       if(counter == 0) 
       { 
        Log.e(TAG, "File size is: " + fileSize); 
       } 
       else 
       { 
        Log.e(TAG, "This is what's left to download: " + fileSize); 
       } 

       sendNotifiction("Connecting", "Connecting...", "Spice bundle: " + fileName); 
       updateUser(MESSAGE_CONNECTING_STARTED, fileSize/1024); 

       outFile = new File(ZIP_LOCATION + "/" + fileName); 
       fos = new FileOutputStream(outFile); 
       bos = new BufferedOutputStream(fos, DOWNLOAD_BUFFER_SIZE); 
       data = new byte[DOWNLOAD_BUFFER_SIZE]; 
       bytesRead = 0; 
       totalRead = 0; 

       downloadSuccessful = download(); 


       Log.d(TAG, "Total bytes Read: " + totalRead + " of: " + fileSize); 

       if(totalRead == fileSize) 
       { 
        downloadSuccessful = true; 

        Log.e(TAG, "Download successful use this message wairily"); 
       } 
       else 
       { 
        Log.e(TAG, "Download fubar"); 
       } 
      } 
      catch(FileNotFoundException e) 
      { 
       Log.e(TAG, "FileNotFoundException: " + e.getMessage()); 
       notificationFlash = "Error downloading"; 
       notificationTitle = "Error"; 
       notificationText = "Spice bundle: " + fileName; 

       sendNotifiction(notificationFlash, notificationTitle, notificationText); 
       updateUser(MESSAGE_DOWNLOAD_ERROR, 0); 
      } 
      catch(NullPointerException e) 
      { 
       Log.e(TAG, "NullPointerException"); 
      } 
      catch(IllegalStateException e) 
      { 
       Log.e(TAG, e.getMessage()); 
      } 
      catch(Exception e) 
      { 
       Log.e(TAG, "Exception: " + e.getMessage()); 

       notificationFlash = "Error downloading"; 
       notificationTitle = "Error"; 
       notificationText = "Spice bundle: " + fileName; 

       sendNotifiction(notificationFlash, notificationTitle, notificationText); 
       updateUser(MESSAGE_DOWNLOAD_ERROR, 0); 
      } 

      counter++; 
     } 

     try 
     { 
      if(bos != null) 
      { 
       bos.close(); 
      } 

      if(fos != null) 
      { 
       fos.close(); 
      } 

      if(bis != null) 
      { 
       bis.close(); 
      } 

      if(is != null) 
      { 
       is.close(); 
      } 
     } 
     catch (IOException e) 
     { 
      Log.e(TAG + " given up downloading", "IOException thrown"); 

      notificationFlash = "Error downloading"; 
      notificationTitle = "Error"; 
      notificationText = "Spice bundle: " + fileName; 

      sendNotifiction(notificationFlash, notificationTitle, notificationText); 
      updateUser(MESSAGE_DOWNLOAD_ERROR, 0); 
     } 

     return downloadSuccessful; 
    } 

    private boolean download() 
    { 
     boolean downloadComplete = false; 

     try 
     { 
      while(!this.isCancelled()) 
      { 
       bytesRead = bis.read(data, 0, data.length); 

       if(bytesRead < 0) 
       { 
        downloadComplete = true; 
        break; 
       } 

       bos.write(data, 0, bytesRead); 

       totalRead += bytesRead; 
       int totalReadInKB = totalRead/1024; 

       if(totalReadInKB % 100 == 0) 
       { 
        Log.d(TAG, "Total Read In KB: " + totalReadInKB + " of: " + fileSize/1024); 
        updateUser(MESSAGE_UPDATE_PROGRESS_BAR, totalReadInKB); 
       } 
      } 
     } 
     catch(IOException e) 
     { 
      Log.e(TAG + ".download()", e.getMessage()); 
      Log.e(TAG + ".download()", "IOException thrown download incomplete"); 
     } 
     finally 
     { 
      try 
      { 
       bos.flush(); 
      } 
      catch (IOException e) 
      { 
       Log.e(TAG + ".download()", e.getMessage()); 
       Log.e(TAG + ".download()", "IOException thrown couldn't flush"); 
      } 
     } 

     return downloadComplete; 
    } 
관련 문제