I am trying to create a webview app where I want to access a website. The website works fine on chrome but when I run the webview app...it is not loading javascript files/function in the log. I get 'Uncaught error, function not defined...with the js file name mentioned in the Android Studio.
Here is my Code
package com.menux.sss2009;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.os.Bundle;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
CookieManager.getInstance().setAcceptCookie(true);
WebSettings webSettings = webView.getSettings();
webSettings.setAllowContentAccess(true );
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setAppCacheEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDatabaseEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setAppCachePath("/data/data/"+ getPackageName() +"/cache");
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(true);
// webSettings.setJavaScriptEnabled(true);
// webView.getSettings().setPluginState(WebSettings.PluginState.ON);
// webView.getSettings().setAllowFileAccess(true);
// webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
// MyWebChromeClient myWebChromeClient = new MyWebChromeClient();
// webView.setWebChromeClient(myWebChromeClient);
// WebSettings ebSettings = webView.getSettings();
// webSettings.setJavaScriptEnabled(true);
// webSettings.setDomStorageEnabled(true);
// webSettings.setLoadWithOverviewMode(true);
// webSettings.setUseWideViewPort(true);
// webSettings.setBuiltInZoomControls(true);
// webSettings.setDisplayZoomControls(false);
// webSettings.setSupportZoom(true);
webSettings.setDefaultTextEncodingName("utf-8");
// Set cache size to 8 mb by default. should be more than enough
webView.getSettings().setAppCacheMaxSize(1024*1024*64);
// This next one is crazy. It's the DEFAULT location for your app's cache
// But it didn't work for me without this line
// webView.getSettings().setAppCachePath("/data/data/"+ getPackageName() +"/cache");
// webView.getSettings().setAllowFileAccess(true);
// webView.getSettings().setAppCacheEnabled(true);
// webView.getSettings().setDomStorageEnabled(true);
// webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
//
final AppCompatActivity activity = this;
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 100);
}
});
webView.setWebViewClient(new WebViewClient());
/* webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});*/
webView.loadUrl("http://psh2009.net/");
// webView.loadDataWithBaseURL("http://www.psh2009.net/",null,"text/html","UTF-8","http://www.psh2009.net/");
// webView.loadDataWithBaseURL("http://www.psh2009.net/",null,"text/html","UTF-8",null);
}
/*public void onBackPressed() {
if(webView.canGoBack())
{
webView.goBack();
}
else{
super.onBackPressed();
}
}*/
}
I have searched stackoverflow for answer but none of them resolved my issue. Any help is appreciated.
I have searched stackoverflow for answer but none of them resolved my issue.
This is the logcat after the login page...when the main page loads.
07-25 07:22:49.294 2123-2123/? I/chromium: [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
07-25 07:22:49.314 2123-2123/? I/chromium: [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
07-25 07:22:50.394 2123-2123/com.menux.psh2009 I/chromium: [INFO:CONSOLE(2)] "Uncaught SyntaxError: Unexpected token in", source: http://117.254.84.212:3000/getjs?nadipdata=%22%7B%22url%22:%22%2Fjs%2Fplugins%2Ficheck%2Ficheck.min.js%22%2C%22referer%22:%22http:%2F%2Fpsh2009.net%2F%22%2C%22host%22:%22psh2009.net%22%2C%22categories%22:%5B0%5D%2C%22reputations%22:%5B1%5D%2C%22nadipdomain%22:1%7D%22&screenheight=480&screenwidth=854&tm=1564019570223&lib=true&fingerprint=c2VwLW5vLXJlZGlyZWN0 (2)
07-25 07:23:11.444 2123-2123/com.menux.psh2009 W/EGL_emulation: eglSurfaceAttrib not implemented 3093 12436
07-25 07:23:22.234 2123-2123/com.menux.psh2009 I/chromium: [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
07-25 07:23:25.624 2123-2123/com.menux.psh2009 I/chromium: [INFO:CONSOLE(195)] "Uncaught SyntaxError: Unexpected token =", source: http://psh2009.net/js/contentloading/loadingfunctions.js (195)
07-25 07:23:25.714 2123-2123/com.menux.psh2009 I/chromium: [INFO:CONSOLE(3)] "Uncaught SyntaxError: Unexpected token =", source: http://psh2009.net/js/contentloading/contributionscript.js (3)
07-25 07:23:25.784 2123-2123/com.menux.psh2009 I/chromium: [INFO:CONSOLE(330)] "Uncaught SyntaxError: Unexpected token =", source: http://psh2009.net/js/contentloading/userscripts.js (330)
07-25 07:23:25.834 2123-2123/com.menux.psh2009 I/chromium: [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected token =", source: http://psh2009.net/js/contentloading/loansexpenses.js (1)
07-25 07:23:26.514 2123-2123/com.menux.psh2009 I/chromium: [INFO:CONSOLE(39)] "Uncaught ReferenceError: getchartdata is not defined", source: http://psh2009.net/js/demo_dashboard.js (39)
The above errors are showing up.
I can't figure out the problem with the Nox Player, but I am able to have it worked in Vivo 11, and Vivo 11 Pro which has Funtouch OS 4.5 (based on Android 8.1) also the android emulator. Initially when I installed the app the webview showed the error 'net:ERR_CLEARTEXT_NOT_PERMITTED'. Then I added this line in manifest file 'android:usesCleartextTraffic="true"' in the 'application' tag and it worked without any issues. all the javascript files/functions are loading properly. Thanks everyone for the inputs.