Search code examples
javaandroidandroid-activityioexception

How to handle a IOException by starting a new Activity in android


I am trying to start my main page activity when an exception is thrown. Right now when the exception happens, the app shuts down and starts from the main page. I want for it to seamlessly go to the main page app, not give the user a waring that the app is shutting down. The error it gives me is this:

D/Issue with Http:: IOException
04-19 13:57:31.518 27520-27570/myapp W/System.err: java.io.FileNotFoundException: my webpage
04-19 13:57:31.518 27520-27570/myapp W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238)
04-19 13:57:31.518 27520-27570/myapp W/System.err:     at myapp.HttpManager.MyHttpGet.httpDownloadData(MyHttpGet.java:79)
04-19 13:57:31.518 27520-27570/myapp W/System.err:     at myapp.HttpManager.MyHttpGet.doInBackground(MyHttpGet.java:53)
04-19 13:57:31.518 27520-27570/myapp W/System.err:     at myapp.HttpManager.MyHttpGet.doInBackground(MyHttpGet.java:31)
04-19 13:57:31.518 27520-27570/myapp W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
04-19 13:57:31.518 27520-27570/myapp W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-19 13:57:31.519 27520-27570/myapp W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
04-19 13:57:31.519 27520-27570/myapp W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
04-19 13:57:31.519 27520-27570/myapp W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
04-19 13:57:31.519 27520-27570/myapp W/System.err:     at java.lang.Thread.run(Thread.java:818)
04-19 13:57:31.538 27520-27520/myapp D/AndroidRuntime: Shutting down VM
04-19 13:57:31.594 27520-27520/myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: myapp, PID: 27520
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
at myapp.JSON.LoginJsonParser.checkUsername(LoginJsonParser.java:41)
at myapp.LoginPage$2.onClick(LoginPage.java:128)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

This is fine and dandy for dev, but I want to start a new activity when this happens. What I have code wise for this problem is this.

 private String httpDownloadData(String myUrl)

{
    String respone = null;
    HttpURLConnection urlConnection = null;
    try
    {
        URL url = new URL(myUrl);
        //put in the username and pssword for parmas to send to url
        //this is good for login
        if (username!=null)
        {
            Authenticator.setDefault(new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password.toCharArray());
                }
            });
        }

        urlConnection = (HttpURLConnection)url.openConnection();
        InputStream inputStream = urlConnection.getInputStream();

        if(inputStream != null)
        {
            respone = streamToString(inputStream);
            inputStream.close();
        }

    }catch (IOException ie)
    {
        //ie.printStackTrace();
        Log.d("Issue with Http: " , "IOException");
        ie.printStackTrace();
        Intent newIntent = new Intent(activity.getApplicationContext(), ECom.class);
        activity.startActivity(newIntent);
    }finally {
        if(urlConnection != null)
        {
            urlConnection.disconnect();
        }
    }
    return respone;
}

Thank you for any help with this.


Solution

  • You cannot perform any networking operation in UI thread, it will give you networkOnMainThreadException. Please use AsyncTask to perform network and long running operation. You can find samples and docs here

    or use volley a framework to handle network operations