Getting this error when using webview net::ERR_CONTENT_DECODING_FAILED - java

Using webview on java.
The request I am sending is throwing an error.
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());
WebView.setWebContentsDebuggingEnabled(true);
mywebView.loadUrl("https://lighthearted-platypus-a4a153.netlify.app/");
WebSettings webSettings=mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
public class mywebClient extends 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 onBackPressed(){
if(mywebView.canGoBack()) {
mywebView.goBack();
}
else{
super.onBackPressed();
}
}
}
Need to send get request successfully on the api.

Related

Android webview doesn't open api.whatsapp

I'm trying to open an api.whatsapp link from my webpage using the Android web view but I faced the error ERR_UNKNOWN_URL_SCHEME.
I have changed the shouldOverrideUrlLoading method several times trying to fix it but the intent never works. Have someone faced the same problem before?
This is the current version of my code:
public class MainActivity extends AppCompatActivity {
// public static final String TAG = MainActivity.class.getSimpleName();
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mywebView.getSettings();
mywebView.loadUrl("https://mypage");
webSettings.setJavaScriptEnabled(true);
mywebView.setWebViewClient(new WebViewClient());
}
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//String url = request.getUrl().toString();
if (Uri.parse(url).getHost().equals("https://mypage")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Any suggestions?
Add Validating to your shouldOverrideUrlLoading method like:
Patterns.WEB_URL.matcher(url).matches();

Website unavailable when loaded with WebView

I am trying to load a url with webview that loads perfectly fine on mobile using chrome, but the website says "Website temporarily unavailable". I tried changing the user agent to no avail. I can't figure out for the life of me why this is happening. The url in question is https://www.manototv.com/news
A screenshot of loading with webview
A screenshot of loading with chrome
Relevant portion of the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
mWebView = findViewById(R.id.webView);
WebSettings webSettings = mWebView.getSettings();
mWebView.setWebViewClient(new MyWeb());
mWebView.setWebChromeClient(new MyChrome());
webSettings.setJavaScriptEnabled(true);
webSettings.setMediaPlaybackRequiresUserGesture(false);
mWebView.loadUrl(path)
}
class MyWeb extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
setTitle(view.getTitle());
mWebView.loadUrl(SCRIPT_FULLSCREEN);
mWebView.loadUrl(SCRIPT_CLICK);
mWebView.loadUrl(SCRIPT_PLAY);
mWebView.setVisibility(View.VISIBLE);
}
}
private class MyChrome extends WebChromeClient {
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
private int mOriginalOrientation;
private int mOriginalSystemUiVisibility;
#Override
public Bitmap getDefaultVideoPoster() {
if (mCustomView == null) {
return null;
}
return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
}
#Override
public void onHideCustomView() {
((FrameLayout) getWindow().getDecorView()).removeView(this.mCustomView);
this.mCustomView = null;
getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
setRequestedOrientation(this.mOriginalOrientation);
this.mCustomViewCallback.onCustomViewHidden();
this.mCustomViewCallback = null;
}
#Override
public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback) {
if (this.mCustomView != null) {
onHideCustomView();
return;
}
this.mCustomView = paramView;
this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
this.mOriginalOrientation = getRequestedOrientation();
this.mCustomViewCallback = paramCustomViewCallback;
((FrameLayout) getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
getWindow().getDecorView().setSystemUiVisibility(3846);
}
}

Webview loading url but opening content in default browser?

It opens my link in Webview but when I try to open hyperlinks and content of the web or navigation link, it starts opening with the default browser.
public class MainActivity extends Activity {
private WebView mWebView;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = findViewById(R.id.activity_main_webview);
mWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://");
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Objects.requireNonNull(Uri.parse(url).getHost()).endsWith("http://www.ieltsmadeeasy.tk/")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
I want to browse the whole website inside the web view, not any browser please help me
Here is some documentation about WebViews: https://developer.android.com/guide/webapps/webview#java
The problem is, that you don't override the shouldOverrideUrlLoading(view, url) function of the WebViewClient, as you use the default one.
If you use this as the WebViewClient, it should work (copied from examples in documentation):
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("https://www.example.com")) {
// This is my website, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
So, just take this class and change the line mWebView.setWebViewClient(new WebViewClient()); to mWebView.setWebViewClient(new MyWebViewClient());

How do you disable links, pages and references in a WebView?

I have a Web View in Android,and I open a html web page in it.But it's full of links and images,and when I click one of them,it loads in my webview. So I want to disable this behavior,so if I click on a link,don't load it. I've tried this solution and edited a bit for myself, but not worked.
Please give me a solution with full code. I'm new to Java and Android.
My Full code here:
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
WebView webView;
String url="http://example.com";
TextView textView;
//to hide progressbar after loading part 1
LinearLayout liProgressContainer;
private String currentUrl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
webView = (WebView) findViewById(R.id.webView);
textView = (TextView) findViewById(R.id.tvLoadingPercentage);
//to hide progressbar after loading part 2
liProgressContainer = (LinearLayout) findViewById(R.id.liProgressContainer);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
textView.setText(progress + " %");
}
});
webView.setWebViewClient(new MyWebViewClient());
WebSettings browserSetting = webView.getSettings();
browserSetting.setJavaScriptEnabled(true);
webView.loadUrl(url);
}
//back button function
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
liProgressContainer.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
//return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
liProgressContainer.setVisibility(View.GONE);
//hide header part
}
}
}
You need to configure your shouldOverrideUrlLoading method in your MyWebViewClient class to only load a URL should it match your desired URL. Add a field to your MyWebViewClient class to store a URL and add a single-arg constructor.
private String desiredUrl;
public MyWebViewClient(String url) {
this.desiredUrl = url;
}
In your shouldOverrideUrlLoading method, check to see if the supplied URL is equal to your URL instance variable.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (desiredUrl.equals(url))
view.loadUrl(url);
return true;
}
When setting your web view client, pass your URL argument, e.g.
webView.setWebViewClient(new MyWebViewClient(url));

Starting intent in an extra WebView class instead in an inner class?

I wanted to keep my MainActivity small and clear. So I have created an seperate class for WebView instead of an inner class this way:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
mWebView.setWebViewClient(new CustomWebViewClient());
But now I wanted to start a new activity in CustomWebViewClient with the openInAppBrowser method:
public class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith(PROJECT_REMOTE)) {
return false;
} else {
openInAppBrowser(this, url);
}
return true;
}
private void openInAppBrowser(Activity activity, String url) {
Intent intent = new Intent(activity, InAppBrowser.class);
intent.putExtra("url", url);
activity.startActivity(intent);
}
Since this and MainActivity.this won't work here, how could I reference the MainActivity to make this method work? Or is the inner class inside the MainActivity the better or only option?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
mWebView.setWebViewClient(new CustomWebViewClient(this));
public class CustomWebViewClient extends WebViewClient {
Activity activity;
public CustomWebViewClient (Activity activity) {
this.activity= activity;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith(PROJECT_REMOTE)) {
return false;
} else {
openInAppBrowser(url);
}
return true;
}
private void openInAppBrowser(String url) {
Intent intent = new Intent(activity, InAppBrowser.class);
intent.putExtra("url", url);
activity.startActivity(intent);
}
As requested. I am not sure this will work, that's why I did not put on answer in the first place.

Categories

Resources