【问题标题】:Android Webview does not load the urlAndroid Webview 不加载 url
【发布时间】:2016-07-18 07:28:05
【问题描述】:

我想通过 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>

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

【问题讨论】:

  • 任何异常???
  • 你确定 ResultCode == ConnectionResult.SUCCESS 是真的吗??
  • loadUrl 和 Intent 同时触发
  • @Sr. Pulgarin,是的,我确信,因为第二个活动效果很好,而且我已经在控制语句“ResultCode == ConnectionResult.SUCCESS”下测试了日志中的示例行,效果很好。

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


【解决方案1】:

记得将权限添加到您的清单文件中。

<Manifest> 

...

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

...

</Manifest>

【讨论】:

  • 感谢您的提醒。这个我已经实现了。
  • @ManasMaity - 只是检查。我总是忘记那部分。
  • 我可以以编程方式使用 chrome 作为默认 webview 吗?
【解决方案2】:

问题

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 是否满足

【讨论】:

  • @inteliJ Amiya,感谢您的回复。成功加载网站后,我需要进行另一项活动。如果我从上面的地方删除意图,那么我应该把它放在哪里?请解释。再次感谢。
  • @ManasMaity 转到stackoverflow.com/questions/6199717/… 。当 100% loadad 然后添加意图
  • @ManasMaity 检查上面的链接。我面对它。希望这对你有帮助。
  • 并确保添加此&lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  • @ManasMaity 请让我反馈一下。
【解决方案3】:
  1. public boolean shouldOverrideUrlLoading(WebView view, String url)
    -> 应该返回false
  2. 重写此方法:

    @Override    
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {    
        // super.onReceivedSslError(view, handler, error);    
        handler.proceed();    
    }    
    

【讨论】:

  • 实际上这是个坏例子,Google Play 会发出安全警报。我们应该添加对话框让用户决定是否要避免 SSL 错误。
猜你喜欢
  • 1970-01-01
  • 2018-10-18
  • 2013-04-18
  • 1970-01-01
  • 1970-01-01
  • 2011-11-16
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多