I have a problem, I want to show the graph in real time of a web page into a webview in my app. I tried the solution proposed How to display a part of the webpage on the webview android , but does not show anything in my app, could you help me to know where I am wrong? This is my code:
NOTE: To enter the webpage and see the graphs
User: javier
Password: 000000
public class MainActivity extends AppCompatActivity {
WebView mWebView;
Handler uiHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView)findViewById(R.id.activity_main_webview);
//Activamos en JavaScript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//Activamos el zoom
webSettings.setBuiltInZoomControls(true);
mWebView.setWebViewClient(new MyWebViewClient());
new BackgroundWorker().execute();
}
// load links in WebView instead of default browser
private class MyWebViewClient extends WebViewClient {
@RequiresApi(21)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return false;
}
}
private class BackgroundWorker extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
getGraphics();
return null;
}
public void getGraphics() {
try {
Document htmlDocument = Jsoup.connect("http://amunet.com.mx/wordpress/index.php/graficas-de-3-tomas/").get();
Element element = htmlDocument.select("div#chartdiv1").first();
// replace body with selected element
htmlDocument.body().empty().append(element.toString());
final String html = htmlDocument.toString();
uiHandler.post(new Runnable() {
@Override
public void run() {
mWebView.loadData(html, "text/html", "UTF-8");
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction()== KeyEvent.ACTION_DOWN){
switch(keyCode){
case KeyEvent.KEYCODE_BACK:
if(mWebView.canGoBack()){
mWebView.goBack();
}
else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}