2016-04-29 6 views
0

URLにエラーを送信するサービスがありますが、このサービスは他のサービスでうまく動作しますが、自分自身でエラーが発生したときに非常に悪いループに落ちます。Android - サービスがループする?

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(); 
     } 
    } 
} 

どうすればよいですか?

答えて

1

私はあなたがこれを、恐ろしい書き直すべきだと思います。

*あなたの命名は、(本当に、Error_ServiceとErrorService?)を混乱さ

あなたは識別できない変数名を

(一体G gは何である)を使用*あなたは上AsyncTask.execute.get()を使用*メインスレッドおそらく間違っている必要があると思う場合は、ほとんど使用しないでください。あなたがメインスレッド上にいる場合、あなたは常に間違っています。

*あなたは最終的に意図を起こすために新しいスレッドを開始します。どうして?文字通りそこにスレッドを使用する理由はありません。

*同じエラー処理コードのコピーを12か所に貼り付けました。

このコードは、実際に私が長い間見た中で最悪のものです。それをつぶしてやり直してください。

エラー処理コードはすぐに問題になります。これはエラーログサービスです。別のエラーをログに記録できない場合は、なぜ自分のログを記録できるのでしょうか?それが失敗すると、それは黙って失敗する必要があります。