title: Android how to set time out on delayed response for asynchronous get method
body: <p>I want to set time out ,if the data couldn't get in 15 seconds. i used Asynchronous method to get the data from the server. i tried something which is shown in the code.i use  <code>get(15000, TimeUnit.MILLISECONDS);</code>,it wait for 15 seconds and loose the connection still it able to get the data in 15 seconds.I also used timer in <code>doInBackground()</code> , but it have same issue. <strong>I want to put timeout for 15 seconds ,if the data cann't get in time than it show a dialogue for 'connection problem'.</strong> please help me .thanks in advance. </p>
  
  <p><strong>Class</strong>  </p>
  
  <pre><code> class GetTAsk extends AsyncTask&lt;String, String, String&gt;
  {
      @Override
      protected String doInBackground(String... uri) {
  
         //HttpParams params = new BasicHttpParams();
         //HttpConnectionParams.setConnectionTimeout(params,15000);
         //HttpConnectionParams.setSoTimeout(params,15000);
         //DefaultHttpClient client = new DefaultHttpClient(params);
  
          HttpClient httpClient = new DefaultHttpClient();
          final HttpGet httpGet = new HttpGet(uri[0]);
         //httpGet.setParams(params);
          HttpResponse response = null;
          String responseString = null;
          requestTime =  new Date();
          Log.i(l,"URL: "+uri[0]);
          try {
  
              response = httpClient.execute(httpGet);
              Log.i(l,""+response.getStatusLine().toString());
              StatusLine statusLine = response.getStatusLine();
              if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                  ByteArrayOutputStream out = new ByteArrayOutputStream();
                  response.getEntity().writeTo(out);
                  out.close();
                  responseString = out.toString();
              } else{
                  response.getEntity().getContent().close();
                  throw new IOException(statusLine.getReasonPhrase());
              }
          } catch (ClientProtocolException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          /*new Timer().schedule(new TimerTask() {            
              @Override
              public void run() {
                  // TODO Auto-generated method stub
                  httpGet.abort();
                  Log.e("LOG","request aborted");
                  progressBar.cancel();
                  cancel();
              }
          },1000);
          */
          try {
              get(1000, TimeUnit.MILLISECONDS);
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (ExecutionException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (TimeoutException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          if (isCancelled()) {
              Log.e("LOG","the task is cancelled");
              progressBar.cancel();
          }
          return responseString;
      }
  
      @Override
      protected void onPostExecute(String result) {
          super.onPostExecute(result);
  
      }
  }
  </code></pre>