Android Webview不会加载网址

时间:2016-03-30 10:38:31

标签: java android android-studio webview android-webview

我想通过WebView加载网站。当它通过Web浏览器加载时,没有问题。但是当我尝试通过WebView加载时,Nothing加载。而是显示白色空白背景。

这是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>

The resulted Image

我做错了吗?请提出建议。

3 个答案:

答案 0 :(得分:1)

请记住在您的清单文件中添加权限。

<Manifest> 

...

<uses-permission android:name="android.permission.INTERNET"/>

...

</Manifest>

答案 1 :(得分:0)

<强>问题

if (resultCode == ConnectionResult.SUCCESS){
        mWebView.loadUrl("https://pavementworks.corelynx.com?imei="+id);                             
        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);
    } 

在这种情况下loadUrlIntent同时开火。什么是if (resultCode == ConnectionResult.SUCCESS)

  1. 删除Intent intent部分。

  2. 仅供参考: WebView 显示Your connection is not private

  3. 确保resultCode == ConnectionResult.SUCCESS正确满意

答案 2 :(得分:0)

  1. public boolean shouldOverrideUrlLoading(WebView view,String url)
    - &gt; 应该返回false

  2. 覆盖此方法:

    @覆盖         public void onReceivedSslError(WebView视图,                                        SslErrorHandler处理程序,SslError错误){             // super.onReceivedSslError(view,handler,error);             handler.proceed();         }