title: How to Make Retrofit API calls inside Service in Android
body: <p>I'm trying to retrieve data from database and display the result as a toast message (planning to do more after I get it working).However, it's giving me the error - "unexpected pending exception: java.lang.ArrayIndexOutOfBoundsException" and telling me the case is "        Call> call = apiServiceNotif.getNotifications(id, onReceive);" </p>
  
  <p>I'm calling the callback function inside [ onStartCommand ] like this:</p>
  
  <pre><code>@Override
  public int onStartCommand(Intent intent, int flags, int startId) {
        getMessage("1"); //for testing
        return START_STICKY;
  }
  </code></pre>
  
  <p>I've tested the callback function on another class and worked fine. It's my first time working with services so I'm honestly kinda lost. Any help is much appreciated.</p>
  
  <p>Here's my callback function:</p>
  
  <pre><code>public void getMessage(String id){
          ApiServiceMessage apiServiceMessage = ApiClient.getClient().create(ApiServiceMessage.class);
          Call&lt;List&lt;Messsages&gt;&gt; call = apiServiceMessage.getMessages(id);
          call.enqueue(new Callback&lt;List&lt;Messages&gt;&gt;() {
              @Override
              public void onResponse(Call&lt;List&lt;Messages&gt;&gt; call,
                                     Response&lt;List&lt;Messages&gt;&gt; response) {
                  List&lt;Messages&gt; messages= response.body();
  
                   for(Messages m: messages){
                       Toast(getApplicationContext(), m.getMessageTitle + " " + m.getMessageContent, Toast.LENGTH_LONG).show();
                   }
                  }
              }
  
              @Override
              public void onFailure(Call&lt;List&lt;Messages&gt;&gt; call, Throwable t) {
                  Toast.makeText(getApplicationContext(), "Failed to load." + t, Toast.LENGTH_LONG).show();
  
  
              }
          });
      }
  </code></pre>