2015-07-22 68 views
0

文件我有一個網址https://www.youtube.com/watch?v=mGhQTs3F1O4&spf=prefetch,當我在它下載包含視頻的信息文件瀏覽器中打開..網址下載android系統

在瀏覽器中效果很好,但我不能說實現在Android的..當我嘗試使用下面的代碼我得到的html文件,下載文件..

private class DownloadTask extends AsyncTask<String, Integer, String> { 

    @Override 
    protected String doInBackground(String... sUrl) { 
     InputStream input = null; 
     OutputStream output = null; 
     HttpURLConnection connection = null; 
     String path = Environment.getExternalStorageDirectory().getPath() 
       + "/" + sUrl[1]; 
     Log.d(TAG, "PATH: " + path); 
     try { 
      URL url = new URL(sUrl[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.connect(); 

      // expect HTTP 200 OK, so we don't mistakenly save error report 
      // instead of the file 
      if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { 
       return "Server returned HTTP " 
         + connection.getResponseCode() + " " 
         + connection.getResponseMessage(); 
      } 

      // this will be useful to display download percentage 
      // might be -1: server did not report the length 
      int fileLength = connection.getContentLength(); 

      // download the file 
      input = connection.getInputStream(); 
      output = new FileOutputStream(path); 

      byte data[] = new byte[4096]; 
      int count; 
      while ((count = input.read(data)) != -1) { 
       // allow canceling with back button 
       if (isCancelled()) { 
        input.close(); 
        return null; 
       } 

       output.write(data, 0, count); 
      } 
     } catch (Exception e) { 
      return e.toString(); 
     } finally { 
      try { 
       if (output != null) 
        output.close(); 
       if (input != null) 
        input.close(); 
      } catch (IOException ignored) { 
      } 

      if (connection != null) 
       connection.disconnect(); 
     } 
     return path; 
    } 
} 

所以..任何想法?

+0

嘗試設置在請求的MIME類型。 – avinash

回答

0

編輯沒有過時類的用法:

看來,他們檢查用戶代理,所以你必須設定這樣的:

connection = (HttpURLConnection) url.openConnection(); 
connection.setRequestProperty("User-Agent", "Mozilla/5.0") 
connection.connect(); 

OLD回答: 對我來說,它的工作原理使用HTTPClient而不是HTTPConnection(在我看來,對於這種情況,沒有理由使用後一類)

下面是一個例子:

@Override 
protected String doInBackground(String... sUrl) { 
    HttpClient client = new DefaultHttpClient(); 
    HttpGet get = new HttpGet(sUrl[0]); 
    try { 
     URL url = new URL(sUrl[0]); 
     HttpResponse resp = client.execute(get); 
     return EntityUtils.toString(resp.getEntity()); 

    } catch (Exception e) { 
      return e.toString(); 
    } 
} 

您還應該考慮的RetroFit使用。

+0

其實這兩個類已被棄用。改爲使用UrlConnection。檢查這個[參考](http://android-developers.blogspot.com/2011/09/androids-http-clients.html) – mhenryk

+0

編輯,謝謝我想我錯過了那個帖子! – Jibbo

+0

@OP我的答案幫助你可以檢查它是否正確? – Jibbo

0

嘗試以下代碼:

class A extends AsyncTask<Void, Void, Void> 
{ 
     @Override 
      protected void onPostExecute(Void result) { 
       Log.e("ASYNCTASK", "DONE"); 
       } 
    @Override 
    protected Void doInBackground(Void... params) { 
     try { 
      String url="https://www.youtube.com/watch?v=mGhQTs3F1O4&spf=prefetch"; 
      HttpGet httpGet=new HttpGet(url); 
      HttpClient client=new DefaultHttpClient(); 
     HttpResponse httpResponse= client.execute(httpGet); 
      if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK) 
      { 
       //long fileLength = httpResponse.getEntity().getContentLength(); 
      InputStream input = httpResponse.getEntity().getContent(); 
       FileOutputStream output = new FileOutputStream("mnt/sdcard/yahoo.json"); 
       byte data[] = new byte[4096]; 
       int count; 
       while ((count = input.read(data)) != -1) { 
        // allow canceling with back button 
        if (isCancelled()) { 
         input.close(); 
         return null; 
        } 

        output.write(data, 0, count); 
       } 
       output.close(); 
      } 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return null; 
    } 

}