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 8952589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:54:29+00:00 2026-06-15T13:54:29+00:00

Below is the code which is basically owrking…well mostly. It works on all phones

  • 0

Below is the code which is basically owrking…well mostly. It works on all phones I was testing, but fails on the client phone, I can’t use. Which part of this code could cause NetworkOnMainThreadException?

It’s really straight forward. What I do in onPostExecute() is:

  1. Geting InputStream from onBackground()
  2. Aborting if InputStream is null, aborting if null.
  3. Checking external storage avalibility, aborting if not.
  4. Creating file from InputStream
  5. Checking PDF viewer avalibility, aborting if not avalible
  6. Opening newly created PDF

    @Override
    protected void onPostExecute(InputStream is) {
        super.onPostExecute(is);
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    
        if (is== null) {
            errorMessage = getString(R.string.error_server_communication);
            CustomDialogs.showErrorDialog( fragment.getActivity(), getString( R.string.error_title ), errorMessage );
            return;
        }
    
        //checking external storage avalibility
        if (!Tools.isWriteableExternalStorageAvalible()) {
            errorMessage = getString(R.string.error_no_external_storage);
            CustomDialogs.showErrorDialog( fragment.getActivity(), getString( R.string.error_title ), errorMessage );
            return;
        }               
    
        // opening pdf
        try {
            File directory = new File(Environment.getExternalStorageDirectory(), "MyApp");
            if (!directory.exists())
                directory.mkdirs();
    
            File file = new File(directory, "file.pdf");
    
            //usuwa poprzednio przechowywany wynik jesli taki byl
            if (file.exists()) 
                file.delete();
    
            file.createNewFile();                   
    
            // write the inputStream to a FileOutputStream
            OutputStream out = new FileOutputStream(file);
            out.write(IOUtils.toByteArray(is));
    
            is.close();
            out.flush();
            out.close();
    
            if (Tools.canDisplayPdf(fragment.getActivity())) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(file), "application/pdf");
                startActivity( intent );
            } else {
                //user nie ma przegladarki pdf - komunikat
                errorMessage = getString(R.string.error_server_communication);
                CustomDialogs.showErrorDialog( fragment.getActivity(), getString( R.string.error_title ), errorMessage );
            }
    
        } catch (IOException e) {
            errorMessage = getString(R.string.error_server_communication);
            CustomDialogs.showErrorDialog( fragment.getActivity(), getString( R.string.error_title ), errorMessage );
        }
    }
    

Also including a stack trace. There is one more clue. Client is retrieving InputStream using SSL, and I’m not. Unfortunately can’t debug it with SSL yet.

12-05 21:34:02.648: E/AndroidRuntime(711): FATAL EXCEPTION: main
12-05 21:34:02.648: E/AndroidRuntime(711): android.os.NetworkOnMainThreadException
12-05 21:34:02.648: E/AndroidRuntime(711):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
12-05 21:34:02.648: E/AndroidRuntime(711):  at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:668)
12-05 21:34:02.648: E/AndroidRuntime(711):  at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
12-05 21:34:02.648: E/AndroidRuntime(711):  at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134)
12-05 21:34:02.648: E/AndroidRuntime(711):  at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:174)
12-05 21:34:02.648: E/AndroidRuntime(711):  at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:188)
12-05 21:34:02.648: E/AndroidRuntime(711):  at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:178)
12-05 21:34:02.648: E/AndroidRuntime(711):  at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1383)
12-05 21:34:02.648: E/AndroidRuntime(711):  at org.apache.commons.io.IOUtils.copy(IOUtils.java:1357)
12-05 21:34:02.648: E/AndroidRuntime(711):  at org.apache.commons.io.IOUtils.toByteArray(IOUtils.java:361)
12-05 21:34:02.648: E/AndroidRuntime(711):  at pl.luxmed.pp.activities.MedicalExaminationsActivity$OpenPdfAsyncTask.onPostExecute(MedicalExaminationsActivity.java:270)
12-05 21:34:02.648: E/AndroidRuntime(711):  at pl.luxmed.pp.activities.MedicalExaminationsActivity$OpenPdfAsyncTask.onPostExecute(MedicalExaminationsActivity.java:1)
12-05 21:34:02.648: E/AndroidRuntime(711):  at android.os.AsyncTask.finish(AsyncTask.java:631)
12-05 21:34:02.648: E/AndroidRuntime(711):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-05 21:34:02.648: E/AndroidRuntime(711):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-05 21:34:02.648: E/AndroidRuntime(711):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 21:34:02.648: E/AndroidRuntime(711):  at android.os.Looper.loop(Looper.java:137)
12-05 21:34:02.648: E/AndroidRuntime(711):  at android.app.ActivityThread.main(ActivityThread.java:4745)
12-05 21:34:02.648: E/AndroidRuntime(711):  at java.lang.reflect.Method.invokeNative(Native Method)
12-05 21:34:02.648: E/AndroidRuntime(711):  at java.lang.reflect.Method.invoke(Method.java:511)
12-05 21:34:02.648: E/AndroidRuntime(711):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-05 21:34:02.648: E/AndroidRuntime(711):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-05 21:34:02.648: E/AndroidRuntime(711):  at dalvik.system.NativeStart.main(Native Method)
  • 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-15T13:54:30+00:00Added an answer on June 15, 2026 at 1:54 pm

    Though you are using AsyncTask, its onPostExecute() method is executed on UI thread (to let you update your views etc). Actually, the only method of AsyncTask executed in separate thread is doInBackground(), so you have to perform all operations involving I/O (disk, network), only in this method, otherwise you’ll get an exception.

    Refer to this for details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently i'm using below code which works well. $(#topperAtBaseLevel:visible, #lowerAtBaseLevel:visible, #midAtBaseLevel).hide(); any optimised code?
I have the following code below which works, but I want to insert values
Hi All below is the code which i use. <!DOCTYPE html PUBLIC -//W3C//DTD XHTML
Hi I have this code below which basically after 25 seconds of showing the
I have the code snippet below in C++ which basically calculates the pi using
So, I have the code below which works fine when entered into Google's Rich
Below I have some code which basically animates a div from the bottom of
Basically, the code below will generate divs which are then captured using the jquery
I have below code which overrides equals() and hashcode() methods. public boolean equals(Object obj)
Can anyone help me to rewrite the below code which use a fade effect

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.