BobHU BobHU - 2 months ago 22
Android Question

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bobqiu.http_01" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity"
android:hardwareAccelerated="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>




activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.bobqiu.http_01.MainActivity">

<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webview1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="72dp" />
</RelativeLayout>


HttpThread.java


package com.example.bobqiu.http_01;

import android.os.Handler;
import android.webkit.WebView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
* Created by Bobqiu on 2016/9/22.
*/

public class HttpThread extends Thread {
private String url;

private WebView webView;

private Handler handler;

public HttpThread(String url,WebView webView,Handler handler){
this.url = url;
this.webView = webView;
this.handler = handler;
}
public void run(){
try {
URL httpUrl = new URL(url);
try {
HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
connection.setReadTimeout(5000);
connection.setRequestMethod("GET");
final StringBuffer sb = new StringBuffer();
BufferedReader reader = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
String str;
while((str = reader.readLine())!=null){
sb.append(str);
}

handler.post(new Runnable() {
@Override
public void run() {
webView.loadData(sb.toString(),"text/html;
charset=utf-8",null);
}
});
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}


MainActivity.java


package com.example.bobqiu.http_01;

import android.Manifest;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {
private WebView webView;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView= (WebView) this.findViewById(R.id.webview1);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
int permissionCheckContextCompat.
checkSelfPermission(MainActivity.this,
Manifest.permission.INTERNET);
System.out.println("!!!!!"+permissionCheck);//it shows 0 in console
new HttpThread("https://m.baidu.com/",webView,handler).start();
}
}


I have also tried the webView.loadDataWithBaseURL method, still doesn't work...
Please help me, thank you!

Answer

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>