title: What are the Efficient and optimized ways of making an API call in android
body: <p>I am beginner in android, I am trying to call a <a href="http://openweathermap.org/current" rel="nofollow noreferrer">simple weather api</a> which returns the JSON response. </p>
  
  <p>I wanted to know what are the efficient ways to make the api call and from where I can study those ways. </p>
  
  <p>I have tried implementing it using Jackson:</p>
  
  <p><strong>WebService.java</strong> (copy paste from some link)</p>
  
  <pre><code>public class WebService {
  
      public &lt;T&gt; T get(String url, T object) throws Exception {
          RestTemplate restTemplate = new RestTemplate();
          HttpHeaders headers = new HttpHeaders();
          headers.set("x-system-code", "mobile");
          headers.set("x-server-api-password", "mobile");
          headers.set("x-server-api-key", "mobile");
          headers.set("Connection", "Close");
          HttpEntity entity = new HttpEntity("parameters", headers);
          restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
          return (T) restTemplate.exchange(url, HttpMethod.GET, entity, object.getClass()).getBody();
      }
  
      public &lt;T&gt; T get(String url, T object, Map uriVariables) throws Exception {
          RestTemplate restTemplate = new RestTemplate();
          HttpHeaders headers = new HttpHeaders();
          headers.set("Connection", "Close");
          HttpEntity entity = new HttpEntity("parameters", headers);
          restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
          return (T) restTemplate.exchange(url, HttpMethod.GET, entity, object.getClass(), uriVariables).getBody();
      }
  
  
      public &lt;T&gt; T post(String url, Object request, T object) throws Exception {
          RestTemplate restTemplate = new RestTemplate();
          HttpHeaders headers = new HttpHeaders();
          headers.set("x-system-code", "mobile");
          headers.set("x-server-api-password", "mobile");
          headers.set("x-server-api-key", "mobile");
          headers.set("Connection", "Close");
          HttpEntity entity = new HttpEntity(request, headers);
          restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
          return (T) restTemplate.exchange(url, HttpMethod.POST, entity, object.getClass()).getBody();
      }
  
      public void put(String url, Object request) {
          try {
              RestTemplate restTemplate = new RestTemplate();
              restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
              restTemplate.put(url, request);
  
          } catch (Exception e) {
              Log.e("WebService - put", e.getMessage(), e);
          }
      }
  
      public void delete(String url, Object request) {
          try {
              RestTemplate restTemplate = new RestTemplate();
              restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
              restTemplate.delete(url);
          } catch (Exception e) {
              Log.e("WebService - delete", e.getMessage(), e);
          }
      }
  }`
  </code></pre>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      WebService webService=new WebService();
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          GetWeather getWeather=new GetWeather();
          getWeather.execute();
  
      }
  
      public class GetWeather extends AsyncTask&lt;URL, Integer, response&gt;{
  
          String url;
          public GetWeather(){
              url="http://api.openweathermap.org/data/2.5/weather?q=Gurgaon&amp;units=metric&amp;appid=94d605bef2c15307c23f65a326d*****";
          }
  
  
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
          }
          @Override
          protected response doInBackground(URL... params) {
              try {
                  response weatherResponse= webService.get(url,new response());
              } catch (Exception e) {
                  e.printStackTrace();
              }
              return null;
          }
  
          @Override
          protected void onPostExecute(response response) {
              super.onPostExecute(response);
              Log.i("response", response.toString());
          }
      }
  }
  </code></pre>
  
  <p><strong>Error</strong> </p>
  
  <blockquote>
    <p>org.springframework.web.client.ResourceAccessException: I/O error: unexpected end of stream on Connection{api.openweathermap.org:80, proxy=DIRECT hostAddress=128.199.109.89 cipherSuite=none protocol=http/1.1} (recycle count=0); nested exception is java.io.IOException: unexpected end of stream on Connection{api.openweathermap.org:80, proxy=DIRECT hostAddress=128.199.109.89 cipherSuite=none protocol=http/1.1} (recycle count=0)
    01-04 07:21:51.536 21098-21137/com.example.rajatgupta.weatherapp W/System.err:     at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:491)</p>
  </blockquote>
  
  <p>Please tell me what am I doing wrong, or any other way to make API hit. </p>