Home > Blockchain >  The application is minimized when the code is executed
The application is minimized when the code is executed

Time:11-16

I am writing the simplest application for Android. I receive all the HTML of the site and display this text on the smartphone screen. All this happens in Android Studio. Created a project using the "Fragment ViewModel" template. In MainActiviti, using the getContent method, I connect to the site and read its HTML. The method works, but as soon as I try to transfer the text to the screen, the application is immediately minimized on startup. Tell me what's wrong.

public class MainFragment extends Fragment{
    final public String URL_POSTER = "https://udmfil.ru/";


    public static MainFragment newInstance() {
        return new MainFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        DownLoad FOG = new DownLoad();
        FOG.execute(URL_POSTER);
        String result_post = String.valueOf(FOG);
        Log.i("URL",result_post);
        return inflater.inflate(R.layout.main_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        new ViewModelProvider(this).get(MainViewModel.class);
    }
    public class DownLoad extends AsyncTask<String,Void,String[]> {

        @Override
        protected String[] doInBackground(String... strings) {
            HttpURLConnection connection = null;
            BufferedReader reader= null;

            try {
                URL url = new URL(strings[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();

                InputStream stream = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(stream));

                StringBuilder buffer = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null)
                    buffer.append(line);
                    Log.i("URL", String.valueOf(buffer));
                /*String pos = buffer.toString();
                int indexPost, i = 1;
                String postB;
                do {
                    indexPost = pos.indexOf("m-img buy")   21;
                    Log.i("URL", String.valueOf(indexPost));
                    if (indexPost != -1){
                        postB = pos.substring(indexPost, indexPost   55);
                        Log.i("URL",postB);
                        strings[i] = postB;
                        indexPost = indexPost  55;
                        pos = pos.substring(indexPost);
                        Log.i("FLAG",pos);
                        i  ;
                        Log.i("FLAG",strings[i]);
                        Log.i("URL", String.valueOf(i));
                    }
                }while (indexPost != -1);*/
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (connection != null)
                    connection.disconnect();
                try {
                    if (reader != null)
                        reader.close();
                }catch (IOException e){
                    e.printStackTrace();
                }

            }

            return strings;
        }
    }
}

error log:

java.lang.RuntimeException: An error occurred while executing doInBackground()
 at android.os.AsyncTask$4.done(Unknown Source:27)
 at java.util.concurrent.FutureTask.finishCompletion(Unknown Source:36)
 at java.util.concurrent.FutureTask.setException(Unknown Source:23)
 at java.util.concurrent.FutureTask.run(Unknown Source:40)
 at android.os.AsyncTask$SerialExecutor$1.run(Unknown Source:2)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source:79)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source:2)
 at java.lang.Thread.run(Unknown Source:4)
Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
 at java.net.Inet6AddressImpl.lookupHostByName(Unknown Source:111)
 at java.net.Inet6AddressImpl.lookupAllHostAddr(Unknown Source:22)
 at java.net.InetAddress.getAllByName(Unknown Source:3)
 at com.android.okhttp.Dns$1.lookup(Dns.java:41)
 at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:178)
 at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:144)
 at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:86)
 at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:176)
 at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
 at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
 at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
 at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
 at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
 at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
 at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:90)
 at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:30)
 at com.example.myapplication.ui.main.MainFragment$DownLoad.doInBackground(MainFragment.java:58)
 at com.example.myapplication.ui.main.MainFragment$DownLoad.doInBackground(MainFragment.java:48)
 at android.os.AsyncTask$3.call(Unknown Source:20)
 at java.util.concurrent.FutureTask.run(Unknown Source:31)
Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
 at libcore.io.Linux.android_getaddrinfo(Native Method)
 at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:74)
 at libcore.io.BlockGuardOs.android_getaddrinfo(BlockGuardOs.java:200)
 at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:74)
 at java.net.Inet6AddressImpl.lookupHostByName(Unknown Source:51)
Caused by: android.system.ErrnoException: android_getaddrinfo failed: EACCES (Permission denied)
 

Now this is how it turns out.

CodePudding user response:

As the message in the error log suggested, you have to add INTERNET permission to the Manifest file like below:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.stackoverflow">

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

CodePudding user response:

As you have not given the Internet permission host is not able to access the address and receive the data and in result it is getting crashed when you initiating the app.

Insert the below line in your menifest:

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