title: Apache httpclient android dealing with connection refused
body: <p>I'm working on an intentservice that will do a httpget to a server and retrieve a json string. </p>
  
  <p>I'm using the following code, which works aslong as the server is up:</p>
  
  <pre><code> private String getServerContentsJson() throws IOException {
      HttpClient httpclient = new DefaultHttpClient();
  
     HttpResponse  response = httpclient.execute(new HttpGet(url));
  
  
      StatusLine statusline = response.getStatusLine();
  
      if(statusline.getStatusCode() == HttpStatus.SC_OK){
  
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          response.getEntity().writeTo(out);
          String responseString = out.toString();
          out.close();
          return responseString;}
  
      else{
          //Closes the connection.
          response.getEntity().getContent().close();
          throw new IOException(statusline.getReasonPhrase());
  
      }
  </code></pre>
  
  <p>The problem is when the server is down, I want to deal with that properly, but it is unclear to me how I can do this. According to the documentation <code>httpclient.execute</code> will either return a proper response, or throw IOException or ClientProtocolException, but instead I'm getting <code>HttpHostConnectException: Connection to http://irrelevantURL refused</code> which doesn't seem to trace back to my code. The app then crashes. Any suggestions?</p>
  
  <p>Here's the full stack trace:</p>
  
  <pre><code>W/System.err﹕ org.apache.http.conn.HttpHostConnectException: Connection to http://irrelevantURL refused
  W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
  W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
  W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
  W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
  W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
  W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
  </code></pre>
  
  <p>edit: I guess this isn't related to server being down, but instead it refuse connection for some reason, the question remains though I still want to deal with this properly so that the app does not crash</p>