James do
James do

Reputation: 59

Android: Why does my android app displays an empty layout then crash when I am trying to retrieve information from a website?

I am trying to retrieve the source of a website but my app neither displays my gui nor does it print to the logs.

Here is my code.

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 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();           //start reading inputstream reader

            while(data!=-1){
                char current = (char) data;

                result += current;

                data = reader.read();

            }
            return result;

        }
        catch (Exception e){

            e.printStackTrace();
            return null;
        }


    }
}

@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.therichest.com/top-lists/top-100-richest-celebrities").get();

        Log.i("Contents of the url:", result);

    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }
    catch (ExecutionException e) {
        e.printStackTrace();
    }

}

}

This is what displays in my log

07-06 21:08:01.020 2025-2025/? I/art: Late-enabling -Xcheck:jni
07-06 21:08:01.050 2025-2031/? I/art: Debugger is no longer active
07-06 21:08:01.082 2025-2025/? W/System: ClassLoader referenced unknown path: /data/app/com.example.jimmy.guesstherich-1/lib/arm
07-06 21:08:01.116 2025-2025/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-06 21:08:01.138 2025-2025/? D/PhoneWindowEx: [PWEx][generateLayout] setLGNavigationBarColor : colors=0xff000000
07-06 21:08:01.138 2025-2025/? I/PhoneWindow: [setLGNavigationBarColor] color=0x ff000000
07-06 21:08:01.233 2025-2039/? D/libc: skt_base:0, kt_base:0, mptcp_enabled:0, socks_enabled:0, wifi_connected:1
07-06 21:08:01.234 2025-2039/? D/libc: skt_base:0, kt_base:0, mptcp_enabled:0, socks_enabled:0, wifi_connected:1
07-06 21:08:32.295 2025-2039/com.example.jimmy.guesstherich W/System.err: java.net.ConnectException: failed to connect to www.therichest.com/34.202.89.146 (port 443): connect failed: ETIMEDOUT (Connection timed out)
07-06 21:08:32.295 2025-2039/com.example.jimmy.guesstherich W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:124)
07-06 21:08:32.295 2025-2039/com.example.jimmy.guesstherich W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
07-06 21:08:32.295 2025-2039/com.example.jimmy.guesstherich W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452)
07-06 21:08:32.295 2025-2039/com.example.jimmy.guesstherich W/System.err:     at java.net.Socket.connect(Socket.java:888)
07-06 21:08:32.295 2025-2039/com.example.jimmy.guesstherich W/System.err:     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:117)
07-06 21:08:32.296 2025-2039/com.example.jimmy.guesstherich W/System.err:     at com.android.okhttp.internal.http.SocketConnector.connectRawSocket(SocketConnector.java:188)
07-06 21:08:32.296 2025-2039/com.example.jimmy.guesstherich W/System.err:     at com.android.okhttp.internal.http.SocketConnector.connectTls(SocketConnector.java:99)
07-06 21:08:32.296 2025-2039/com.example.jimmy.guesstherich W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:151)
07-06 21:08:32.296 2025-2039/com.example.jimmy.guesstherich W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:187)
07-06 21:08:32.296 2025-2039/com.example.jimmy.guesstherich W/System.err:     at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:238)
07-06 21:08:32.296 2025-2039/com.example.jimmy.guesstherich W/System.err:     at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:134)
07-06 21:08:32.296 2025-2039/com.example.jimmy.guesstherich W/System.err:     at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:384)
07-06 21:08:32.296 2025-2039/com.example.jimmy.guesstherich W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:371)
07-06 21:08:32.296 2025-2039/com.example.jimmy.guesstherich W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:289)
07-06 21:08:32.296 2025-2039/com.example.jimmy.guesstherich W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
07-06 21:08:32.296 2025-2039/com.example.jimmy.guesstherich W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:408)
07-06 21:08:32.296 2025-2039/com.example.jimmy.guesstherich W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:247)
07-06 21:08:32.296 2025-2039/com.example.jimmy.guesstherich W/System.err:     at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
07-06 21:08:32.296 2025-2039/com.example.jimmy.guesstherich W/System.err:     at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)
07-06 21:08:32.296 2025-2039/com.example.jimmy.guesstherich W/System.err:     at com.example.jimmy.guesstherich.MainActivity$DownloadTask.doInBackground(MainActivity.java:29)
07-06 21:08:32.296 2025-2039/com.example.jimmy.guesstherich W/System.err:     at com.example.jimmy.guesstherich.MainActivity$DownloadTask.doInBackground(MainActivity.java:15)
07-06 21:08:32.297 2025-2039/com.example.jimmy.guesstherich W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
07-06 21:08:32.297 2025-2039/com.example.jimmy.guesstherich W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-06 21:08:32.297 2025-2039/com.example.jimmy.guesstherich W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
07-06 21:08:32.297 2025-2039/com.example.jimmy.guesstherich W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
07-06 21:08:32.297 2025-2039/com.example.jimmy.guesstherich W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
07-06 21:08:32.297 2025-2039/com.example.jimmy.guesstherich W/System.err:     at java.lang.Thread.run(Thread.java:818)
07-06 21:08:32.297 2025-2039/com.example.jimmy.guesstherich W/System.err: Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
07-06 21:08:32.297 2025-2039/com.example.jimmy.guesstherich W/System.err:     at libcore.io.Posix.connect(Native Method)
07-06 21:08:32.298 2025-2039/com.example.jimmy.guesstherich W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
07-06 21:08:32.298 2025-2039/com.example.jimmy.guesstherich W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
07-06 21:08:32.298 2025-2039/com.example.jimmy.guesstherich W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:122)
07-06 21:08:32.298 2025-2039/com.example.jimmy.guesstherich W/System.err:   ... 26 more
07-06 21:08:32.299 2025-2025/com.example.jimmy.guesstherich D/AndroidRuntime: Shutting down VM

It shows a blank screen for 30-45 seconds then it crashes and the logs print the following

07-06 21:08:32.300 2025-2025/com.example.jimmy.guesstherich E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.jimmy.guesstherich, PID: 2025
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jimmy.guesstherich/com.example.jimmy.guesstherich.MainActivity}: java.lang.NullPointerException: println needs a message
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2440)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500)
                                                                                  at android.app.ActivityThread.access$900(ActivityThread.java:163)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:148)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5585)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
                                                                               Caused by: java.lang.NullPointerException: println needs a message
                                                                                  at android.util.Log.println_native(Native Method)
                                                                                  at android.util.Log.i(Log.java:160)
                                                                                  at com.example.jimmy.guesstherich.MainActivity.onCreate(MainActivity.java:67)
                                                                                  at android.app.Activity.performCreate(Activity.java:6279)
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500) 
                                                                                  at android.app.ActivityThread.access$900(ActivityThread.java:163) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:148) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5585) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

Any idea whats going on?

I'm a beginner so I apologize for any mistakes.

I am also following a video tutorial and have triple checked that everything is the same. His works completely fine. If it matters it's Rob Percival's "The Complete Android Development Course"

Thanks in advance, I appreciate it!

Upvotes: 0

Views: 55

Answers (2)

Arpit Patel
Arpit Patel

Reputation: 8067

You are facing this problem because you are putting wrong URL chek that url in your browser.

result = task.execute("https://www.therichest.com/top-lists/top-100-richest-celebrities").get();

Here is demo URL

result = task.execute("http://api.androidhive.info/contacts/").get();

Upvotes: 0

Alex
Alex

Reputation: 9352

This line will always return you null(because asynktask is asynchronous):

result = task.execute("https://www.therichest.com/top-lists/top-100-richest-celebrities").get();

And the you try to print log with null object, this is causing the crash.

The empty layout is because application cannot connect to the website, and receiving timeout exception.

Upvotes: 2

Related Questions