0

I was trying out this code, which should show the html code of certain website in the Logcat. But I was only getting Using Network Security Config from resource network_security_config debugBuild: true and java.net.UnknownHostException: Unable to resolve host "www.google.co.in": No address associated with hostname

My internet connection is active. I was following a android tutorial, and that guy did the same and got the result.(He was using old version of android studio

java code

package com.example.newapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {

    public static class DownloadTask extends AsyncTask<String, Void, String>
    {

        @Override
        protected String doInBackground(String... urls) {
            String result= "";
            URL url;
            HttpURLConnection urlConnection= null;
            try{
                url= new URL(urls[0]);

                urlConnection=(HttpURLConnection) url.openConnection();
                InputStream in = urlConnection.getInputStream();
                InputStreamReader reader= new InputStreamReader(in);
                int data = reader.read();

                while (data != -1)
                {
                    char current = (char)data;
                    result+= current;
                    data= reader.read();
                }
                return result;
            }
            catch (Exception e)
            {
                e.printStackTrace();
                return "failed";
            }

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DownloadTask task= new DownloadTask();
        String result = null;
        try {
            result= task.execute("https://www.google.co.in//").get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Log.i("Contents of URL" ,result);
    }
}

AndroidManifest.xml

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

    <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"
        android:networkSecurityConfig="@xml/network_security_config" >
        <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>

network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>
Prithin Babu
  • 176
  • 1
  • 3
  • 12

1 Answers1

-1

By changing the minimum api to 28 my issue was solved, for that you have to keep the api of your emulator greater than 28 which can be done through AVD manager and then go to File>- Project Structure>- Modules. Then Go to Default Config on the top, Change Min SDK version to 28. (You can also try changing it in Gradle Script (below app on the left side of the screen)>- build.app(Module:app) there you would find minSdkVersion, just make changes there.

Prithin Babu
  • 176
  • 1
  • 3
  • 12