URL 通过 Intent 在 Android 中重定向不起作用 -(找不到页面错误)

URL Redirection not working in Android via Intent - (Page Not Found Error)

我有这个代码:

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 呈现=30=]进入浏览器。但是当从应用程序完成后,它会转到未找到的页面。我正在使用 Android Safari 浏览器进行测试。

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

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

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

注意:

互联网连接不是问题。此代码适用于其他 URL。这个特定的 link.

发生了一些奇怪的事情

谢谢!

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

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

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

也就是说,此解决方案不适用于我的旧 Sprint XPRT(Android 2.3.5 - API 级别 10)并且它在模拟的 Nexus S(上面有 KitKat 的旧图像)上不起作用。两者都显示 "web page not found"。当我使用浏览器在我的 XPRT 上进一步尝试它时,手动输入 url,我得到 "A secure connection could not be established" 的对话框。不幸的是,删除 https 中的 's' 也不起作用,因为它重定向到安全版本,我仍然得到 "A secure connection could not be established".

先前的回答:

如果您完全遵循 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>

同样对于阅读本文的任何其他人,我没有在清单中提及所需的 INTERNET 权限,因为最初提出问题的人已经涵盖了该部分。

如果您在 Android Safari 浏览器中遇到页面未找到错误,那么您可以在 chrome 浏览器中打开 link,许多设备都拥有该浏览器或为用户提供选项 select 浏览器应用程序将在 selected 浏览器中打开 link。

要打开 link 到 google chrome 浏览器,您必须检查它是否安装在设备中。为此,您可以使用此方法检查是否安装了 chrome 浏览器。

private boolean isPackageInstalled(String packagename, Context context) {
     PackageManager pm = context.getPackageManager();
     try {
         pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
         return true;
     } catch (PackageManager.NameNotFoundException e) {
         return false;
     }
} 

chrome 浏览器的包名称是 com.android.chrome。您必须使用 browserIntent.setPackage("com.android.chrome");

将其设置为包名称

为了向用户显示 select 应用程序的选项,您可以像这样创建:

Intent chooserIntent = Intent.createChooser(browserIntent, "Select Application");
startActivity(chooserIntent);

这是您的最终代码:

Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                  Uri.parse("https://www.bluehost.com/track/businessplan/"));
boolean isChromeInstalled = isPackageInstalled("com.android.chrome", YourActivityName.this);
if (isChromeInstalled) {
    browserIntent.setPackage("com.android.chrome");
    startActivity(browserIntent);
}else{
    Intent chooserIntent = Intent.createChooser(browserIntent, "Select Application");
    startActivity(chooserIntent);
}

但请注意,如果您为 select 浏览器应用程序提供选项,那么 Safari 浏览器也会列出。

或者您也可以选择使用 Webview Stephan Branczyk

的回答

希望对您有所帮助。

浏览器存在某些错误。

Support for SPDY and HTTP/2 is limited to certain modern versions of Google Chrome, Opera, Firefox, Safari, and Internet Explorer browsers. For more detailed information, take a look at http://caniuse.com/spdy for what browsers support SPDY and at http://caniuse.com/http2 for what browsers support HTTP/2.

All browsers only support SPDY and HTTP/2 over a TLS connection. Therefore HTTPS has to be used to leverage SPDY or HTTP/2.

当我在 chrome 浏览器中尝试 link 时,它显示“网页不可用”。但是当我在 Opera 浏览器中尝试相同的操作时,它会打开。

On Chrome browser it says " ERR_SPDY_PROTOCOL_ERROR ". while i surfing around the internet it says, it is the bug exist in Chrome browser itself. One of the possible solution is to try the workaround mentioned in this reference link

  1. 尝试将手机中可用的浏览器更新到最新版本。

  2. 实际站点可能正在维护中。

  3. 如果安装了"Avast App in your mobile",尝试添加link

  4. Chrome 中的 SPDY 会话可以通过 URI 检查:chrome://net-internals/#events&q=type:SPDY_SESSION% 20is:活跃。 你可能会在那里找到类似的东西:

    启用SPDY:真

    使用备用协议:true

    始终强制 SPDY:false

    通过 SSL 强制 SPDY:真

    下一个协议:http/1.1,spdy/2,spdy/3

如果您只是将 "SPDY Enabled" 选项设置为 false,问题就会解决,因为之后,Web 通信将使用 HTTP。

  1. 如果您在手机中安装了 avast 应用程序,有时 avast 也可能会导致问题。转到设置 > 主动保护 > 自定义 Web 防护 > 排除项 > 添加 "your link here".

希望它能帮到你.. 在某些情况下!!.抱歉我的英语不好。

来源收集自网络

Use this 

Uri uri = Uri.parse( "https://www.bluehost.com/track/businessplan/" ); startActivity(Intent.createChooser(新意图(Intent.ACTION_VIEW, uri), "Choose browser"));

您的代码在设备浏览器中打开 url 如果重定向不起作用,则问题肯定不在您的代码上。这可能是因为网站中的重定向不起作用或您使用的浏览器无法执行此重定向。在你的情况下,我猜你正在使用的浏览器无法执行重定向。