title: Http Request - how to?
body: <p>I have a service for http requst in web every 10 sec it run corectly and work perfectly but after a hour the app show erorr  <strong>app unfortunately stop</strong> and app not work here is my service. please see and Suggest me how it Resolve ?
  Here is <strong>Server.java</strong></p>
  
  <pre><code>public class Server extends Service {
  
      private static String TAG = "allstart";
      private Handler handler;
      private Runnable runnable;
      private final int runTime = 5000;
  
      @Override
      public void onCreate() {
          super.onCreate();
          handler = new Handler();
          runnable = new Runnable() {
              @Override
              public void run() {if (!isOnline(Server.this)){
               return;}
  
                  String output=null;
                  String url="http://localhost:8080/feed/appfeed.php";
                  try {
                      output = new RequestTask().execute(url).get();
                  } catch (ExecutionException e) {
                  } catch (InterruptedException e) {
                  }
  
                  String withoutitlne = output.replaceAll("\\r|\\n", "");
                  String input = withoutitlne.replace(" ", "");
                  if (input != null &amp;&amp; !input.isEmpty()) {
                  String[] separated = output.split("\\*");
                  String nb= separated[1].toString(); 
                      String am= separated[2].toString();
                      String oper= separated[0].toString(); 
                      String nnb = nb.replace(" ", "");
                      if (oper.indexOf("GP") &gt;= 0) {
  
                          Intent newintent = new Intent(getApplicationContext(), Recharge.class);
                          newintent.putExtra("rcnumber", nnb);
                          newintent.putExtra("rcamount", am);
                          newintent.putExtra("gpin", rb);
                          startService(newintent);
                      }               
                  }
                  handler.postDelayed(runnable, runTime);
              }
          };
          handler.post(runnable);
      }
  
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          Toast.makeText(getApplicationContext(), "Service Task destroyed", Toast.LENGTH_LONG).show();
          Intent myIntent = new Intent(getApplicationContext(), Server.class);
  
          PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0);
  
          AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
  
          Calendar calendar = Calendar.getInstance();
          calendar.setTimeInMillis(System.currentTimeMillis());
          calendar.add(Calendar.SECOND, 10);
          alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
  
      }
  
      @Override
      public void onTaskRemoved(Intent rootIntent) {
          super.onTaskRemoved(rootIntent);
  
          Intent myIntent = new Intent(getApplicationContext(), Server.class);
          PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0);
          AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
          Calendar calendar = Calendar.getInstance();
          calendar.setTimeInMillis(System.currentTimeMillis());
          calendar.add(Calendar.SECOND, 10);
          alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          return START_STICKY;
      }
  
      @SuppressWarnings("deprecation")
      @Override
      public void onStart(Intent intent, int startId) {
          super.onStart(intent, startId);
  
      }
  
      class RequestTask extends AsyncTask&lt;String, String, String&gt;{
  
          @Override
          protected String doInBackground(String... uri) {
              HttpClient httpclient = new DefaultHttpClient();
              String userAgent = "Mozilla/5.0 (Linux; Android "+getAndroidVersion()+"; "+getDeviceName()+" Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36";    
              httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent);
  
              HttpResponse response;
              String responseString = null;
              try {
                  response = httpclient.execute(new HttpGet(uri[0]));
                  StatusLine statusLine = response.getStatusLine();
  
                  if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                      ByteArrayOutputStream out = new ByteArrayOutputStream();
                      response.getEntity().writeTo(out);
                      responseString = out.toString();
                      out.close();
                  } else{
                      // Closes the connection.
                      response.getEntity().getContent().close();
                      throw new IOException(statusLine.getReasonPhrase());
                  }
              } catch (ClientProtocolException e) {
                  // TODO Handle problems..
              } catch (IOException e) {
                  // TODO Handle problems..
              }
              return responseString;
          }
  
          @Override
          protected void onPostExecute(String result) {
              super.onPostExecute(result);
              //Do anything with response..
          }    
      }
  
      public String getAndroidVersion() {
          String release = Build.VERSION.RELEASE;
          int sdkVersion = Build.VERSION.SDK_INT;
          return release ;
      }
  
      private boolean isOnline(Context mContext) {
          ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo netInfo = cm.getActiveNetworkInfo();
          if (netInfo != null &amp;&amp; netInfo.isConnectedOrConnecting()) {
              return true;
          }
          return false;
      }
  
      public String getDeviceName() {
          String manufacturer = Build.MANUFACTURER;
          String model = Build.MODEL;
          return capitalize(model);
      }
  
  
      private String capitalize(String s) {
          if (s == null || s.length() == 0) {
              return "";
          }
          char first = s.charAt(0);
          if (Character.isUpperCase(first)) {
              return s;
          } else {
              return Character.toUpperCase(first) + s.substring(1);
          }
      } 
  
      public static String getPref(String key, Context context) {
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
          return preferences.getString(key, null);
      }
  }
  </code></pre>
  
  <p>My target Device above 4.0 version.</p>