๐Ÿ“˜ Programming/Android

#9 WebView(์›น ๋ทฐ) - err_cleartext_not_permitted ํ•ด๊ฒฐ๋ฒ•

ํ•œ์ฝ”๋”ฉ 2020. 5. 5. 15:59
728x90
728x90

๋ฉ”๋‹ˆํŽ˜์ŠคํŠธ์— ์ธํ„ฐ๋„ท์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ •์ด ํ•„์š”ํ•จ

<uses-permission android:name="android.permission.INTERNET"/>๋ฅผ ์•„๋ž˜์ฒ˜๋Ÿผ ์ž…๋ ฅํ•ด์•ผํ•จ

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.webviewexample">

    <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:usesCleartextTraffic="true"
        android:theme="@style/AppTheme">
        <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>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>
package com.example.webviewexample;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    WebView webView;
    // url์€ ์ž์‹ ์ด ๋ณด์—ฌ์ฃผ๊ณ  ์‹ถ์€ ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅ
    String url = "http://www.naver.com";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView) findViewById(R.id.webView);

        // ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ—ˆ์šฉํ•ด์ฃผ๊ธฐ ์œ„ํ•ด true๋กœ ์˜ต์…˜ ์„ค์ •
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(url);   // ํ•ด๋‹น url์„ ๋ณด์—ฌ์ค˜๋ผ
        webView.setWebChromeClient(new WebChromeClient());   // ํฌ๋กฌ์—์„œ ์ตœ์ ํ™”๋˜์–ด ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•œ ์„ค์ •

        // webViewClientClass ์˜ค๋ฅ˜ ์ด์œ  : ๋‚ด๋ถ€ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ด์•ผํ•จ
        // ( ๋นจ๊ฐ„ ๊ธ€์”จ ํด๋ฆญ ํ›„ Alt + Enterํ•˜๊ณ  ๋‚˜์˜จ ๋ฉ”๋‰ด์—์„œ Inner Class๋ฅผ ์„ ํƒํ•˜์—ฌ ์ƒ์„ฑ
        webView.setWebViewClient(new WebViewClientClass());
    }

    // ์•ˆ๋“œ๋กœ์ด๋“œ์˜ ํ‚ค๋ณด๋“œ์˜ ํ‚ค๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ ์–ด๋–ค ๋™์ž‘์„ ํ•˜๋Š”์ง€ ์ง€์ •
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        // ๋’ค๋กœ๊ฐ€๊ธฐ ๋ˆ„๋ฅผ ๋•Œ, ์›น์ด ๋’ค๋กœ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค๋ฉด ๋’ค๋กœ๊ฐ€๊ฒŒ ํ•˜๋ผ!
        if((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()){
            webView.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    private class WebViewClientClass extends WebViewClient {

        // ํ˜„์žฌ ํŽ˜์ด์ง€ url์„ ์ฝ์–ด์˜ค๋Š” ๋ฉ”์†Œ๋“œ๋กœ ์ƒˆ ์ฐฝ์„ ์ฝ๊ฑฐ๋‚˜, ํŠน์ˆ˜ ๊ธฐ๋Šฅ์„ ๋„ฃ์„ ์ˆ˜ ์žˆ์œผ๋‹ˆ ๊ตฌํ˜„ํ•˜๋Š”๊ฒƒ์ด ์ข‹์Œ
       @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

 

 

 

์•„๋ž˜์™€ ๊ฐ™์ด ERR_CLEARTEXT_NOT_PERMITTED๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค๋ฉด

๋ฉ”๋‹ˆํŽ˜์ŠคํŠธ ํŒŒ์ผ์—์„œ         android:usesCleartextTraffic="true"      ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์ž

์—†๋‹ค๋ฉด ๊ฒŒ์‹œ๊ธ€ ์ƒ๋‹จ์˜ ๋ฉ”๋‹ˆํŽ˜์ŠคํŠธ ์ฝ”๋“œ์™€ ๊ฐ™์ด ์ž…๋ ฅํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•