title: I/O error: No route to host; nested exception is java.net.NoRouteToHostException: No route to host
body: <p>I am getting the above exception when trying to consume restservice using resttemplate in android. the following is the piece of code.</p>
  
  <pre><code> private class HttpRequestTask extends AsyncTask&lt;Void, Void, ShiftPlannerModel[]&gt; {
      @Override
      protected ShiftPlannerModel[] doInBackground(Void... params) {
          try {
              RestTemplate restTemplate = new RestTemplate();
              restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
              ResponseEntity&lt;ShiftPlannerModel[]&gt; greeting = restTemplate.getForEntity(URL, ShiftPlannerModel[].class);
              Log.i(greeting.getBody().toString(), "doInBackground: ");
              return greeting.getBody();
          } catch (Exception e) {
              Log.e("doInBackgrouExcp", e.getMessage(), e);
          }
  
          return null;
      }
  </code></pre>
  
  <p>NOTE: rest service is up and running in local machine</p>
  
  <p><a href="https://i.stack.imgur.com/wHPUT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wHPUT.png" alt="enter image description here"></a></p>
  
  <p>The following is the full stack trace:
  4-19 00:56:21.260 11743-11794/com.example.admin.myapplication E/doInBackgrouExcp: I/O error: No route to host; nested exception is java.net.NoRouteToHostException: No route to host
      org.springframework.web.client.ResourceAccessException: I/O error: No route to host; nested exception is java.net.NoRouteToHostException: No route to host
          at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:491)
          at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
          at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:259)
          at com.example.admin.myapplication.MainActivity$HttpRequestTask.doInBackground(MainActivity.java:132)
          at com.example.admin.myapplication.MainActivity$HttpRequestTask.doInBackground(MainActivity.java:126)
          at android.os.AsyncTask$2.call(AsyncTask.java:304)
          at java.util.concurrent.FutureTask.run(FutureTask.java:237)
          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
          at java.lang.Thread.run(Thread.java:762)
       Caused by: java.net.NoRouteToHostException: No route to host
          at java.net.PlainSocketImpl.socketConnect(Native Method)
          at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:334)
          at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
          at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
          at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
          at java.net.Socket.connect(Socket.java:586)
          at com.android.okhttp.internal.Platform.connectSocket(Platform.java:113)
          at com.android.okhttp.Connection.connectSocket(Connection.java:1455)
          at com.android.okhttp.Connection.connect(Connection.java:1413)
          at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:1700)
          at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:133)
          at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:466)
          at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:371)
          at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:503)
          at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:130)
          at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:72)
          at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:46)
          at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:63)
          at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:476)
          at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439) 
          at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:259) 
          at com.example.admin.myapplication.MainActivity$HttpRequestTask.doInBackground(MainActivity.java:132) 
          at com.example.admin.myapplication.MainActivity$HttpRequestTask.doInBackground(MainActivity.java:126) 
          at android.os.AsyncTask$2.call(AsyncTask.java:304) 
          at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
          at java.lang.Thread.run(Thread.java:762) 
  04-19 00:56:21.261 11743-11743/com.example.admin.myapplication D/AndroidRuntime: Shutting down VM
  04-19 00:56:21.261 11743-11743/com.example.admin.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.admin.myapplication, PID: 11743
      java.lang.NullPointerException: Attempt to get length of null array
          at com.example.admin.myapplication.MainActivity$HttpRequestTask.onPostExecute(MainActivity.java:151)
          at com.example.admin.myapplication.MainActivity$HttpRequestTask.onPostExecute(MainActivity.java:126)
          at android.os.AsyncTask.finish(AsyncTask.java:660)
          at android.os.AsyncTask.-wrap1(AsyncTask.java)
          at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:154)
          at android.app.ActivityThread.main(ActivityThread.java:6776)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)</p>