Android - Get Host isn't returning consitently












2















I have my own MyWebViewClient that extends WebViewClient for my webView in my app. Instead of making complicated navigation bars that may not be supported on all models, I have implemented my own USERAGENT string on the webview and using the back end of the website, I show a different link on the page. I have made two links that are supposed to fire events when clicked. Here is the function I am using to accomplish this



    @Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.getUrl().getHost().equals("close.app")) {
clearCookiesAndCache();
return false;
}
if (request.getUrl().getHost().equals("showsettings.app")) {
openSettingsMenu();
return true;
}
if (request.getUrl().getHost().equals(SERVER_URL)) {
return false;
}
final Uri uri = request.getUrl();
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
return true;
}


If the user visits the SERVER_URL string, which is the domain my webView loads, then we load the new page in the webView. If they click OUTSIDE the web address (example.com), such as notexample.com, then we start a new intent to open the external link in chrome or some other browser.



This works GREAT in my android environment inside Android Studio. When I added this to my live testing device however, clicking the links is trying to load them as a page instead of firing the showSettings event or logging them out like it does in my studio environment.



What am I doing wrong here that it's not getting the proper host string on all devices? Just in-case, here is my showSettings function, which again does fire properly in the Studio Environment (Nexus 5, and Google Pixel 2), my test device is a Samsung Galaxy S6 Edge btw



public void openSettingsMenu() {
Log.e(TAG, "They requested the settings menu");
isSettingsOpen = true;
startActivity(new Intent(Browser.this, appSettings.class));
}


Thanks for any help.










share|improve this question



























    2















    I have my own MyWebViewClient that extends WebViewClient for my webView in my app. Instead of making complicated navigation bars that may not be supported on all models, I have implemented my own USERAGENT string on the webview and using the back end of the website, I show a different link on the page. I have made two links that are supposed to fire events when clicked. Here is the function I am using to accomplish this



        @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    if (request.getUrl().getHost().equals("close.app")) {
    clearCookiesAndCache();
    return false;
    }
    if (request.getUrl().getHost().equals("showsettings.app")) {
    openSettingsMenu();
    return true;
    }
    if (request.getUrl().getHost().equals(SERVER_URL)) {
    return false;
    }
    final Uri uri = request.getUrl();
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
    return true;
    }


    If the user visits the SERVER_URL string, which is the domain my webView loads, then we load the new page in the webView. If they click OUTSIDE the web address (example.com), such as notexample.com, then we start a new intent to open the external link in chrome or some other browser.



    This works GREAT in my android environment inside Android Studio. When I added this to my live testing device however, clicking the links is trying to load them as a page instead of firing the showSettings event or logging them out like it does in my studio environment.



    What am I doing wrong here that it's not getting the proper host string on all devices? Just in-case, here is my showSettings function, which again does fire properly in the Studio Environment (Nexus 5, and Google Pixel 2), my test device is a Samsung Galaxy S6 Edge btw



    public void openSettingsMenu() {
    Log.e(TAG, "They requested the settings menu");
    isSettingsOpen = true;
    startActivity(new Intent(Browser.this, appSettings.class));
    }


    Thanks for any help.










    share|improve this question

























      2












      2








      2








      I have my own MyWebViewClient that extends WebViewClient for my webView in my app. Instead of making complicated navigation bars that may not be supported on all models, I have implemented my own USERAGENT string on the webview and using the back end of the website, I show a different link on the page. I have made two links that are supposed to fire events when clicked. Here is the function I am using to accomplish this



          @Override
      public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
      if (request.getUrl().getHost().equals("close.app")) {
      clearCookiesAndCache();
      return false;
      }
      if (request.getUrl().getHost().equals("showsettings.app")) {
      openSettingsMenu();
      return true;
      }
      if (request.getUrl().getHost().equals(SERVER_URL)) {
      return false;
      }
      final Uri uri = request.getUrl();
      Intent intent = new Intent(Intent.ACTION_VIEW, uri);
      startActivity(intent);
      return true;
      }


      If the user visits the SERVER_URL string, which is the domain my webView loads, then we load the new page in the webView. If they click OUTSIDE the web address (example.com), such as notexample.com, then we start a new intent to open the external link in chrome or some other browser.



      This works GREAT in my android environment inside Android Studio. When I added this to my live testing device however, clicking the links is trying to load them as a page instead of firing the showSettings event or logging them out like it does in my studio environment.



      What am I doing wrong here that it's not getting the proper host string on all devices? Just in-case, here is my showSettings function, which again does fire properly in the Studio Environment (Nexus 5, and Google Pixel 2), my test device is a Samsung Galaxy S6 Edge btw



      public void openSettingsMenu() {
      Log.e(TAG, "They requested the settings menu");
      isSettingsOpen = true;
      startActivity(new Intent(Browser.this, appSettings.class));
      }


      Thanks for any help.










      share|improve this question














      I have my own MyWebViewClient that extends WebViewClient for my webView in my app. Instead of making complicated navigation bars that may not be supported on all models, I have implemented my own USERAGENT string on the webview and using the back end of the website, I show a different link on the page. I have made two links that are supposed to fire events when clicked. Here is the function I am using to accomplish this



          @Override
      public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
      if (request.getUrl().getHost().equals("close.app")) {
      clearCookiesAndCache();
      return false;
      }
      if (request.getUrl().getHost().equals("showsettings.app")) {
      openSettingsMenu();
      return true;
      }
      if (request.getUrl().getHost().equals(SERVER_URL)) {
      return false;
      }
      final Uri uri = request.getUrl();
      Intent intent = new Intent(Intent.ACTION_VIEW, uri);
      startActivity(intent);
      return true;
      }


      If the user visits the SERVER_URL string, which is the domain my webView loads, then we load the new page in the webView. If they click OUTSIDE the web address (example.com), such as notexample.com, then we start a new intent to open the external link in chrome or some other browser.



      This works GREAT in my android environment inside Android Studio. When I added this to my live testing device however, clicking the links is trying to load them as a page instead of firing the showSettings event or logging them out like it does in my studio environment.



      What am I doing wrong here that it's not getting the proper host string on all devices? Just in-case, here is my showSettings function, which again does fire properly in the Studio Environment (Nexus 5, and Google Pixel 2), my test device is a Samsung Galaxy S6 Edge btw



      public void openSettingsMenu() {
      Log.e(TAG, "They requested the settings menu");
      isSettingsOpen = true;
      startActivity(new Intent(Browser.this, appSettings.class));
      }


      Thanks for any help.







      java android android-intent android-webview






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 5:25









      KaboomKaboom

      432316




      432316
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I solved the issue. Apparently on older versions you need to override the String Url instead of webresourcerequest. Once I converted to the string version, it started to work. I am unsure which version is deprecated, so I will use this one since it seems to be working now. Here is the updated working code!



             @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
          super.shouldOverrideUrlLoading(view, url);
          if (Uri.parse(url).getHost().equals("close.app")) {
          clearCookiesAndCache();
          return true;
          }
          if (Uri.parse(url).getHost().equals("showsettings.app")) {
          openSettingsMenu();
          return true;
          }
          if (Uri.parse(url).getHost().equals(SERVER_URL)) {
          return false;
          }
          final Uri uri = Uri.parse(url);
          Intent intent = new Intent(Intent.ACTION_VIEW, uri);
          startActivity(intent);
          return true;
          }





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405724%2fandroid-get-host-isnt-returning-consitently%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I solved the issue. Apparently on older versions you need to override the String Url instead of webresourcerequest. Once I converted to the string version, it started to work. I am unsure which version is deprecated, so I will use this one since it seems to be working now. Here is the updated working code!



               @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
            super.shouldOverrideUrlLoading(view, url);
            if (Uri.parse(url).getHost().equals("close.app")) {
            clearCookiesAndCache();
            return true;
            }
            if (Uri.parse(url).getHost().equals("showsettings.app")) {
            openSettingsMenu();
            return true;
            }
            if (Uri.parse(url).getHost().equals(SERVER_URL)) {
            return false;
            }
            final Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
            return true;
            }





            share|improve this answer




























              0














              I solved the issue. Apparently on older versions you need to override the String Url instead of webresourcerequest. Once I converted to the string version, it started to work. I am unsure which version is deprecated, so I will use this one since it seems to be working now. Here is the updated working code!



                 @Override
              public boolean shouldOverrideUrlLoading(WebView view, String url) {
              super.shouldOverrideUrlLoading(view, url);
              if (Uri.parse(url).getHost().equals("close.app")) {
              clearCookiesAndCache();
              return true;
              }
              if (Uri.parse(url).getHost().equals("showsettings.app")) {
              openSettingsMenu();
              return true;
              }
              if (Uri.parse(url).getHost().equals(SERVER_URL)) {
              return false;
              }
              final Uri uri = Uri.parse(url);
              Intent intent = new Intent(Intent.ACTION_VIEW, uri);
              startActivity(intent);
              return true;
              }





              share|improve this answer


























                0












                0








                0







                I solved the issue. Apparently on older versions you need to override the String Url instead of webresourcerequest. Once I converted to the string version, it started to work. I am unsure which version is deprecated, so I will use this one since it seems to be working now. Here is the updated working code!



                   @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                super.shouldOverrideUrlLoading(view, url);
                if (Uri.parse(url).getHost().equals("close.app")) {
                clearCookiesAndCache();
                return true;
                }
                if (Uri.parse(url).getHost().equals("showsettings.app")) {
                openSettingsMenu();
                return true;
                }
                if (Uri.parse(url).getHost().equals(SERVER_URL)) {
                return false;
                }
                final Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
                return true;
                }





                share|improve this answer













                I solved the issue. Apparently on older versions you need to override the String Url instead of webresourcerequest. Once I converted to the string version, it started to work. I am unsure which version is deprecated, so I will use this one since it seems to be working now. Here is the updated working code!



                   @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                super.shouldOverrideUrlLoading(view, url);
                if (Uri.parse(url).getHost().equals("close.app")) {
                clearCookiesAndCache();
                return true;
                }
                if (Uri.parse(url).getHost().equals("showsettings.app")) {
                openSettingsMenu();
                return true;
                }
                if (Uri.parse(url).getHost().equals(SERVER_URL)) {
                return false;
                }
                final Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
                return true;
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 6:46









                KaboomKaboom

                432316




                432316
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405724%2fandroid-get-host-isnt-returning-consitently%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()