Genadinik
Genadinik

Reputation: 18649

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

I have this code:

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);
    }
});

Technically, this code works to send the person to that url, but for some reason this particular URL goes to a page not found from this code, but renders if you put this url into a browser. But when done from the app it goes to page not found. I am testing with Android Safari browser.

Is it because of the redirect on the URL? And how could I fix this?

I do have this line in my manifest file:

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

NOTE:

Internet connection is NOT the problem. This code works for other URLs. It is something strange that is happening with this specific link.

Thanks!

Upvotes: 16

Views: 9085

Answers (5)

Stephan Branczyk
Stephan Branczyk

Reputation: 9375

The code in my answer below and the corresponding redirect worked for me.

On my Xperia Z Ultra, it looks like this:

According to the official Android documentation, it should also work on previous api level below 19. I assumed the Chrome documentation mentioned the minimum api level number 19 because it was specifically targeting web developers that wanted to use its HTML5 functionality.

That being said, this solution does not work with my old Sprint XPRT (Android 2.3.5 - API Level 10) and it does not work on an emulated Nexus S (with an old image of KitKat on it). It says "web page not found" on both. And when I further tried it on my XPRT using the browser, but entered the url manually, I get the dialog that "A secure connection could not be established". Unfortunately, removing the 's' in https doesn't work either, because it redirects to the secure version, and I still get "A secure connection could not be established".

Earlier Answer:

If you follow that Chrome WebView tutorial exactly. You'll get to this part. The emphasis in bold is mine.

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.

In other words, if you just stop the tutorial at this point, you'll get the behavior you desire.

Here is the actual code in case the link to that tutorial stops working:

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>

Also for anyone else reading this, I am not mentioning the needed INTERNET permission in the manifest because the original person asking already has that part covered.

Upvotes: 5

MinFu
MinFu

Reputation: 343

your code is open the url in device browser if the redirection is not working, definitely the problem is not on your code. it may because of the redirection in the website is not working or the browser you are using can not perform this redirection. in your case i guess is the browser your are using can not perform the redirection.

Upvotes: 0

Ashish Rawat
Ashish Rawat

Reputation: 5839

Use this 

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

Upvotes: -1

Rajesh Jadav
Rajesh Jadav

Reputation: 12861

If you get Page not found error in Android Safari browser then you can open link in chrome browser which many devices have or give option to user to select browser application which will open link in selected browser.

For opening link into google chrome browser you have to check whether it is installed in device. for that you can use this method to check whether chrome browser is installed or not.

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;
     }
} 

packge name of chrome browser is com.android.chrome. you have to set this as package name using browserIntent.setPackage("com.android.chrome");

For Showing options to user to select application you can create like this:

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

Here is your final code:

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);
}

But note that if you give option to select browser application then Safari browser also listed.

or you can also have option of using Webviewas answered by Stephan Branczyk

I hope this helps you.

Upvotes: 2

Swaminathan V
Swaminathan V

Reputation: 4781

There are certain bugs available with the browsers.

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.

When i try the link in the chrome browser it says, " Web Page not Available ". But when i try the same in opera browser, it opens.

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. Try to update the browsers available in your mobile to the latest version.

  2. The actual site may be down for maintenance.

  3. If you installed "Avast App in your mobile", try to add the link

  4. SPDY sessions in Chrome can be inspected via the URI: chrome://net-internals/#events&q=type:SPDY_SESSION%20is:active. You might find something like this there:

    SPDY Enabled: true

    Use Alternate Protocol: true

    Force SPDY Always: false

    Force SPDY Over SSL: true

    Next Protocols: http/1.1,spdy/2,spdy/3

If you just set "SPDY Enabled" option to false, the problem would be solved as after that, HTTP will be used for the web communication.

  1. Sometimes avast may also causes the problem if you installed avast app in your mobile. Go to Settings > Active Protection > Customize Web Shield > Exclusions > Add "your link here".

Hopefully it may help you.. in some case!!. Sorry for my bad english.

Source collected from Internet.

Upvotes: 0

Related Questions