URL with parameter in WebView not working in Android?

前端 未结 8 1397
南笙
南笙 2020-12-20 14:40

I am trying to call the loadUrl method in a webview with the below url

http://stage.realtylog.net/iPhone/functions.php?username=xxx&ID=xxx&act=readFileAndPri

相关标签:
8条回答
  • 2020-12-20 15:40

    As Dino Fancellu pointed out, its a bad bug, but I think it only applies to the asset local files in your android project.
    Incredibly enough, this bug did not exist in versions 2+ (Froyo).
    And my app relied heavily on passing url variables to /asset/ files.
    I was only passing one variable (?id=xx), but heres how i fixed it, and tweaked it to work with multiple variables:

        // in App declare a string to hold the url parameters
        String strUrlParams;
    
        // In WebViewClient, Override the onReceiveError
        // Basially check if params exist in Url, strip them and save them to string
        private class HelloWebViewClient extends WebViewClient {
        @Override
        public void onReceivedError(WebView webview, int errorCode, String description, String failingUrl)
        {
            System.out.println("onReceivedError: " + description);
            if (failingUrl.contains("?")) {
                    String[] temp;
                    temp = failingUrl.split(Pattern.quote("?"));
                    strUrlParams = temp[1]; // save params to string
                    webview.loadUrl(temp[0]); // load page without params
    
            } else {
            webview.loadUrl("file:///android_asset/error.html");
            }
        }
        ..........
        }
    
    
        // In JavascriptInterface, add method to return the params string
        public String getUrlPString() {
        return strUrlParams;
        }
    
    
        // In the webpage that is expecting the params, add javascript to grab them.
        var getUrlParam = function(theParam) {
        var strParam = "nada";
        var query = window.location.search.substring(1);
        if(query.indexOf("=") != -1)  { // check if params
        ....
        } else { 
        // no params in url, grab params string from app
        query = app.getUrlPString();
        }
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if (pair[0] == theParam) {
        strParam = pair[1];
        }
        }
        return strParam;
        }
    
        // example to grab the ?id=xx param 
        var theID = getUrlParam("id");
    
    • But as per this pages question, its not a local asset file; is a http:// url. From what I understand, WebView is not throwing the error. Your php script is: "Function name must be a string in /var/www/stage/realtylog.net/iPhone/functions.php"
      This may be usefull:
      PHP Fatal error: Function name must be a string
    0 讨论(0)
  • 2020-12-20 15:43

    Welcome to a horrible known bug, that Google doesn't feel like fixing, or even addressing.

    http://code.google.com/p/android/issues/detail?id=17535

    0 讨论(0)
提交回复
热议问题