android - URL 重定向无法通过 Intent 在 Android 中工作 - (页面未找到错误)

标签 android mobile-browser

我有这个代码:

Button bluehost = (Button)findViewById(R.id.bluehost);
bluehost.setOnClickListener(new Button.OnClickListener()
{
    public void onClick(View v)
    {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                Uri.parse("https://www.bluehost.com/track/businessplan/"));
        startActivity(browserIntent);
    }
});

从技术上讲,此代码可以将人发送到该 url,但由于某种原因,此特定 URL 会转到此代码中未找到的页面,但如果您将此 url 放入浏览器,则会呈现。但是当从应用程序完成时,它会转到未找到的页面。我正在使用 Android Safari 浏览器进行测试。

是因为 URL 上的重定向吗?我该如何解决这个问题?

我的 list 文件中确实有这一行:

<uses-permission android:name="android.permission.INTERNET" />

注意:

互联网连接不是问题。此代码适用于其他 URL。这个特定链接发生了一些奇怪的事情。

谢谢!

最佳答案

下面我的答案中的代码和相应的重定向对我有用。

在我的 Xperia Z Ultra 上,它看起来像这样:

根据安卓官方documentation ,它也应该适用于低于 19 的先前 api 级别。我假设 Chrome 文档提到了最低 api 级别编号 19,因为它专门针对想要使用其 HTML5 功能的 Web 开发人员。

话虽如此,此解决方案不适用于我的旧 Sprint XPRT(Android 2.3.5 - API 级别 10),并且它不适用于模拟 Nexus S(上面有 KitKat 的旧图像)。它在两者上都显示“找不到网页”。当我使用浏览器在 XPRT 上进一步尝试但手动输入 url 时,我收到“无法建立安全连接”的对话框。不幸的是,删除 https 中的 's' 也不起作用,因为它重定向到安全版本,我仍然得到“无法建立安全连接”。

较早的答案:

如果您关注 Chrome WebView tutorial确切地。你会到达这部分。 粗体的重点是我的。

Handling Navigation

Now try changing the URL you're loading to http://www.html5rocks.com/ and rerun your application. You'll notice something strange.

If you run the application now with a site that has a redirect like html5rocks.com, your app ends up opening the site in a browser on the device, not in your WebView -- probably not what you expected. This is because of the way the WebView handles navigation events.

Here's the sequence of events:

  1. The WebView tries to load the original URL from the remote server, and gets a redirect to a new URL.

  2. The WebView checks if the system can handle a view intent for the URL, if so the system handles the URL navigation, otherwise the WebView will navigate internally (i.e. the user has no browser installed on their device).

  3. The system picks the user's preferred application for handling an http:// URL scheme -- that is, the user's default browser. If you have more than one browser installed, you may see a dialog at this point.

换句话说,如果您在此时停止教程,您将获得您想要的行为。

这是 link 的实际代码该教程停止工作:

MainActivity.java

public class MainActivity extends Activity {

 private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       mWebView = (WebView) findViewById(R.id.activity_main_webview);
       mWebView.loadUrl("https://www.bluehost.com/track/businessplan/");
    }

    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }     
}

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/container"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".MainActivity">
     tools:ignore="MergeRootFrame">

         <WebView
         android:id="@+id/activity_main_webview"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
</FrameLayout>

对于其他阅读本文的人,我没有在 list 中提及所需的 INTERNET 权限,因为最初询问的人已经涵盖了该部分。

关于android - URL 重定向无法通过 Intent 在 Android 中工作 - (页面未找到错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34562119/

相关文章:

android - 如果使用微调器,如何 checkin listView。安卓

java - 带有字符串数组 toString() 输出的 JSONObjcet 具有整数值而不是字符串

javascript - 检查浏览器是否支持 HTML5 timepicker

javascript - 使滚动条在移动浏览器中可见

Facebook 好友对话框不适用于移动设备

android - 带有 JSON 数组的 ExpandableListView

android - 在 Android 中将联系人数据加入我的表格

java - 异常:ClassNotFoundException & NoClassDefFoundError At Release Run Application

objective-c - 如何让我的 UIWebView 或 NSURLConnection 告诉网站它是移动浏览器?

javascript - 音频未在移动浏览器上加载(Plyr 播放器)