title: Creating a custom Callback for Retrofit
body: <p>I am using Retrofit and Otto in my app for web service calls. I want to assign an id for each request made so I made a custom Retrofit callback like this</p>
  
  <pre><code>public class RetrofitCallback&lt;T&gt; implements Callback&lt;T&gt; {
  
      private static int _idCounter = 1;
  
      public int requestId;
      private Bus eventBus;
  
      public RetrofitCallback(Bus eventBus){
          this.requestId = _idCounter;
          this.eventBus = eventBus;
          _idCounter++;
      }
  
      @Override
      public void success(T data, Response response) {
          // problem.. data is not the type I expected. please see below
          RetrofitRequestSuccess&lt;T&gt; message = new RetrofitRequestSuccess&lt;&gt;(requestId, 
             data, response);
          eventBus.post(message);
      }
  
      @Override
      public void failure(RetrofitError error) {
          RetrofitRequestFailed message = new RetrofitRequestFailed(requestId, error);
          eventBus.post(message);
      }
  }
  </code></pre>
  
  <p>The RetrofitRequestSuccess class</p>
  
  <pre><code>public class RetrofitRequestSuccess&lt;T&gt; {
      public T data;
      public int requestId;
      public Response response;
  
      public RetrofitRequestSuccess(int requestId, T data, Response response) {
          this.requestId = requestId;
          this.data = data;
          this.response = response;
      }
  }
  </code></pre>
  
  <p>I then use the callback as follows</p>
  
  <pre><code>int requestId = 0; // activity level scope
  
  RestAdapter restApiAdapter = new RestAdapter.Builder().setEndpoint(ENDPOINT).build();
  RestAPI restClient = restApiAdapter.create(RestAPI.class);
  RetrofitCallback&lt;List&lt;Order&gt;&gt; callback = new RetrofitCallback&lt;&gt;(BusProvider.getBus());
  restClient.getOrders(callback);
  requestId = callback.requestId;
  </code></pre>
  
  <p>Otto subscribed method</p>
  
  <pre><code>@Subscribe
  public void onHttpResponseReceived(RetrofitRequestSuccess message) {
      if(message.requestId == requestId) {
          List&lt;Order&gt; ordersList = (ArrayList&lt;Order&gt;) message.data;
      }
  }
  </code></pre>
  
  <p>The problem is I never get List of <code>Order</code> objects in <code>message.data</code>. No matter what type I pass when I create the callback, the <code>message.data</code> always contains a List of Gson's <code>LinkedTreeMap</code> objects. How can I make it to return the correct type? Any help would be greatly appreciated!</p>