WebViewでOS3以降、パラメータつけられない問題の解決策(URL mechanism is broken on android OS 3.0 higher)

Link:WebView – URL mechanism is broken – passing parameters does not work

 

It’s happen on android 3.0 and higher,on OS2.x is no probrem.

we made webview android apps(its not phonegap),but its cannot work on 3.0,at first I think its not so big problem becouse almost people dont use android OS 3.x. usualy,nobody bought tablet.

but..Several months after, its become big problem.its not work on OS4.0 too.

I must think the way to clear it.

 

this problem looks webview manage url parameter as url string.too foolish.

so I try to check loading  url,I got on WebViewClient’s onPageStarted.

I redirect to url(parameter deleted),Its work.but cannot load parameter,ofcouse I dont pass it.

the next , I pass the parameters by JavascriptInterface.Its call from javascript.

At the last , manage the back button.

Its work on OS 3.x and 4.0,checked by optimus pad and galaxy nexus.

 

要はパラメータをURLとして扱えないんならとりあえず抜いてHTML読み込んで、あとでJSでもらいにいけばいいんちゃうって作戦。

 

//Java

	private Boolean Pflag = false;
	private Boolean BackMode = false;
	private Boolean FirstMode = true;
	private ArrayList HistoryStock = new ArrayList();
	private String lastUrl = null;
	private String tmpUrl = "";
	private JSONObject paramJSON;

		//load first
		String starturlJ = "file:///android_asset/index.html";
		lastUrl = starturlJ;
		LL.addView(webview);
		webview.loadUrl(starturlJ);

	public class ViewClient extends WebViewClient {
		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
		}
		@Override
		public void onPageFinished(WebView view, String url) {
		}
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			if (!tmpUrl.equals(url)) {
				if (BackMode) {
					BackMode = false;
					if (HistoryStock.size() > 1) {
						HistoryStock.remove(HistoryStock.size() - 1);
					}
					Pflag = true;
					lastUrl = url;
				} else {
					if (url.indexOf("?") > 0) {
						setJson(url.substring(url.indexOf("?") + 1));
						stockit(url);
						String url2 = url.substring(0, url.indexOf("?"));
						webview.loadUrl(url2);
						Pflag = true;
					} else {
						if (Pflag) {

						} else {
							if (FirstMode) {
								FirstMode = false;
							} else {
								stockit(url);
							}
						}
						Pflag = false;
					}
				}
				adView.loadAd(new AdRequest());
				tmpUrl = url;
			}
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (HistoryStock.size() > 0) {
				BackMode = true;
				String backurl = HistoryStock.get(HistoryStock.size() - 1);
				if (backurl.indexOf("?") > 0) {
					setJson(backurl.substring(backurl.indexOf("?") + 1));
					String url2 = backurl.substring(0, backurl.indexOf("?"));
					webview.loadUrl(url2);
				}else{
					webview.loadUrl(backurl);
				}
				if (HistoryStock.size() == 1) {
					HistoryStock = new ArrayList();
				}
				return true;
			} else {
				this.finish();
				return true;
			}
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	private void stockit(String url) {
		HistoryStock.add(lastUrl);
		lastUrl = url;
	}

	public class JsObj {

		public JsObj(Context con) {
		}

		public String LoadParameter() {
			return paramJSON.toString();
		}

	}

 

 

//Javascript getting parameter.

function getRequest(){
	if(location.search.length > 1) {
		var get = new Object();
		var ret = location.search.substr(1).split("&");
		for(var i = 0; i < ret.length; i++) {
			var r = ret[i].split("=");
			get[r[0]] = r[1];
		}

		return get;
	} else {
		var get = new Object();
		var tmpget =eval("("+roid.LoadParameter()+")");
		for (var i in tmpget){
			get[i] = String(tmpget[i]);
		}
		return get;
  }
};

 

released app.its works on OS4

petty cash book +

 

 

 


1 件のコメント

  1. kaa

    いまさらだけどonPageStartedでやるよりshouldOverrideUrlLoadingでやったほうがいいよ、と残しとく

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です