2016-01-11 43 views
2

我建立我的第一個Android應用程序和具有問題加載遠程URL。基本上我試圖將我的Youtube 2 Mp3頁面內置在PHP中移動到一個移動應用程序。我已經成功地做到了這一點,但我遇到的問題是轉換爲mp3後,我無法從Android應用程序打印在頁面上的鏈接下載MP3文件。它直接從網頁工作正常,它工作正常,如果我有應用程序加載在Android默認瀏覽器(Chrome)中的鏈接,但它不工作時,我有應用程序加載WebView內的鏈接。的Android的WebView不會從錨標記

FILE:MainActivity.java

public class MainActivity extends AppCompatActivity { 

WebView web; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    web = (WebView) findViewById(R.id.webView); 

    web.setWebViewClient(new myWebClient()); 
    web.getSettings().setJavaScriptEnabled(true); 

    web.loadUrl("http://www.bigjohn863.com/youtubetomp3/index.php"); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 
} 

public class myWebClient extends WebViewClient 
{ 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 

     view.loadUrl(url); 
     return false; 

    } 
} 

網頁:http://www.bigjohn863.com/youtubetomp3/index.php

該網頁的正常瀏覽器加載,它工作正常,如果我用它在Android的默認瀏覽器(Chrome)時,按預期工作,但如果我將代碼更改爲僅在webview中加載單擊的鏈接,那麼當我單擊該鏈接時它什麼也不做。

APP VIEW FROM BLUESTACKS

+0

你給的鏈接的相對路徑?如果可能的話,請發表您的代碼,您曾使用超鏈接 – Cool7

回答

2

嘗試在驗證url後添加以下內容到功能shouldOverrideUrlLoading(WebView view, String url)。這將啓動類似下載到從任何網頁

if (url.endsWith(".mp3")) { 
       Uri source = Uri.parse(url); 
       // Make a new request pointing to the .mp3 url 
       DownloadManager.Request request = new DownloadManager.Request(source); 
       // appears the same in Notification bar while downloading 
       request.setDescription("Description for the DownloadManager Bar"); 
       request.setTitle("YourMp3.mp3"); 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
        request.allowScanningByMediaScanner(); 
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
       } 
       // save the file in the "Downloads" folder of SDCARD 
       request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "MyMp3.mp3"); 
       // get download service and enqueue file 
       DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
       manager.enqueue(request); 
      } 
+0

我已經在清單。它正確加載頁面需要互聯網只是頁面上的超鏈接不適用於Android應用程序。 – BigJohn863

+0

告訴我,如果你正在尋找這種功能 –

+0

並添加權限'<使用許可權的android:NAME =「android.permission.ACCESS_DOWNLOAD_MANAGER」 /><使用許可權的android:NAME =「android.permission.WRITE_EXTERNAL_STORAGE」 />' –

0
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?mp3=' + encodeURI(songFile) + '">Download your MP3 file</a> 

此代碼將挑釁不會在Android網絡視圖中工作,因爲它包含的PHP代碼和PHP代碼是服務器端代碼和android web視圖可運行下載只有客戶端代碼。

如果你想運行它,然後從服務器的URL獲取數據,並把它放入錨標記,也可以通過把一個完整的硬編碼URL錨定標記與嘗試。

+0

是的代碼來自服務器端...我只是向您展示了網頁如何根據您的請求生成超鏈接。 – BigJohn863