Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 893
Genadinik Avatar asked Jan 02 '16 02:01

Genadinik


People also ask

Why is my redirection not working?

First try removing and then re-adding the redirects. Make sure to clear your browser cache when you go back to test. If the problem recurs, then check your . htaccess file to see if something is there that may be interfering with your current redirects.

How do I set up URL redirection?

Redirects allow you to forward the visitors of a specific URL to another page of your website. In Site Tools, you can add redirects by going to Domain > Redirects. Choose the desired domain, fill in the URL you want to redirect to another and add the URL of the new page destination. When ready, click Create.

How do you prevent the WebView from invoking the device's Web browser when a redirection occurs in the WebView?

To detect and intercept any redirection from WebView , we can use shouldOverrideUrlLoading and return true if it is supported to redirect into native page so that WebView stop the URL redirection in the web page and stay in the current page.


2 Answers

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.

like image 98
Stephan Branczyk Avatar answered Oct 11 '22 22:10

Stephan Branczyk


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.

like image 39
Rajesh Avatar answered Oct 11 '22 22:10

Rajesh