title: Square Retrofit repeats network calls
body: <p>I am using the Square's Retrofit library to handle networking for an application. I have noticed that the library makes repeated HTTP calls for no apparent reason and I do not really know why.</p>
  
  <p>I have verified that the call to the interface method is executed only once from the android activity (ie.userAPI.getUserByFacebookIdAsync()).</p>
  
  <p>Has anyone experimented this too? Perhaps has something to do with the OKhttp library setup?</p>
  
  <p>Any help much appreciated.</p>
  
  <p>RETROFIT LOGGING</p>
  
  <pre><code>08-18 21:25:56.895  30331-30372/com.package.name D/Retrofit﹕ ---&gt; HTTP GET http://server-instance:8080/server-app//user/facebook?facebookId=00000000
  08-18 21:25:56.895  30331-30372/com.package.name D/Retrofit﹕ FacebookId: 00000000
  08-18 21:25:56.895  30331-30372/com.package.name D/Retrofit﹕ FacebookToken: XXXXX
  08-18 21:25:56.895  30331-30372/com.package.name D/Retrofit﹕ ---&gt; END HTTP (no body)
  
  08-18 21:25:57.434  30331-30372/com.package.name D/Retrofit﹕ ---&gt; HTTP GET http://server-instance:8080/server-app//user/facebook?facebookId=00000000
  08-18 21:25:57.434  30331-30372/com.package.name D/Retrofit﹕ FacebookId: 00000000
  08-18 21:25:57.434  30331-30372/com.package.name D/Retrofit﹕ FacebookToken: XXXXX
  08-18 21:25:57.434  30331-30372/com.package.name D/Retrofit﹕ ---&gt; END HTTP (no body)
  
  08-18 21:25:57.441  30331-30359/com.package.name D/Retrofit﹕ ---&gt; HTTP GET http://server-instance:8080/server-app/user/facebook?facebookId=00000000
  08-18 21:25:57.441  30331-30359/com.package.name D/Retrofit﹕ FacebookId: 00000000
  08-18 21:25:57.441  30331-30359/com.package.name D/Retrofit﹕ FacebookToken: XXXXX
  08-18 21:25:57.441  30331-30359/com.package.name D/Retrofit﹕ ---&gt; END HTTP (no body)
  
  08-19 03:05:59.448  10069-10345/com.package.name D/Retrofit﹕ &lt;--- HTTP 200 http://server-instance:8080/server-app/user/facebook?facebookId=00000000 (770ms)
  08-19 03:05:59.448  10069-10345/com.package.name D/Retrofit﹕ : HTTP/1.1 200 OK
  08-19 03:05:59.448  10069-10345/com.package.name D/Retrofit﹕ Content-Type: application/json
  08-19 03:05:59.448  10069-10345/com.package.name D/Retrofit﹕ Date: Tue, 19 Aug 2014 02:05:59 GMT
  08-19 03:05:59.448  10069-10345/com.package.name D/Retrofit﹕ OkHttp-Received-Millis: 1408413959446
  08-19 03:05:59.448  10069-10345/com.package.name D/Retrofit﹕ OkHttp-Response-Source: CONDITIONAL_CACHE 200
  08-19 03:05:59.448  10069-10345/com.package.name D/Retrofit﹕ OkHttp-Selected-Protocol: http/1.1
  08-19 03:05:59.448  10069-10345/com.package.name D/Retrofit﹕ OkHttp-Sent-Millis: 1408413959001
  08-19 03:05:59.448  10069-10345/com.package.name D/Retrofit﹕ Server: Apache-Coyote/1.1
  08-19 03:05:59.448  10069-10345/com.package.name D/Retrofit﹕ Transfer-Encoding: chunked
  08-19 03:05:59.456  10069-10345/com.package.name D/Retrofit﹕ {"id":4,"facebookId":"00000000","gender":"male","city":"Barcelona","country":"Spain","firstName":"John","premium":false}
  08-19 03:05:59.456  10069-10345/com.package.name D/Retrofit﹕ &lt;--- END HTTP (390-byte body)
  </code></pre>
  
  <p>The responses to the extra 2 calls are received too, they are simply not included in the logging sample.</p>
  
  <p>NETWORK CALL (ACTIVITY CODE INCLUDED FURTHER BELOW)</p>
  
  <pre><code>userAPI.getUserByFacebookIdAsync(user.getId(), new Callback&lt;User&gt;() {
      @Override
      public void success(User user, Response response) {}
      @Override
      public void failure(RetrofitError error) {}
  </code></pre>
  
  <p>RETROFIT API INTERFACE METHOD</p>
  
  <pre><code>@GET("/user/facebook")
  void getUserByFacebookIdAsync(@Query("facebookId") String facebookId, Callback&lt;User&gt; cb);
  </code></pre>
  
  <p>RETROFIT API HANDLER</p>
  
  <pre><code>public class APIHandler {
  static RequestInterceptor requestInterceptor = new RequestInterceptor() {
      @Override
      public void intercept(RequestFacade request) {
          request.addHeader("FacebookId", Constants.FACEBOOK_ID);
          request.addHeader("FacebookToken", Constants.FACEBOOK_TOKEN);
      }
  };
  private static RestAdapter restAdapter;
  
  private static RestAdapter getRestAdapter() {
      if (restAdapter == null) {
          restAdapter = new RestAdapter.Builder().
                  setRequestInterceptor(requestInterceptor).
                  setLogLevel(RestAdapter.LogLevel.FULL).
                  setClient(Constants.OK_CLIENT).
                  setEndpoint(BaseAPI.getInstance().BASE_URL).build();
      }
      return restAdapter;
  }
  
  public static UserAPI getUserAPI() {
      UserAPI userAPI = null;
      try {
          if (restAdapter == null) {
              restAdapter = getRestAdapter();
          }
          userAPI = restAdapter.create(UserAPI.class);
      } catch (Exception e) {
          e.printStackTrace();
      }
      return userAPI;
  }}
  </code></pre>
  
  <p>OKHTTP SETUP (ON APP STARTUP)</p>
  
  <pre><code>int cacheSize = 10 * 1024 * 1024; // 10 MiB
  File cacheDirectory = new File(getApplicationContext().getCacheDir().getAbsolutePath(), "HttpCache");
  Cache cache = null;
  try {
      cache = new Cache(cacheDirectory, cacheSize);
  } catch (IOException e) {
      e.printStackTrace();
  }
  OkHttpClient okHttpClient = new OkHttpClient();
  okHttpClient.setCache(cache);
  Constants.OK_CLIENT = new OkClient(okHttpClient);
  </code></pre>
  
  <p>ANDROID ACTIVITY</p>
  
  <pre><code>public class CurrentUserProfileActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.current_user_profile_activity_layout);
  
      userAPI = APIHandler.getUserAPI();
      clubAPI = APIHandler.getClubAPI();
      visitAPI = APIHandler.getVisitAPI();
  
      picasso = PabloPicasso.withDebug(this);
  
      userProfilePicture = (ImageView) findViewById(R.id.userProfilePicture);
  
      makeMeRequest(Session.getActiveSession());
  
      userProfilePicture.setOnTouchListener(new View.OnTouchListener() {
  
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              Intent intent = new Intent(getBaseContext(), PictureViewerActivity.class);
              intent.putExtra("facebookId", mUser.getFacebookId());
              startActivity(intent);
              return false;
          }
      });
      }
  
      private void makeMeRequest(final Session session) {
         Request request = Request.newMeRequest(session,
              new Request.GraphUserCallback() {
                  @Override
                  public void onCompleted(GraphUser user, com.facebook.Response response) {
                      // If the response is successful
                      if (session == Session.getActiveSession()) {
                          if (user != null) {
                              userAPI.getUserByFacebookIdAsync(user.getId(), new Callback&lt;User&gt;() {
                                  @Override
                                  public void success(User user, Response response) {
                                      mUser = user;
                                 }
                                  @Override
                                  public void failure(RetrofitError error) {
                                      Log.d(TAG, "userApi call to get User object from server failed!");
                                  }
                              });
                          }
                      }
                      if (response.getError() != null) {
                          Intent introActivityIntent = new Intent(CurrentUserProfileActivity.this, IntroActivity.class);
                          introActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                          startActivity(introActivityIntent);
                          finish();
                      }
                  }
              }
         );
         request.executeAsync();
      }}
  </code></pre>