URL with a parameter in a WebView that does not work in Android?

I am trying to call the loadUrl method in a webview using the following URL

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

When this url is called, it gives an image from the server. I need to display this image in a webview.

Works fine with regular urls. But it does not work with the above URL with parameters.

This results in an error:

The function name must be a string in /var/www/stage/realtylog.net/iPhone/functions.php

I tried to execute the URLEncode above URL, but it still does not work. Any help would be greatly appreciated.

package com.hussain.webview2; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.webkit.SslErrorHandler; import android.net.http.*; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; public class WebViewDemo2Activity extends Activity { final Activity activity = this; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.getWindow().requestFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.main); WebView webView = (WebView) findViewById(R.id.webview); // webView.setWebViewClient(new WebViewClient()); // webView.addView(webView.getZoomControls()); webView.getSettings().setJavaScriptEnabled(true); webView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { activity.setTitle("Loading..."); activity.setProgress(progress * 100); if(progress == 100) activity.setTitle("Done"); } }); webView.setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { // Handle the error } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); //webView.loadUrl("http://developer.android.com"); webView.loadUrl("http://stage.realtylog.net/iPhone/functions.php?username=xxxx&ID=xxxx&act=readFileAndPrint"); } } 
+6
source share
7 answers

Welcome to the terrible known mistake that Google does not want to correct or even contact.

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

+11
source

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()); 
+3
source

Yoy can use the List of NameValuePair and URLEncodedUtils to create a 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; } 

An alternative is similar to the following ... you can try this as well ...

 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); 
+1
source

I searched a lot to solve this problem, but found nothing. Although I ran into this problem only with android <4.0

I ran into this problem when loading the url the first time. If the webview has already loaded any other URL, it works fine.

So this is my workaround that works, although it is really stupid.

  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); } 
+1
source

I managed to pass the variables differently.

My problem was that at any time, when I switched to another application, when I came to webapp, web browsing kept rebooting. I guess because of the following line in my onCreate() method: myWebView.loadUrl(url); I had the idea of ​​passing these state variables to a URL, but as you know, this is not yet possible. I did to save the state of some variables using onSaveInstanceState(Bundle outState) {...} and restore them using onRestoreInstanceState(Bundle savedInstanceState){...} .

In the 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 happens is that first I load the page and then pass the variables when the page ends for loading. In the javascript function, loadVariables 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
source

As Dino Fancellu noted, its a bad mistake, but I think it only applies to local asset files in your Android project.
Incredibly, this error did not exist in versions 2+ (Froyo).
And my application relied heavily on passing url variables to / asset / files. I passed only one variable (? Id = xx), but here is how I fixed it and configured it to work with several 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 according to this page question, this is not a local asset file; this is http: // url. As far as I understand, WebView does not throw an error. Your PHP script: "The function name must be a string in /var/www/stage/realtylog.net/iPhone/functions.php"
    This may be useful:
    PHP Fatal error: function name must be a string
0
source

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:

 data= JSON.parse(decodeURIComponent(data)); 
0
source

Source: https://habr.com/ru/post/908198/


All Articles