0

I'm trying to check uncaught exceptions in any of my activities.

I put this block code after onCreate :

Thread.setDefaultUncaughtExceptionHandler(
                    new UncaughtExceptionHandler() {

                        @Override
                        public void uncaughtException(Thread thread, Throwable ex) {
                            ex.printStackTrace();
                            Utils.logError(MainActivity.this,session);
                        }
                    });

Utils.logError method writes logCat content to a text file and then upload with a custom AsyncTask to a FTP Server.

This is the logError method code

public static void logError(final Context ctx,final SessionManager session){
        StringBuilder log=new StringBuilder(); 

        try {  

              (... code that read logCat  and write to a file ...)

               //Upload result file to FTP with a AsyncTask
                new Thread(new Runnable() {
                    public void run() {
                        if(Utils.isInternetConn(ctx)){
                            FTPHandler ftp = null;
                            ArrayList<File> archivosAcargar = new ArrayList<File>();
                            archivosAcargar.add(file);
                            ftp = new FTPHandler(ctx,archivosAcargar,session.getCodUser());
                            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                            StrictMode.setThreadPolicy(policy);                                            
                            if(ftp.ftpConnect("server","user" ,"pass" ,21)){
                            ftp.execute("upload");
                               }

                           }

                    }
                }).start();

         } catch (FileNotFoundException e) {  
          e.printStackTrace();  
         } catch (IOException e) {  
          e.printStackTrace();  
         }  


    }

The AsyncTask code

EDIT:

@Override
    protected String doInBackground(String... peticion) {
        String res="";

        if(peticion[0].equals("upload")){
            if(ftpUpload()) res = "Done upload";
            else res = "Error upload";

        }
        //This print shows "Done upload"
        System.out.println(res);

        return res;

    }

@Override
    protected void onPostExecute(String response) {

        //This print not executes
        System.out.println("Response " + "/" + response + "/");

        if(response.equals("Done upload"))
        {
            System.out.println("done");
            if(!isLog){ mDialog.dismiss();
            //Toast
          }
            else{
                System.exit(1);

            }

        }

        if(response.equals("Error upload"))
        {
            if(!isLog)mDialog.dismiss();    


        }

    }

The problem is that the AsyncTask is completed and the file is uploaded the (@Override) onPostExecute never is called. I've tried to call Utils.logError without a exception and works fine but if I force a exception not works.

Can anyone help me?

Hanzo
  • 1,839
  • 4
  • 30
  • 51

0 Answers0