How I get the web page's source from WebView?
I want to only enter www.google.com in my webview and When I entered this site, I want to get the source for example
String a=........;(source)
I am not sure how far this is going to be helpful. But I have used the below snippet to fetch a small html page's data. I hope it helps you.
Create a class like the one below,
class MyJavaScriptInterface
{
#SuppressWarnings("unused")
public void processHTML(final String html)
{
Log.i("processed html",html);
Thread OauthFetcher=new Thread(new Runnable() {
#Override
public void run() {
String oAuthDetails=null;
oAuthDetails=Html.fromHtml(html).toString();
Log.i("oAuthDetails",oAuthDetails);
}
});OauthFetcher.start();
}
}
Now in your onCreate(),
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
webview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, final String url) {
String oAuthUrl=getString("www.google.com");
if(url.contains(oAuthUrl))
{
Log.i("Contains","Auth URL");
twitter_webview.loadUrl("javascript:window.HTMLOUT.processHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressDialog.show();
}
});
And now what happens is that, when your page finishes loading, the JavaScript class will be called, which would retrieve the page source and store it in a String as your requirement.
And For API 17
import android.webkit.JavascriptInterface;
public class MainActivity extends Activity {
final Context myApp = this;
#JavascriptInterface
public void processHTML(String html) {
if (html == null)
return;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView browser = (WebView) findViewById(R.id.webview);
browser.getSettings().setJavaScriptEnabled(true);
browser.addJavascriptInterface(this, "HTMLOUT");
browser.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
browser.loadUrl("javascript:window.HTMLOUT.processHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
}
});
browser.loadUrl("http://www.google.co.il");
}
}
If your minSdkVersion is 19 or greater you can use the following.
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
view?.evaluateJavascript("""(function() {
return "<html>" + document.getElementsByTagName('html')[0].innerHTML + "</html>";
})()""".trimMargin()) {
console.log(it)
}
}
Related
I am developing an android app in which I have to load a PDF in a webview by appending http://drive.google.com/viewerng/viewer?embedded=true&url= before the actual URL. But after loading it is not previewing the document and saying that "No Preview Available". But the same document is loaded in iOS webview and the Desktop chrome. At the same time, other PDF files from internet are loading properly. I dont know whether it is the issue with the file or my code. Here is my code
`
public class WebViewActivity extends AppCompatActivity {
private WebView webView;
private String postUrl;
private String doc_type;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
this.webView = (WebView) findViewById(R.id.webView);
this.webView.getSettings().setJavaScriptEnabled(true);
this.webView.setHorizontalScrollBarEnabled(false);
this.webView.getSettings().setAllowFileAccess(true);
doc_type = getIntent().getStringExtra("doc_type");
String url = getIntent().getStringExtra("click_action_url");
if (doc_type != null && doc_type.equalsIgnoreCase("others")) {
postUrl = url;
} else{
postUrl = "http://drive.google.com/viewerng/viewer?embedded=true&url=" + url;
}
this.webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
}
});
this.webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
this.webView.loadUrl(postUrl);
}
}`
Someone please help.
I had the same issue, try encoding the url before loading it. I had answered here https://stackoverflow.com/a/67245546/1843984
I am getting message 'Server hangup' when loading url into webView and this message is not implemented in either Android side or server side. If anyone knows how to solve this issue, Please help. Thanks
I have used following code:
private WebView mWebview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new MyWebChromeClient(this));
mWebview .loadUrl("our server url");
setContentView(mWebview);
}
private class MyWebChromeClient extends WebChromeClient {
Context context;
public MyWebChromeClient(Context context) {
super();
this.context = context;
}
}
Please check screenshot
Google.com is secure domain. You have to use https://www.google.com instead of http://www.google.com.
Please use this method and set your website url :
Example :startWebView("https://stackoverflow.com");
private void startWebView(String url) {
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setLoadWithOverviewMode(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getApplicationContext(), "Error:" + description, Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl(url);
}
Webview shows you the HTML returned by the URL that has been loaded.
onReceivedError() will not be called if you get response containing the error message.
Check what you receive as HTML from server using the following code in onPageFinished().
webView.evaluateJavascript(
"(function() { return ('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>'); })();",
new ValueCallback<String>() {
#Override
public void onReceiveValue(String html) {
// displays the HTML received after the URL is loaded.
Log.e("#Eval", "Html -> " + html);
}
});
As you have mentioned that the message is not implemented in your app, it must have been be received from the server end.
I have a class which extends WebViewFragment and what I want to achieve is to be able to load other URL from another class into this WebViewFragment class. Here is how my code looks like:
public class FragWeber extends WebViewFragment {
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
WebView webView = getWebView();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.google.com");
}
}
Any idea how to access the WebView to load other URL?
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String url1 = url; // can add your URL address
view.loadUrl(url);
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
}
});
I am trying to load 2 URL's in a webview, one after the other, and grab their html. My code looks as following:
final WebView webview = (WebView) findViewById(R.id.showInfo_webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webview.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
});
webview.loadUrl(response);
webview.loadUrl(response2); <--
class MyJavaScriptInterface {
#JavascriptInterface
public void showHTML(String html) {
...
}
}
In showHTML I'd have a counter so I can distinguish between the first and the second call. However, showHTML gets called only once. Why is that? Do I need to recreate the webview entirely?
I tried calling webview.reload() after the 2nd loadUrl but then it looks like the 2nd page is loaded twice?
EDIT: I need to process the 1st URL html to obtain the 2nd URL.
String[] urls = {"http://www.demo.com", "http://www.fb.com"};
int delay = 50 * 1000;
Handler handler = new Handler();
for(int i = 0; i < urls.length; i++) {
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
MyRunnable runnable = new MyRunnable(urls[i], webview);
handler.postDelayed(runnable, delay);
delay = delay + 50 * 1000;
}
private class MyRunnable implements Runnable {
private String url;
private WebView wv;
public MyRunnable(String url, WebView wv) {
this.url = url;
this.wv = wv;
}
public void run() {
webview.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webview.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
});
}
}
I worked around it by creating an activity which basically only has the webview. I startActivityForResult twice in a row and that's it. It's certainly not elegant but it works.
I utilize the facebook html like for my app to open a webview like this https://developers.facebook.com/docs/plugins/like-button/
The webview shows a Like and Share button, but after I login to facebook, it doesnt return to the Like and Share button, but a blank page, the share button works fine.
So how do I return to the facebook like url after logging in?
public class LikeFacebookActivity extends BaseActivity {
private WebView webView;
private final String URL = "facebookIDhere";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.like_facebook_webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
showLoading();
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(URL);
ActionBar actionbar = getActionBar();
actionbar.setCustomView(R.layout.actionbar_top_like_facebook);
actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
Button backButton = (Button) findViewById(R.id.buttonGeneralBack);
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
#Override
public void onBackPressed() {
finish();
overridePendingTransition(R.anim.animation_slide_from_left,
R.anim.animation_slide_to_right);
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("something")) return true;
return false;
}
public void onPageFinished(WebView view, String url) {
hideLoading();
}
}
}
I have solved this, just use system.out.println to see which page does facebook load after loggin in
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("something")) return true;
return false; //Default is to not override unless our condition is met.
}
public void onPageFinished(WebView view, String url) {
hideLoading();
//String webUrl = webView.getUrl();
//System.out.println(webUrl);
if(url.startsWith("https://www.facebook.com/plugins/close_popup.php#_=_")){
String redirectUrl = URL;
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
}
}