This is my code:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
WebView webview = (WebView) findViewById(R.id.webView);
/*webview.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.i("WEB_VIEW_TEST", "error code:" + errorCode);
super.onReceivedError(view, errorCode, description, failingUrl);
}
});*/
webview.loadUrl("www.google.com");
}
...
...
...
I set the Internet permission on the manifest.
If I uncomment the setWebViewClient the result it's the same.
How I can solve my problem?
WebView does not assume you want to use HTTP by default. When parsing URLs with WebView, you need to always identify the protocol you wish to use to retrieve the resource.
So instead of just specifying www.google.com load the website with:
webview.loadUrl("http://www.google.com");
Related
I want to be able to recognize when an error occurs and tell show a toast message with the error but I can never get them to call.
I also want to be able to tell when the webview starts navigating to a url and detect which url it's going to.
Unfortunately it just seems to ignore everything and I have no idea how to start a function or method or anything once this occurs.I don't really care how I go about determining when a page starts loading or an error occurs I just need a way that works.
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient()
{
private boolean error;
public void onPageStarted(WebView view, String url, Bitmap favicon) {
onPageStarted(view, url, favicon);
Toast.makeText(getApplicationContext(), "page started:" + url, Toast.LENGTH_SHORT).show();
error = false;
}
public void onReceivedError( WebView view, int errorCode, String description, String failingUrl) {
error = true;
System.out.println("description error" + description);
view.setVisibility( View.GONE );
Toast.makeText(getApplicationContext(), "error:" + description, Toast.LENGTH_SHORT).show();
}
public void onPageFinished(WebView view, String url) {
if (!error) {
view.setVisibility( View.VISIBLE );
}
error = false;
Toast.makeText(getApplicationContext(), "page finished" + url, Toast.LENGTH_SHORT).show();
}
}
);
myWebView.setBackgroundColor(0);
WebSettings webSettings = myWebView.getSettings();
webSettings.setDefaultTextEncodingName("utf-8");
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setAllowFileAccess(true);
// Other webview settings
myWebView.addJavascriptInterface(new MyJavascriptInterface(this), "Android");
myWebView.loadUrl("https://example.com/");
}
}
The onPageStarted(), onPageFinished(), and onReceivedError() methods are not defined in the WebChromeClient class. They're members of WebViewClient. You subclassed the wrong one.
If you add the #Override annotation above each method, your IDE should yell at you that those methods don't override anything in the super class, which is why that annotation is useful.
Simply move those overrides to the WebViewClient instance.
myWebView.setWebViewClient(new WebViewClient() {
private boolean error;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
...
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
...
}
#Override
public void onPageFinished(WebView view, String url) {
...
}
}
);
myWebView.setWebChromeClient(new WebChromeClient());
I have a WebView trying to load a WebPage like this:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
wv=(WebView)findViewById(R.id.webview);
WebSettings ws=wv.getSettings();
ws.setJavaScriptEnabled(true);
wv.loadUrl("http://pro39.blutechnologies.com/crimes.aspx");
}
I have added the internet permission to the manifest
<uses-permission android:name="android.permission.INTERNET"/>
However the WebView is blank,it neither throws an error nor loads the web-page.How do I load webpages like this,I have only tried loading local html files before and I would like to know if I have to do something different.
Try like this way:
mWebView.loadUrl("http://pro39.blutechnologies.com/crimes.aspx");
mWebView.setWebViewClient(new HelloWebViewClient());
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
Use this code .shouldOverrideUrlLoading() not use compulsary.use this code and please reply me this code work or not
Try below code.
public class Main extends Activity {
private WebView mWebview ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
mWebview = (WebView)findViewById(R.id.webview);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://pro39.blutechnologies.com/crimes.aspx");
setContentView(mWebview );
}
}
I'm able load the URL in my WebView. But if I perform some action (like click event), I'm getting following error in LogCat:
Unable to get value of the property 't': object is null or undefinedundefined
But if I perform some events from the emulator's browser, it is working fine.
Here is the code, based on the post:
public class MainActivity extends Activity {
WebView mWebview = null;
String URL = "********"; // Masked intentionally.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main); //Not required as per comment given by Raghunandan.
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebChromeClient(new WebChromeClient());
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl(URL);
setContentView(mWebview );
}
}
Note: Web page is developed using AngularJS.
I've added the below property in Manifiest.xml:
<uses-permission android:name="android.permission.INTERNET" />
I am trying to open URL in webview of my application, but I am getting "Page not found" error.
The same URL is not opening in my device(Android Tablet) browser also.
If I try to open this URL in my desktop browser, Its opening.
Please let me know how to solve this issue. Below is my code.
public class LinkCompanyPageWebView extends Activity{
private String url;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.my_webview);
WebView wvbrowser;
wvbrowser=(WebView)findViewById(R.id.wvbrowser);
wvbrowser.getSettings().setJavaScriptEnabled(true);
wvbrowser.getSettings().setPluginState(WebSettings.PluginState.ON);
url = getIntent().getStringExtra("URL");
wvbrowser.loadUrl(url);
wvbrowser.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.i("WEB_VIEW_TEST", "error code:" + errorCode);
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
}
}
"onReceivedError" not displaying any error.
p.s: Added Internet permissions to my app.
I am writing an app containing the WebView. and I want to put in some functionality to interecpt the "Page Not Found" message in case the content I am trying to show in my app ever is offline or for some other reason unreachable.
I tried using the onReceivedError() but to no avail. Unless perhaps my syntax is wrong, but if soI don't see the error. can someone help?
code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.somesite.net");
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.i("WEB_VIEW_TEST", "error code:" + errorCode);
super.onReceivedError(view, errorCode, description, failingUrl);
//TO DO - do something else in here if the site is down
}
});
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
The WebViewClient should be set before you load the url.
Just came across this. onReceivedError() works fine. You will receive a -2 when the error happens.