Android6.0 WebView Shows Blank Page
I have tried to load a page with webView, and it just shows an empty page on my cellphone. Here are the codes and classes. AndroidManifest.xml
Solution 1:
Add internet permission in you manifest. Without it your application will not be able to access internet services.
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET" />
<application ...
</manifest>
Edited -
In android M and above you have to ask permissions from the user at runtime. Before calling
new HttpThread("https://m.baidu.com/",webView,handler).start();
run this code for granting permissions -
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.INTERNET)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (shouldShowRequestPermissionRationale(
Manifest.permission.INTERNET)) {
// Explain to the user why we need to read the contacts
}
requestPermissions(new String[]{Manifest.permission.INTERNET},
1);
// MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
// app-defined int constant
return;
}else {
new HttpThread("https://m.baidu.com/",webView,handler).start();
}
}else {
new HttpThread("https://m.baidu.com/",webView,handler).start();
}
And override this method -
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
new HttpThread("https://m.baidu.com/",webView,handler).start();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Snackbar.make(parent, "Click on allow to Access Internet in you application", Snackbar.LENGTH_LONG)
.setAction("CLOSE", new View.OnClickListener() {
@Override
public void onClick(View view) {
}
})
.setActionTextColor(getResources().getColor(android.R.color.holo_purple
)).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
After this it should run in your Android M phone too. Hope it works.
Solution 2:
there is no need to load data in a SubThread...
you can log the stringbuilder info before webView loads it
webView.loadData(String url) works well
---update i try to run your code (23 and 19):
Solution 3:
Try loading data like this:
webView.loadData(sb.toString(),"text/html",UTF-8);
or this:
webView.loadDataWithBaseURL("",sb.toString(),"text/html",UTF-8,"");
Post a Comment for "Android6.0 WebView Shows Blank Page"