0

I have a service that send errors to a url ,this service good work in other services but when i have an error in self falls down in loops that is very bad.

public class Error_Service extends Service {
    Context context;
    RequestPackage RP;
    String Value;
    G g;
    Internet_Connect IC;
    public final static String MY_ACTION_E = "MY_ACTION_E";
    @Override
    public void onCreate() {
        context = this;
        IC = new Internet_Connect(context);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        handleStart(intent, startId);
        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    void handleStart(Intent intent, int startId) {
        String ww = null;
        try {
            Log.i("ASDASASDAS",ww);
        }catch (Exception e){
            g = new G();
            new GetErrors(context);
            Exception_String EX =new Exception_String();
            GetErrors.SendErrors("43", EX.ValueEX(e), g.VersionCode());
            return;
        }
        RP = (RequestPackage) intent.getSerializableExtra("MyValue");
        try {
            Value =  new ErrorService().execute(RP).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
            if (IC.getConnectivityStatus()) {
                g = new G();
                new GetErrors(context);
                Exception_String EX = new Exception_String();
                GetErrors.SendErrors("43", EX.ValueEX(e), g.VersionCode());
            }
        } catch (ExecutionException e) {
            e.printStackTrace();
            if (IC.getConnectivityStatus()) {
                g = new G();
                new GetErrors(context);
                Exception_String EX = new Exception_String();
                GetErrors.SendErrors("43", EX.ValueEX(e), g.VersionCode());
            }
        } finally {
            ThreadFinish_Send thread_finish = new ThreadFinish_Send();
            thread_finish.start();
        }
    }
    public class ErrorService extends AsyncTask<RequestPackage,String,String>{

        @Override
        protected String doInBackground(RequestPackage... params) {
            BufferedReader reader = null;
            String uri = params[0].getUri();
            if (params[0].getMethod().equals("GET")) {
                uri += "?" + params[0].getEncodedParams();

            }
            try {
                URL url = new URL(uri);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod(params[0].getMethod());
                StringBuilder sb = new StringBuilder();
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                return sb.toString();
            } catch (Exception e) {
                e.printStackTrace();
                if (IC.getConnectivityStatus()) {
                    g = new G();
                    new GetErrors(context);
                    Exception_String EX = new Exception_String();
                    GetErrors.SendErrors("43", EX.ValueEX(e), g.VersionCode());
                    return null;
                }
                return null;
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                        if (IC.getConnectivityStatus()) {
                            g = new G();
                            new GetErrors(context);
                            Exception_String EX = new Exception_String();
                            GetErrors.SendErrors("43", EX.ValueEX(e), g.VersionCode());
                            return null;
                        }
                        return null;
                    }
                }
            }
        }
    }

    public class ThreadFinish_Send extends Thread{

        @Override
        public void run() {
            Intent intent = new Intent();
            intent.setAction(MY_ACTION_E);
            intent.putExtra("MVE",Value);
            sendBroadcast(intent);
            stopSelf();
        }
    }
}

What can I do ?

1 Answers1

0

I think you should rewrite this, its horrible.

*Your naming is confusing (Error_Service and ErrorService, really?)

*You use unidentifiable variable names (what the heck is G g)

*You use AsyncTask.execute.get() on the main thread. You should almost never use .get- if you think you need to you're probably wrong. If you're on the main thread you're always wrong.

*You have a finally that starts a new thread just to fire an intent. Why? There is literally no possible reason to use a thread there.

*You have identical error handling code copy pasted in a dozen places.

This code is actually some of the worst I've seen in a long while. Junk it and start over.

WHile you're at it- the error handling code is your immediate problem. This is the error logging service. If it can't log another error, why would it be able to log its own? When it fails it needs to fail silently.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127