【问题标题】:Android WebView won't load remote url from anchor tagAndroid WebView 不会从锚标记加载远程 url
【发布时间】:2016-01-11 19:28:51
【问题描述】:

我正在构建我的第一个 android 应用程序并遇到问题。基本上,我正在尝试将我用 PHP 构建的 Youtube 2 Mp3 页面移植到移动应用程序中。我已成功完成此操作,但我遇到的问题是在转换为 mp3 后,我无法从 android 应用程序页面上打印的链接下载 mp3 文件。它可以直接从网页正常工作,如果我让应用程序在 androids 默认浏览器 ( Chrome ) 中加载链接,它可以正常工作,但是当我让应用程序在 WebView 中加载链接时它不起作用。

文件: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 中加载点击的链接,那么当我点击链接时什么都不做。

【问题讨论】:

  • 你给链接的相对路径了吗?如果可能,请发布您用于超链接的代码

标签: javascript php android mobile webview


【解决方案1】:

在验证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);
            }

【讨论】:

  • 我已经在清单中找到了。它正确加载需要互联网的页面,只是页面上的超链接在 android 应用程序中不起作用。
  • 如果您正在寻找这种功能,请告诉我
  • 并添加权限<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  • 你先生......是男人!奇迹般有效。非常感谢
  • 很高兴能帮上忙!
【解决方案2】:
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?mp3=' + encodeURI(songFile) + '">Download your MP3 file</a>

此代码绝对不能在 android web 视图中运行,因为它包含 PHP 代码,而 PHP 代码是服务器端代码,而 android web 视图只能运行客户端代码。

如果您想运行它,则从服务器获取数据作为 URL 并将其放入锚标记中,或者您可以尝试将完整的硬编码 URL 放入锚标记中。

【讨论】:

  • 是的,代码来自服务器端...我只是向您展示了网页如何根据您的请求生成超链接。
猜你喜欢
  • 2018-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多