Solve java.lang.SecurityException: Permission denied (missing INTERNET permission?)at java.net.Inet6Add

Table of Contents

Solve java.lang.SecurityException: Permission denied (missing INTERNET permission?) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:135)


Solution to java.lang.SecurityException: Permission denied (missing INTERNET permission?) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:135)

When developing Android applications, we often encounter various exceptions. One of the common problems is java.lang.SecurityException: Permission denied (missing INTERNET permission?) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:135). This exception is usually caused by a lack of ??INTERNET?? permissions. In an Android application, when using network functions, you need to add the INTERNET permission to the AndroidManifest.xml file. To resolve this issue we need to perform the following steps:

  1. Open your Android project.
  2. Navigate to the app/src/main directory and find the AndroidManifest.xml file.
  3. Find the tag in the AndroidManifest.xml file and add xmlns:android=" http://schemas.android.com/apk/res/android"??property, make sure it is added.
  4. In the tag, add the following code to apply for the INTERNET permission:
xmlCopy code<uses-permission android:name="android.permission.INTERNET" />
  1. Save and close the AndroidManifest.xml file.
  2. Compile and re-run your Android application. Your application should now be able to use network functionality correctly without ??java.lang.SecurityException: Permission denied (missing INTERNET permission?) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java: 135)??Exception. Hope this article can help solve the ??java.lang.SecurityException: Permission denied (missing INTERNET permission?) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:135)?? exception, and Make your Android apps run smoothly. If you have any questions or concerns, please feel free to ask me.

When we develop an application that needs to use network functions, we often encounter??java.lang.SecurityException: Permission denied (missing INTERNET permission?) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java: 135)?Exception. Here is a sample code that demonstrates how to add the INTERNET permission in an Android application: First, in the AndroidManifest.xml file Add ??INTERNET?? permissions:

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

Next, in your Java code, you can use network functions. For example, you can use the HttpURLConnection class to get data from a URL and display it in a TextView:

javaCopy code//Import the required classes
import android.os.AsyncTask;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
//Declare an asynchronous task class
public class NetworkTask extends AsyncTask<String, Void, String> {
    private TextView textView;
    //Construction method, pass in the TextView that displays the results
    public NetworkTask(TextView textView) {
        this.textView = textView;
    }
    //Perform network requests in the background
    @Override
    protected String doInBackground(String... urls) {
        String result = "";
        HttpURLConnection connection = null;
        try {
            URL url = new URL(urls[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            InputStream inputStream = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
            result = stringBuilder.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        return result;
    }
    // After the network request is completed, display the result in TextView
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        textView.setText(result);
    }
}

Then, you can use this asynchronous task class in your Activity to make network requests and display results:

javaCopy code//Import the required classes
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
//Declare a MainActivity class that inherits from AppCompatActivity
public class MainActivity extends AppCompatActivity {
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.text_view);
        // Create an asynchronous task object and execute it
        NetworkTask networkTask = new NetworkTask(textView);
        networkTask.execute("http://www.example.com");
    }
}

In this sample code, we use the ??HttpURLConnection?? class to make HTTP requests and perform background processing through the asynchronous task class. Finally, we display the request results in the TextView in the ??onPostExecute()?? method. I hope this sample code can help you solve the problem in actual application scenarios??java.lang.SecurityException: Permission denied (missing INTERNET permission?) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:135)??Exception. If you have any questions or concerns, please feel free to ask me.

??AndroidManifest.xml? is the manifest file of the Android application. It is an XML file that stores an application’s metadata, including declarations of components such as application name, icons, permissions, activities, services, and receivers. In Android development, the ?AndroidManifest.xml? file plays a very important role in building and running applications. The following is a detailed introduction to some common elements and attributes in the ?AndroidManifest.xml?? file:

  1. ???: Root element used to declare the application’s manifest file.
  • ??package??: Required attribute, used to specify the package name of the application.
  • ??xmlns:android??Attribute: required attribute, used to declare XML namespace.
  1. ???: Used to declare the permissions required by the application.
  • ??android:name??: Required attribute, used to specify the permission name.
  1. ???: used to declare the main components of the application.
  • ??android:icon??: Optional attribute used to specify the icon of the application.
  • ??android:label??: Optional attribute used to specify the name of the application.
  • ??android:theme??: Optional attribute used to specify the theme style of the application.
  1. ???: used to declare the activity component (Activity) of the application.
  • ??android:name??: Required attribute, used to specify the name of the activity class.
  • ??android:label??: Optional attribute used to specify the display name of the activity.
  • ??android:theme??: Optional attribute used to specify the theme style of the activity.
  1. ???: Used to declare the service component of the application.
  • ??android:name??: Required attribute, used to specify the name of the service class.
  1. ???: Used to declare the broadcast receiver component of the application.
  • ??android:name??: Required attribute, used to specify the name of the broadcast receiver class.
  1. ???: Used to declare an intent filter for an activity, service, or receiver.
  • ????: Used to specify the intended operation.
  • ????: used to specify the category of intent.
  1. ???: Used to add additional metadata to the component.
  • ??android:name??: Required attribute, used to specify the name of the metadata.
  • ??android:value??: Optional attribute used to specify the value of metadata. The purpose of the AndroidManifest.xml file is to introduce the application’s features and components to the system and to declare the permissions required by the application. When the application is built and run, the file is read and parsed, and the declarations in it are processed accordingly. Therefore, it is very important to keep the AndroidManifest.xml file correct and complete to ensure that the application can run correctly.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 138778 people are learning the system