android webview only show loaded data after second refresh/load -
i trying write android webview code load data jason file url. however, noticed data not shown on page initial time, succussfully shown if click button load/refresh data page again (not sure if because of refresh or reload). checked log file. first time click output following log.
08-17 16:07:38.784: v/webview(17435): singlecursorhandlertouchevent -geteditablesupport fasle 08-17 16:07:39.184: v/webview(17435): doshortpress : mnativeclass - 2089760 mpreventdefault - 2 08-17 16:07:41.777: v/webview(17435): singlecursorhandlertouchevent -geteditablesupport fasle 08-17 16:07:42.187: v/webview(17435): doshortpress : mnativeclass - 2089760 mpreventdefault - 2 08-17 16:07:42.588: d/dalvikvm(17435): gc_concurrent freed 375k, 46% free 3455k/6343k, external 377k/1281k, paused 2ms+2ms
the second time run bring me following text in log, can see loaded string data output log @ last line.
08-17 16:07:51.837: v/webview(17435): singlecursorhandlertouchevent -geteditablesupport fasle 08-17 16:07:52.247: v/webview(17435): doshortpress : mnativeclass - 2089760 mpreventdefault - 2 08-17 16:07:55.901: v/webview(17435): singlecursorhandlertouchevent -geteditablesupport fasle 08-17 16:07:56.301: v/webview(17435): doshortpress : mnativeclass - 2089760 mpreventdefault - 2 08-17 16:07:56.501: d/dalvikvm(17435): gc_for_malloc freed 117k, 46% free 3472k/6343k, external 377k/1281k, paused 15ms 08-17 16:07:56.501: i/dalvikvm-heap(17435): grow heap (frag case) 6.195mb 87396-byte allocation 08-17 16:07:56.521: d/dalvikvm(17435): gc_for_malloc freed 32k, 46% free 3525k/6471k, external 377k/1281k, paused 16ms 08-17 16:07:56.591: i/my call:(17435): <ul data-role='listview'><li>abc<p style='font-size:1 font-color:grey'>2014-08-11</p></li><li>def<p style='font-size:1 font-color:grey'>2014-08-14</p></li><li>fgh<p style='font-size:1 font-color:grey'>2014-08-14</p></li><li>hijk<p style='font-size:1 font-color:grey'>2014-08-15</p></li></ul>
can tell why first time didn't bring data? please help. thanks.
following activity code.
@suppresslint("setjavascriptenabled") public class messageactivity extends activity { webview mwebview; public static string liststr = ""; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_message); mwebview = (webview) findviewbyid(r.id.webview); mwebview.setwebviewclient(new webviewclient() { public boolean shouldoverrideurlloading(webview view, string url) { return false; } }); mwebview.setverticalscrollbarenabled(false); mwebview.getsettings().setjavascriptenabled(true); mwebview.getsettings().setdomstorageenabled(true); mwebview.addjavascriptinterface(new javascriptinterface(), "jsinterface"); mwebview.loadurl("file:///android_asset/index.html"); } public boolean onkeydown(int keycode, keyevent event) { if ((keycode == keyevent.keycode_back) && mwebview.cangoback()) { mwebview.destroy(); finish(); mwebview.destroy(); return true; } else if (keycode == keyevent.keycode_back){ mwebview.destroy(); finish(); mwebview.destroy(); } return super.onkeydown(keycode, event); } private class longrunninggetio extends asynctask <void, void, string> { protected string getasciicontentfromentity(httpentity entity) throws illegalstateexception, ioexception { inputstream in = entity.getcontent(); stringbuffer out = new stringbuffer(); int n = 1; while (n>0) { byte[] b = new byte[4096]; n = in.read(b); if (n>0) out.append(new string(b, 0, n)); } return out.tostring(); } @override protected string doinbackground(void... params) { httpclient httpclient = new defaulthttpclient(); httpcontext localcontext = new basichttpcontext(); httpget httpget = new httpget("http://****/post.json"); string text = null; try { httpresponse response = httpclient.execute(httpget, localcontext); httpentity entity = response.getentity(); text = getasciicontentfromentity(entity); } catch (exception e) { return e.getlocalizedmessage(); } return text; } protected void onpostexecute(string results) { getjsondata(results); } protected void getjsondata (string jsonstr) { jsonarray postarr = new jsonarray(); liststr = "<ul data-role='listview'>"; try { jsonobject posts = (new jsonobject(jsonstr).getjsonobject("posts")); postarr = posts.getjsonarray("post"); (int i=0; i<postarr.length(); i++) { jsonobject p = postarr.getjsonobject(i); liststr += "<li>"; liststr += p.getstring("title"); liststr += "<p style='font-size:1 font-color:grey'>"; liststr += p.getstring("date"); liststr += "</p>"; liststr += "</li>"; } liststr += "</ul>"; } catch (jsonexception e) { e.printstacktrace(); } } } public class javascriptinterface { string str; @javascriptinterface public string getpostvalue() { new longrunninggetio().execute(); this.str = liststr; return str; } } }
my index.html page.
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>loadpagetest</title> <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=open+sans:300,400,700"> <link rel="stylesheet" href="./css/customizedstyle.css"> <link rel="stylesheet" href="./css/themes/default/jquery.mobile-1.4.3.min.css"> <link rel="stylesheet" href="./css/jqm-demos.css"> <script src="./js/jquery.js"></script> <script scr="./js/customizedjs.js"></script> <script src="./js/jquery.mobile-1.4.3.min.js"></script> <script> $( document ).on( "pagecreate", "#demo-page", function() { $( document ).on( "swipeleft swiperight", "#demo-page", function( e ) { if ( $( ".ui-page-active" ).jqmdata( "panel" ) !== "open" ) { if ( e.type === "swipeleft" ) { $( "#right-panel" ).panel( "open" ); } } }); }); </script> <style type="text/css"> body { overflow:hidden; } </style> </head> <body style= "overflow:hidden" scrolling="no"> <style type="text/css"> body { overflow:hidden; } </style> <div data-role="page" id="main-page" style= "overflow:hidden" scrolling="no"> <div role="main" class="ui-content" id ="maindiv" style= "overflow: auto"> load diff pages here. </div><!-- /content --> <div data-role="panel" id="left-panel" data-theme="b"> <ul data-role="listview" data-icon="false" id="menu"> <li> <a href="#" id = "btna" data-rel="close">go page <img src="./images/icona.png" class="ui-li-thumb"/> </li> </ul> </div><!-- /panel --> <script type="text/javascript"> $("#btna").on("click", function(){ var listposts = window.jsinterface.getpostvalue(); document.getelementbyid( "maindiv" ).innerhtml = "<html><body>"+listposts+"</body></html>"; }); </script> </div><!-- /page --> </body> </html>
Comments
Post a Comment