1

I would like to load an website via WebView. When it loads via web browser,there is no problem. But when I try to load via WebView, Nothing loads. A white blank background shows instead.

Here is MainActivity class(Here, it is Main class):

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();
    }
}

Here is my 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

Am I doing any wrong? Please give suggestion.

Manas Maity
  • 139
  • 2
  • 14

3 Answers3

1

Remember to add permissions into your Manifest file.

<Manifest> 

...

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

...

</Manifest>
Oli Wood
  • 46
  • 2
0

Problem

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

In that case loadUrl and Intent firing same time . What is if (resultCode == ConnectionResult.SUCCESS)

  1. Remove Intent intent Section .

  2. FYI : WebView showing Your connection is not private

  3. Make sure resultCode == ConnectionResult.SUCCESS Properly Satisfied or not
IntelliJ Amiya
  • 70,230
  • 14
  • 154
  • 181
0
  1. public boolean shouldOverrideUrlLoading(WebView view, String url)
    -> should return false
  2. Override this method:

    @Override    
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {    
        // super.onReceivedSslError(view, handler, error);    
        handler.proceed();    
    }    
    
anhtuannd
  • 883
  • 9
  • 15
hoangtu23
  • 789
  • 8
  • 10
  • Actually it's bad example and Google Play will throw a security alert. We should add dialog to let user decide whether he want to avoid SSL error or not. – anhtuannd Aug 09 '19 at 12:48