Created
July 13, 2019 18:55
-
-
Save iamEtornam/3343c8d8de1a4de4484919296cd1c231 to your computer and use it in GitHub Desktop.
Solving SSL Permission Error in Android WebView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
package="com.example.myapplication"> | |
//add Internet permission | |
<uses-permission android:name="android.permission.INTERNET"/> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme" | |
android:usesCleartextTraffic="true" //enable clearTextTraffic | |
tools:ignore="GoogleAppIndexingWarning"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.myapplication; | |
import android.annotation.SuppressLint; | |
import android.content.DialogInterface; | |
import android.net.http.SslError; | |
import android.os.Bundle; | |
import android.webkit.SslErrorHandler; | |
import android.webkit.WebSettings; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
import androidx.appcompat.app.AlertDialog; | |
import androidx.appcompat.app.AppCompatActivity; | |
public class MainActivity extends AppCompatActivity { | |
WebView webView; | |
String url = "https://andela.com/alc"; | |
@SuppressLint("SetJavaScriptEnabled") | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
webView = findViewById(R.id.web_view); | |
WebSettings webSettings = webView.getSettings(); | |
webSettings.setJavaScriptEnabled(true); | |
webView.setWebViewClient(new MyClientWebView()); | |
webView.loadUrl(url); | |
} | |
//our custom webviewclient | |
private class MyClientWebView extends WebViewClient { | |
@Override | |
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) { | |
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); | |
builder.setMessage(R.string.notification_error_ssl_cert_invalid); | |
builder.setPositiveButton("continue", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
handler.proceed(); | |
} | |
}); | |
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
handler.cancel(); | |
} | |
}); | |
final AlertDialog dialog = builder.create(); | |
dialog.show(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment