[英]Android Webview does not load the url
我想通過 WebView 加載網站。 當它通過網絡瀏覽器加載時,沒有問題。 但是當我嘗試通過 WebView 加載時,沒有加載。 而是顯示白色空白背景。
這是 MainActivity 類(這里是 Main 類):
package com.xamarin.gcmexample;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import android.telephony.TelephonyManager;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;
import java.util.logging.Logger;
public class Main extends AppCompatActivity {
WebView mWebView;
Logger LOGGER;
private static Main mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
LOGGER = Logger.getLogger(RegistrationIntentService.class.getName());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new MyJavaScriptInterface(this), "Android");
mWebView.setWebViewClient(new MyWebViewClient());
mContext =Main.this;
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String id=telephonyManager.getDeviceId();
int resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(mContext);
if (resultCode == ConnectionResult.SUCCESS){
mWebView.loadUrl("https://pavementworks.corelynx.com?imei="+id);
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
}catch(Exception ex)
{ LOGGER.info(" mWebView:"+ex.getMessage());
return;
}
}
@Override
protected void onResume() {
super.onResume();
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mContext =Main.this;
LOGGER = Logger.getLogger(RegistrationIntentService.class.getName());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new MyJavaScriptInterface(this), "Android");
mWebView.setWebViewClient(new MyWebViewClient());
int resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(mContext);
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String id=telephonyManager.getDeviceId();
if (resultCode == ConnectionResult.SUCCESS){
mWebView.loadUrl("https://pavementworks.corelynx.com?imei="+id);
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
}
}
class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
//You can add some custom functionality here
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//You can add some custom functionality here
}
@Override
public void onReceivedError (WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
//You can add some custom functionality here
}
}
class MyJavaScriptInterface {
Activity activity;
MyJavaScriptInterface(Activity activity) {
this.activity = activity;
}
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(activity, toast, Toast.LENGTH_SHORT).show();
}
@JavascriptInterface
public void closeActivity() {
activity.finish();
}
}
這是我的 Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.xamarin.gcmexample.Main">
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>
我做錯了嗎? 請給出建議。
請記住將權限添加到您的清單文件中。
<Manifest>
...
<uses-permission android:name="android.permission.INTERNET"/>
...
</Manifest>
問題
if (resultCode == ConnectionResult.SUCCESS){
mWebView.loadUrl("https://pavementworks.corelynx.com?imei="+id);
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
在這種情況下, loadUrl
和Intent
觸發。 什么是if (resultCode == ConnectionResult.SUCCESS)
刪除Intent intent
部分。
僅供參考: WebView顯示Your connection is not private
resultCode == ConnectionResult.SUCCESS
正確滿足與否 覆蓋此方法:
@Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { // super.onReceivedSslError(view, handler, error); handler.proceed(); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.