title: HttpHostConnectException connection refused ConnectException ErrnoException
body: <p>I have a network processes in my application where this exception occurs and I don't know how to fix this. I already applied certain permissions in my manifest, and here is the permissions I applied for my whole app:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.RECORD_AUDIO" /&gt;
  &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
  </code></pre>
  
  <p>and the URI I am using is from our server in its global IP address already. It works fine in the browser but in the app, this exception comes out. Here is the stack trace:</p>
  
  <pre><code>        08-12 06:15:36.234: W/System.err(862): org.apache.http.conn.HttpHostConnectException: Connection to http://xxx.xx.xxx.xxx:8080 refused
      08-12 06:15:36.324: W/System.err(862):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
      08-12 06:15:36.494: W/System.err(862):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
      08-12 06:15:36.594: W/System.err(862):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
      08-12 06:15:36.785: W/System.err(862):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
      08-12 06:15:36.904: W/System.err(862):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
      08-12 06:15:37.014: W/System.err(862):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
      08-12 06:15:37.164: W/System.err(862):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
      08-12 06:15:37.194: W/System.err(862):  at com.android.app.util.Utility.getRequest(Utility.java:42)
      08-12 06:15:37.284: W/System.err(862):  at com.android.app.LoginActivity$LoginTask.doInBackground(LoginActivity.java:440)
      08-12 06:15:37.404: W/System.err(862):  at com.android.app.LoginActivity$LoginTask.doInBackground(LoginActivity.java:1)
      08-12 06:15:37.534: W/System.err(862):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
      08-12 06:15:37.744: W/System.err(862):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
      08-12 06:15:37.804: W/System.err(862):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
      08-12 06:15:37.944: W/System.err(862):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
      08-12 06:15:38.044: W/System.err(862):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
      08-12 06:15:38.164: W/System.err(862):  at java.lang.Thread.run(Thread.java:856)
      08-12 06:15:38.364: W/System.err(862): Caused by: java.net.ConnectException: failed to connect to /xxx.xx.xxx.xxx (port 8080): connect failed: ETIMEDOUT (Connection timed out)
      08-12 06:15:38.394: W/System.err(862):  at libcore.io.IoBridge.connect(IoBridge.java:114)
      08-12 06:15:38.664: W/System.err(862):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
      08-12 06:15:38.845: W/System.err(862):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
      08-12 06:15:38.894: W/System.err(862):  at java.net.Socket.connect(Socket.java:842)
      08-12 06:15:39.085: W/System.err(862):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
      08-12 06:15:39.154: W/System.err(862):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
      08-12 06:15:39.184: W/System.err(862):  ... 15 more
      08-12 06:15:39.244: W/System.err(862): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
      08-12 06:15:39.464: W/System.err(862):  at libcore.io.Posix.connect(Native Method)
      08-12 06:15:39.774: W/System.err(862):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
      08-12 06:15:39.804: W/System.err(862):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
      08-12 06:15:39.894: W/System.err(862):  at libcore.io.IoBridge.connect(IoBridge.java:112)
      08-12 06:15:39.984: W/System.err(862):  ... 20 more
  </code></pre>
  
  <p>Any help will be appreciated. Thank you.</p>
  
  <p><strong>EDIT:</strong>
  Okay, so here's my Utility:</p>
  
  <pre><code>public static String getRequest(String path){
      String url = http://xxx.xx.xxx.xxx:8080/SampleApi/sample-apis; // Global URL of the API
      String username = "sampleuname";
      String password = "samplepass";
  
      CredentialsProvider credProvider = new BasicCredentialsProvider();
      credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
              new UsernamePasswordCredentials(username, password));
  
      try{
          DefaultHttpClient httpClient = new DefaultHttpClient();
          httpClient.setCredentialsProvider(credProvider);
          HttpGet httpGet = new HttpGet(url + path);
          HttpResponse httpResponse = httpClient.execute(httpGet); // Okay, so the stack trace is pointing in this line
  
          HttpEntity httpEntity = httpResponse.getEntity();
          if(httpEntity != null){ 
              inStream = httpEntity.getContent(); 
              String jsonString = convertStreamToString(inStream);
              return jsonString;
          }
      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      } catch (ClientProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      return "Request NULL";
  }
  </code></pre>
  
  <p>and my calling Asynctask:</p>
  
  <pre><code>public class LoginTask extends AsyncTask&lt;String, Void, String&gt; {
  
      @Override
      protected String doInBackground(String... path) {
          Log.d(Constant.TAG_LOGIN, path[0]);
  
          String apiRequestReturn = Utility.getRequest(path[0]); // So here's the other line pointed by the stack trace where I called Utility
          if (apiRequestReturn.equals("")) {
              Log.d(Constant.TAG_LOGIN, "WebService request is null");
              return null;
          } else {
              Log.d(Constant.TAG_LOGIN, "WebService request has data");
              return apiRequestReturn;
          }
      }
  
      @Override
      protected void onPostExecute(String result) {
          super.onPostExecute(null);
  
          //JSON PARSING
      }
  }
  </code></pre>