URL with parameter in WebView not working in Android?

前端 未结 8 1403
南笙
南笙 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:18

    change your url to:

    StringBuffer buffer=new StringBuffer("http://stage.realtylog.net/iPhone/functions.php");
    buffer.append("?username="+URLEncoder.encode("xxxxxxx"));
    buffer.append("&id="+URLEncoder.encode("xxxxxxxx"));
    buffer.append("act="+URLEncoder.encode("readFileAndPrint"));
    webView.loadUrl(buffer.toString());
    
    0 讨论(0)
  • 2020-12-20 15:18

    Yoy can use a List of NameValuePair and URLEncodedUtils to create the url string..

    protected String addLocationToUrl(String url){
        if(!url.endsWith("?"))
            url += "?";
    
        List<NameValuePair> params = new LinkedList<NameValuePair>();
    
        if (lat != 0.0 && lon != 0.0){
            params.add(new BasicNameValuePair("lat", String.valueOf(lat)));
            params.add(new BasicNameValuePair("lon", String.valueOf(lon)));
        }
    
        if (address != null && address.getPostalCode() != null)
            params.add(new BasicNameValuePair("postalCode", address.getPostalCode()));
        if (address != null && address.getCountryCode() != null)
            params.add(new BasicNameValuePair("country",address.getCountryCode()));
    
        params.add(new BasicNameValuePair("user", agent.uniqueId));
    
        String paramString = URLEncodedUtils.format(params, "utf-8");
    
        url += paramString;
        return url;
    }
    

    Alternate option is like following...you can try this also...

    HttpPost postMethod = new HttpPost("your url");
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    
    nameValuePairs.add(new BasicNameValuePair("your parameter","parameter value"));
    nameValuePairs.add(new BasicNameValuePair("your parameter","parameter value"));
    
    postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    DefaultHttpClient hc = new DefaultHttpClient();
    
    HttpResponse response = hc.execute(postMethod);
    
    0 讨论(0)
  • 2020-12-20 15:19

    This fixes the issue:

    webView.getSettings().setDomStorageEnabled(true);
    
    0 讨论(0)
  • 2020-12-20 15:20

    I managed to pass variables in a different way.

    My problem was that anytime I switched to another app, when coming to the webapp, the webview kept reloading. I guess that's because of the following line in my onCreate() method: myWebView.loadUrl(url); I had the idea to pass these state variables in the url, but as you know it is not possible yet. What I did was to save the state of some variables using onSaveInstanceState(Bundle outState) {...} and restore them with onRestoreInstanceState(Bundle savedInstanceState){...}.

    In onCreate method after setting up myWebView I did the following:

    myWebView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String urlString)
    {
         Log.i("onPageFinished", "loadVariables("+newURL+")");
         if(newURL!="")
             myWebView.loadUrl("javascript:loadVariables("+"\""+newURL+"\")");
    }
    
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
    });
    
    jsInterface = new JSInterface(this,myWebView);
    myWebView.addJavascriptInterface(jsInterface, "Android");
    
    if (savedInstanceState != null)
    {
    // retrieve saved variables and build a new URL
    newURL = "www.yoururl.com";
    newURL +="?var1=" + savedInstanceState.getInt("key1");
    newURL +="?var2=" + savedInstanceState.getInt("key2");
    Log.i("myWebApp","NEW URL = " + newURL);
    }
    myWebView.loadUrl("www.yoururl.com");
    

    So, what it happens is that first I load the page and then I pass the variables when the page finished to load. In javascript loadVariables function looks like this:

    function loadVariables(urlString){
        // if it is not the default URL
        if(urlString!="www.yoururl.com")
        {
            console.log("loadVariables: " + urlString);
            // parse the URL using a javascript url parser (here I use purl.js)
            var source = $.url(urlString).attr('source');
            var query = $.url(urlString).attr('query');  
            console.log("URL SOURCE = "+source + " URL QUERY = "+query);
            //do something with the variables 
        }
    }
    
    0 讨论(0)
  • 2020-12-20 15:26

    change your url to:

    webView.loadUrl(MessageFormat.format("{0}{1}{2}","http://stage.realtylog.net/iPhone/functions.php",URLEncoder.encode("?username=xxxx"),URLEncoder.encode("&ID=xxxx"),URLEncoder.encode("&act=readFileAndPrint")));
    

    js file like this:

    data= JSON.parse(decodeURIComponent(data));
    
    0 讨论(0)
  • 2020-12-20 15:30

    I searched a lot to solve this issue but found nothing working. Though I only encountered this problem with android < 4.0

    I only encountered this problem when first loading the url. If the webview has already loaded some other url it works fine.

    So this is my workaround that works although it is really silly.

            if (API_ICS_OR_LATER) {
    
                mWebView.loadUrl(mURL);
    
    
            } else {
                /***
                 * Workaround for Webview bug when url contains parameters
                 * https://code.google.com/p/android/issues/detail?id=17535
                 */
    
                mWebView.loadData("<html></html>", "text/html", "UTF-8");
    
                new Handler().postDelayed(new Runnable() {
    
                    @Override
                    public void run() {
                        mWebView.loadUrl(mURL);
    
                    }
                }, 500);
            }
    
    0 讨论(0)
提交回复
热议问题