Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9166645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:11:55+00:00 2026-06-17T15:11:55+00:00

When I run an asynctask to connect to an HTTP server, I want to

  • 0

When I run an asynctask to connect to an HTTP server, I want to display error message when it cannot connect to server. Instead, the app forcefully closes on error, without displaying the error message. I want to display error message when error occurs instead of crashing, but it just shows alert dialog with message error(e.getMessage()) in body description alert dialog.

Below is my code:

private String downloadUrl(String strUrl) throws IOException {
    String data = "";
    InputStream iStream = null;
    try {
        URL url = new URL(strUrl);

        HttpURLConnection urlConnection = (HttpURLConnection) url
            .openConnection();

        urlConnection.connect();
        iStream = urlConnection.getInputStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(
            iStream));

        StringBuffer sb = new StringBuffer();

        String line = "";
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        data = sb.toString();
        br.close();
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
        ErrorDialog(e.getMessage());
    }

    return data;
 }

private class DownloadTask extends AsyncTask<String, Integer, String> {
    String data = null;

    @Override
    protected String doInBackground(String... url) {
        try {
            data = downloadUrl(url[0]);
        } catch (IOException e) {
            Log.d(TAG, e.getMessage());
            ErrorDialog(e.getMessage());
        }

        return data;
    }

    @Override
    protected void onPostExecute(String result) {
        if (result != null && result.length() < 0) {
            ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask();
            listViewLoaderTask.execute(result);
        }
    }
}

private void ErrorDialog(String Description) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(
            ListCategoryActivity.this);
    alertDialog.setTitle("You get Error...");
    alertDialog.setMessage(Description);
    alertDialog.setIcon(R.drawable.ic_warning);

    alertDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

    alertDialog.show();
}

Logcat error:

01-23 11:58:36.949: E/AndroidRuntime(5776): FATAL EXCEPTION: AsyncTask #1
01-23 11:58:36.949: E/AndroidRuntime(5776): java.lang.RuntimeException: An error occured while executing doInBackground()
01-23 11:58:36.949: E/AndroidRuntime(5776):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at java.lang.Thread.run(Thread.java:856)
01-23 11:58:36.949: E/AndroidRuntime(5776): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
01-23 11:58:36.949: E/AndroidRuntime(5776):     at android.os.Handler.<init>(Handler.java:121)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at android.app.Dialog.<init>(Dialog.java:107)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at android.app.AlertDialog.<init>(AlertDialog.java:114)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at android.app.AlertDialog$Builder.create(AlertDialog.java:913)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at android.app.AlertDialog$Builder.show(AlertDialog.java:931)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at com.haichal.codefive.ListCategoryActivity.ErrorDialog(ListCategoryActivity.java:327)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at com.haichal.codefive.ListCategoryActivity.downloadUrl(ListCategoryActivity.java:150)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at com.haichal.codefive.ListCategoryActivity.access$2(ListCategoryActivity.java:125)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at com.haichal.codefive.ListCategoryActivity$DownloadTask.doInBackground(ListCategoryActivity.java:162)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at com.haichal.codefive.ListCategoryActivity$DownloadTask.doInBackground(ListCategoryActivity.java:1)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
01-23 11:58:36.949: E/AndroidRuntime(5776):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-23 11:58:36.949: E/AndroidRuntime(5776):     ... 5 more
01-23 11:58:38.079: E/WindowManager(5776): Activity com.haichal.codefive.ListCategoryActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41410d58 that was originally added here
01-23 11:58:38.079: E/WindowManager(5776): android.view.WindowLeaked: Activity com.haichal.codefive.ListCategoryActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41410d58 that was originally added here
01-23 11:58:38.079: E/WindowManager(5776):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
01-23 11:58:38.079: E/WindowManager(5776):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
01-23 11:58:38.079: E/WindowManager(5776):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
01-23 11:58:38.079: E/WindowManager(5776):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
01-23 11:58:38.079: E/WindowManager(5776):  at android.view.Window$LocalWindowManager.addView(Window.java:537)
01-23 11:58:38.079: E/WindowManager(5776):  at android.app.Dialog.show(Dialog.java:278)
01-23 11:58:38.079: E/WindowManager(5776):  at com.haichal.codefive.ListCategoryActivity.startDownload(ListCategoryActivity.java:100)
01-23 11:58:38.079: E/WindowManager(5776):  at com.haichal.codefive.ListCategoryActivity.initFromDb(ListCategoryActivity.java:91)
01-23 11:58:38.079: E/WindowManager(5776):  at com.haichal.codefive.ListCategoryActivity.onCreate(ListCategoryActivity.java:54)
01-23 11:58:38.079: E/WindowManager(5776):  at android.app.Activity.performCreate(Activity.java:4465)
01-23 11:58:38.079: E/WindowManager(5776):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-23 11:58:38.079: E/WindowManager(5776):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1923)
01-23 11:58:38.079: E/WindowManager(5776):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1984)
01-23 11:58:38.079: E/WindowManager(5776):  at android.app.ActivityThread.access$600(ActivityThread.java:126)
01-23 11:58:38.079: E/WindowManager(5776):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1150)
01-23 11:58:38.079: E/WindowManager(5776):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 11:58:38.079: E/WindowManager(5776):  at android.os.Looper.loop(Looper.java:137)
01-23 11:58:38.079: E/WindowManager(5776):  at android.app.ActivityThread.main(ActivityThread.java:4456)
01-23 11:58:38.079: E/WindowManager(5776):  at java.lang.reflect.Method.invokeNative(Native Method)
01-23 11:58:38.079: E/WindowManager(5776):  at java.lang.reflect.Method.invoke(Method.java:511)
01-23 11:58:38.079: E/WindowManager(5776):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
01-23 11:58:38.079: E/WindowManager(5776):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
01-23 11:58:38.079: E/WindowManager(5776):  at dalvik.system.NativeStart.main(Native Method)

Logcat verbose e.getMessage();

01-23 11:58:36.909: D/HaichalLog(5776): failed to connect to 
  /192.168.103.121 (port 80): connect failed: ENETUNREACH (Network is unreachable)

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:11:57+00:00Added an answer on June 17, 2026 at 3:11 pm

    Run your dialog separate from the UI thread .Just try out this way:

     private class DownloadTask extends AsyncTask<String, Integer, String> {
        String data = null;
        @Override
        protected String doInBackground(String... url) {
           try {
                data = downloadUrl(url[0]);
              } catch (IOException e) {
               Log.d(TAG, e.getMessage());
            runOnUiThread(new Runnable(){
              public void run() {
                     ErrorDialog(e.getMessage());
                    }
         });
        }
    }
            return data;
        }
        @Override
        protected void onPostExecute(String result) {
            if (result != null && result.length() < 0) {
                ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask();
                listViewLoaderTask.execute(result);
            }
        }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use AsyncTask for update my db4o with a server. In the
I have wrote an app to run an AsyncTask and part of the code
I have an AsyncTask in a activity that i only want to run once
I don't understand why I'm getting this error. I'm using AsyncTask to run some
Does there is a way to run my AsyncTask after it finish ? My
I run this code here <html> <script type=text/javascript src=lib/jquery-ui-1.8.21.custom.min.js></script> <script src=http://127.0.0.1:5984/_utils/script/jquery.couch.js></script> <!--<script type=text/javascript src=lib/jquery-1.7.2.js></script>-->
I run into this problem. I have a textarea which I only want to
I want to send a simple http notification after user hit DONE. I use
I'm trying to connect to mysql using AsyncTask in android but i am having
This app must not work without active Internet connection. But, checking Internet connection via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.