Skip to content

Instantly share code, notes, and snippets.

@JonathonFry
Created January 18, 2017 10:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonathonFry/6f507f6f39e3605e3047e359bce92314 to your computer and use it in GitHub Desktop.
Save JonathonFry/6f507f6f39e3605e3047e359bce92314 to your computer and use it in GitHub Desktop.
package test.iamjonfry.com.interceptortest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import java.io.File;
import java.io.IOException;
import java.util.List;
import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;
public class MainActivity extends AppCompatActivity {
private static final String API_URL = "https://api.github.com";
private static final String SAMPLE_RESPONSE = "[{\"login\":\"JakeWharton\",\"id\":66577,\"avatar_url\":\"https://avatars.githubusercontent.com/u/66577?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/JakeWharton\",\"html_url\":\"https://github.com/JakeWharton\",\"followers_url\":\"https://api.github.com/users/JakeWharton/followers\",\"following_url\":\"https://api.github.com/users/JakeWharton/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/JakeWharton/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/JakeWharton/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/JakeWharton/subscriptions\",\"organizations_url\":\"https://api.github.com/users/JakeWharton/orgs\",\"repos_url\":\"https://api.github.com/users/JakeWharton/repos\",\"events_url\":\"https://api.github.com/users/JakeWharton/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/JakeWharton/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":816},{\"login\":\"swankjesse\",\"id\":133019,\"avatar_url\":\"https://avatars.githubusercontent.com/u/133019?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/swankjesse\",\"html_url\":\"https://github.com/swankjesse\",\"followers_url\":\"https://api.github.com/users/swankjesse/followers\",\"following_url\":\"https://api.github.com/users/swankjesse/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/swankjesse/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/swankjesse/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/swankjesse/subscriptions\",\"organizations_url\":\"https://api.github.com/users/swankjesse/orgs\",\"repos_url\":\"https://api.github.com/users/swankjesse/repos\",\"events_url\":\"https://api.github.com/users/swankjesse/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/swankjesse/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":183},{\"login\":\"pforhan\",\"id\":331925,\"avatar_url\":\"https://avatars.githubusercontent.com/u/331925?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/pforhan\",\"html_url\":\"https://github.com/pforhan\",\"followers_url\":\"https://api.github.com/users/pforhan/followers\",\"following_url\":\"https://api.github.com/users/pforhan/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/pforhan/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/pforhan/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/pforhan/subscriptions\",\"organizations_url\":\"https://api.github.com/users/pforhan/orgs\",\"repos_url\":\"https://api.github.com/users/pforhan/repos\",\"events_url\":\"https://api.github.com/users/pforhan/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/pforhan/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":48},{\"login\":\"eburke\",\"id\":63761,\"avatar_url\":\"https://avatars.githubusercontent.com/u/63761?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/eburke\",\"html_url\":\"https://github.com/eburke\",\"followers_url\":\"https://api.github.com/users/eburke/followers\",\"following_url\":\"https://api.github.com/users/eburke/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/eburke/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/eburke/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/eburke/subscriptions\",\"organizations_url\":\"https://api.github.com/users/eburke/orgs\",\"repos_url\":\"https://api.github.com/users/eburke/repos\",\"events_url\":\"https://api.github.com/users/eburke/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/eburke/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":36},{\"login\":\"dnkoutso\",\"id\":310370,\"avatar_url\":\"https://avatars.githubusercontent.com/u/310370?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/dnkoutso\",\"html_url\":\"https://github.com/dnkoutso\",\"followers_url\":\"https://api.github.com/users/dnkoutso/followers\",\"following_url\":\"https://api.github.com/users/dnkoutso/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/dnkoutso/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/dnkoutso/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/dnkoutso/subscriptions\",\"organizations_url\":\"https://api.github.com/users/dnkoutso/orgs\",\"repos_url\":\"https://api.github.com/users/dnkoutso/repos\",\"events_url\":\"https://api.github.com/users/dnkoutso/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/dnkoutso/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":26},{\"login\":\"edenman\",\"id\":1063557,\"avatar_url\":\"https://avatars.githubusercontent.com/u/1063557?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/edenman\",\"html_url\":\"https://github.com/edenman\",\"followers_url\":\"https://api.github.com/users/edenman/followers\",\"following_url\":\"https://api.github.com/users/edenman/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/edenman/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/edenman/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/edenman/subscriptions\",\"organizations_url\":\"https://api.github.com/users/edenman/orgs\",\"repos_url\":\"https://api.github.com/users/edenman/repos\",\"events_url\":\"https://api.github.com/users/edenman/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/edenman/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":24},{\"login\":\"loganj\",\"id\":18877,\"avatar_url\":\"https://avatars.githubusercontent.com/u/18877?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/loganj\",\"html_url\":\"https://github.com/loganj\",\"followers_url\":\"https://api.github.com/users/loganj/followers\",\"following_url\":\"https://api.github.com/users/loganj/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/loganj/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/loganj/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/loganj/subscriptions\",\"organizations_url\":\"https://api.github.com/users/loganj/orgs\",\"repos_url\":\"https://api.github.com/users/loganj/repos\",\"events_url\":\"https://api.github.com/users/loganj/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/loganj/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":17},{\"login\":\"rcdickerson\",\"id\":63143,\"avatar_url\":\"https://avatars.githubusercontent.com/u/63143?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/rcdickerson\",\"html_url\":\"https://github.com/rcdickerson\",\"followers_url\":\"https://api.github.com/users/rcdickerson/followers\",\"following_url\":\"https://api.github.com/users/rcdickerson/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/rcdickerson/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/rcdickerson/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/rcdickerson/subscriptions\",\"organizations_url\":\"https://api.github.com/users/rcdickerson/orgs\",\"repos_url\":\"https://api.github.com/users/rcdickerson/repos\",\"events_url\":\"https://api.github.com/users/rcdickerson/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/rcdickerson/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":14},{\"login\":\"rjrjr\",\"id\":1884445,\"avatar_url\":\"https://avatars.githubusercontent.com/u/1884445?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/rjrjr\",\"html_url\":\"https://github.com/rjrjr\",\"followers_url\":\"https://api.github.com/users/rjrjr/followers\",\"following_url\":\"https://api.github.com/users/rjrjr/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/rjrjr/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/rjrjr/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/rjrjr/subscriptions\",\"organizations_url\":\"https://api.github.com/users/rjrjr/orgs\",\"repos_url\":\"https://api.github.com/users/rjrjr/repos\",\"events_url\":\"https://api.github.com/users/rjrjr/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/rjrjr/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":13},{\"login\":\"kryali\",\"id\":174889,\"avatar_url\":\"https://avatars.githubusercontent.com/u/174889?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/kryali\",\"html_url\":\"https://github.com/kryali\",\"followers_url\":\"https://api.github.com/users/kryali/followers\",\"following_url\":\"https://api.github.com/users/kryali/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/kryali/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/kryali/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/kryali/subscriptions\",\"organizations_url\":\"https://api.github.com/users/kryali/orgs\",\"repos_url\":\"https://api.github.com/users/kryali/repos\",\"events_url\":\"https://api.github.com/users/kryali/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/kryali/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":9},{\"login\":\"holmes\",\"id\":59162,\"avatar_url\":\"https://avatars.githubusercontent.com/u/59162?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/holmes\",\"html_url\":\"https://github.com/holmes\",\"followers_url\":\"https://api.github.com/users/holmes/followers\",\"following_url\":\"https://api.github.com/users/holmes/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/holmes/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/holmes/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/holmes/subscriptions\",\"organizations_url\":\"https://api.github.com/users/holmes/orgs\",\"repos_url\":\"https://api.github.com/users/holmes/repos\",\"events_url\":\"https://api.github.com/users/holmes/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/holmes/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":8},{\"login\":\"adriancole\",\"id\":64215,\"avatar_url\":\"https://avatars.githubusercontent.com/u/64215?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/adriancole\",\"html_url\":\"https://github.com/adriancole\",\"followers_url\":\"https://api.github.com/users/adriancole/followers\",\"following_url\":\"https://api.github.com/users/adriancole/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/adriancole/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/adriancole/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/adriancole/subscriptions\",\"organizations_url\":\"https://api.github.com/users/adriancole/orgs\",\"repos_url\":\"https://api.github.com/users/adriancole/repos\",\"events_url\":\"https://api.github.com/users/adriancole/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/adriancole/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":8},{\"login\":\"swanson\",\"id\":56947,\"avatar_url\":\"https://avatars.githubusercontent.com/u/56947?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/swanson\",\"html_url\":\"https://github.com/swanson\",\"followers_url\":\"https://api.github.com/users/swanson/followers\",\"following_url\":\"https://api.github.com/users/swanson/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/swanson/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/swanson/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/swanson/subscriptions\",\"organizations_url\":\"https://api.github.com/users/swanson/orgs\",\"repos_url\":\"https://api.github.com/users/swanson/repos\",\"events_url\":\"https://api.github.com/users/swanson/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/swanson/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":7},{\"login\":\"crazybob\",\"id\":170788,\"avatar_url\":\"https://avatars.githubusercontent.com/u/170788?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/crazybob\",\"html_url\":\"https://github.com/crazybob\",\"followers_url\":\"https://api.github.com/users/crazybob/followers\",\"following_url\":\"https://api.github.com/users/crazybob/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/crazybob/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/crazybob/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/crazybob/subscriptions\",\"organizations_url\":\"https://api.github.com/users/crazybob/orgs\",\"repos_url\":\"https://api.github.com/users/crazybob/repos\",\"events_url\":\"https://api.github.com/users/crazybob/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/crazybob/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":6},{\"login\":\"danrice-square\",\"id\":1952897,\"avatar_url\":\"https://avatars.githubusercontent.com/u/1952897?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/danrice-square\",\"html_url\":\"https://github.com/danrice-square\",\"followers_url\":\"https://api.github.com/users/danrice-square/followers\",\"following_url\":\"https://api.github.com/users/danrice-square/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/danrice-square/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/danrice-square/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/danrice-square/subscriptions\",\"organizations_url\":\"https://api.github.com/users/danrice-square/orgs\",\"repos_url\":\"https://api.github.com/users/danrice-square/repos\",\"events_url\":\"https://api.github.com/users/danrice-square/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/danrice-square/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":5},{\"login\":\"JayNewstrom\",\"id\":713288,\"avatar_url\":\"https://avatars.githubusercontent.com/u/713288?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/JayNewstrom\",\"html_url\":\"https://github.com/JayNewstrom\",\"followers_url\":\"https://api.github.com/users/JayNewstrom/followers\",\"following_url\":\"https://api.github.com/users/JayNewstrom/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/JayNewstrom/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/JayNewstrom/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/JayNewstrom/subscriptions\",\"organizations_url\":\"https://api.github.com/users/JayNewstrom/orgs\",\"repos_url\":\"https://api.github.com/users/JayNewstrom/repos\",\"events_url\":\"https://api.github.com/users/JayNewstrom/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/JayNewstrom/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":5},{\"login\":\"Turbo87\",\"id\":141300,\"avatar_url\":\"https://avatars.githubusercontent.com/u/141300?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/Turbo87\",\"html_url\":\"https://github.com/Turbo87\",\"followers_url\":\"https://api.github.com/users/Turbo87/followers\",\"following_url\":\"https://api.github.com/users/Turbo87/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/Turbo87/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/Turbo87/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/Turbo87/subscriptions\",\"organizations_url\":\"https://api.github.com/users/Turbo87/orgs\",\"repos_url\":\"https://api.github.com/users/Turbo87/repos\",\"events_url\":\"https://api.github.com/users/Turbo87/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/Turbo87/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":5},{\"login\":\"NightlyNexus\",\"id\":4032667,\"avatar_url\":\"https://avatars.githubusercontent.com/u/4032667?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/NightlyNexus\",\"html_url\":\"https://github.com/NightlyNexus\",\"followers_url\":\"https://api.github.com/users/NightlyNexus/followers\",\"following_url\":\"https://api.github.com/users/NightlyNexus/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/NightlyNexus/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/NightlyNexus/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/NightlyNexus/subscriptions\",\"organizations_url\":\"https://api.github.com/users/NightlyNexus/orgs\",\"repos_url\":\"https://api.github.com/users/NightlyNexus/repos\",\"events_url\":\"https://api.github.com/users/NightlyNexus/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/NightlyNexus/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":4},{\"login\":\"ransombriggs\",\"id\":62452,\"avatar_url\":\"https://avatars.githubusercontent.com/u/62452?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/ransombriggs\",\"html_url\":\"https://github.com/ransombriggs\",\"followers_url\":\"https://api.github.com/users/ransombriggs/followers\",\"following_url\":\"https://api.github.com/users/ransombriggs/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/ransombriggs/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/ransombriggs/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/ransombriggs/subscriptions\",\"organizations_url\":\"https://api.github.com/users/ransombriggs/orgs\",\"repos_url\":\"https://api.github.com/users/ransombriggs/repos\",\"events_url\":\"https://api.github.com/users/ransombriggs/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/ransombriggs/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":4},{\"login\":\"Jawnnypoo\",\"id\":1459320,\"avatar_url\":\"https://avatars.githubusercontent.com/u/1459320?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/Jawnnypoo\",\"html_url\":\"https://github.com/Jawnnypoo\",\"followers_url\":\"https://api.github.com/users/Jawnnypoo/followers\",\"following_url\":\"https://api.github.com/users/Jawnnypoo/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/Jawnnypoo/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/Jawnnypoo/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/Jawnnypoo/subscriptions\",\"organizations_url\":\"https://api.github.com/users/Jawnnypoo/orgs\",\"repos_url\":\"https://api.github.com/users/Jawnnypoo/repos\",\"events_url\":\"https://api.github.com/users/Jawnnypoo/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/Jawnnypoo/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":4},{\"login\":\"artem-zinnatullin\",\"id\":967132,\"avatar_url\":\"https://avatars.githubusercontent.com/u/967132?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/artem-zinnatullin\",\"html_url\":\"https://github.com/artem-zinnatullin\",\"followers_url\":\"https://api.github.com/users/artem-zinnatullin/followers\",\"following_url\":\"https://api.github.com/users/artem-zinnatullin/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/artem-zinnatullin/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/artem-zinnatullin/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/artem-zinnatullin/subscriptions\",\"organizations_url\":\"https://api.github.com/users/artem-zinnatullin/orgs\",\"repos_url\":\"https://api.github.com/users/artem-zinnatullin/repos\",\"events_url\":\"https://api.github.com/users/artem-zinnatullin/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/artem-zinnatullin/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":3},{\"login\":\"codebutler\",\"id\":3827,\"avatar_url\":\"https://avatars.githubusercontent.com/u/3827?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/codebutler\",\"html_url\":\"https://github.com/codebutler\",\"followers_url\":\"https://api.github.com/users/codebutler/followers\",\"following_url\":\"https://api.github.com/users/codebutler/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/codebutler/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/codebutler/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/codebutler/subscriptions\",\"organizations_url\":\"https://api.github.com/users/codebutler/orgs\",\"repos_url\":\"https://api.github.com/users/codebutler/repos\",\"events_url\":\"https://api.github.com/users/codebutler/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/codebutler/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":3},{\"login\":\"icastell\",\"id\":1678605,\"avatar_url\":\"https://avatars.githubusercontent.com/u/1678605?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/icastell\",\"html_url\":\"https://github.com/icastell\",\"followers_url\":\"https://api.github.com/users/icastell/followers\",\"following_url\":\"https://api.github.com/users/icastell/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/icastell/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/icastell/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/icastell/subscriptions\",\"organizations_url\":\"https://api.github.com/users/icastell/orgs\",\"repos_url\":\"https://api.github.com/users/icastell/repos\",\"events_url\":\"https://api.github.com/users/icastell/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/icastell/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":3},{\"login\":\"jjNford\",\"id\":965425,\"avatar_url\":\"https://avatars.githubusercontent.com/u/965425?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/jjNford\",\"html_url\":\"https://github.com/jjNford\",\"followers_url\":\"https://api.github.com/users/jjNford/followers\",\"following_url\":\"https://api.github.com/users/jjNford/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/jjNford/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/jjNford/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/jjNford/subscriptions\",\"organizations_url\":\"https://api.github.com/users/jjNford/orgs\",\"repos_url\":\"https://api.github.com/users/jjNford/repos\",\"events_url\":\"https://api.github.com/users/jjNford/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/jjNford/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":3},{\"login\":\"f2prateek\",\"id\":843979,\"avatar_url\":\"https://avatars.githubusercontent.com/u/843979?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/f2prateek\",\"html_url\":\"https://github.com/f2prateek\",\"followers_url\":\"https://api.github.com/users/f2prateek/followers\",\"following_url\":\"https://api.github.com/users/f2prateek/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/f2prateek/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/f2prateek/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/f2prateek/subscriptions\",\"organizations_url\":\"https://api.github.com/users/f2prateek/orgs\",\"repos_url\":\"https://api.github.com/users/f2prateek/repos\",\"events_url\":\"https://api.github.com/users/f2prateek/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/f2prateek/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":3},{\"login\":\"koalahamlet\",\"id\":2353874,\"avatar_url\":\"https://avatars.githubusercontent.com/u/2353874?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/koalahamlet\",\"html_url\":\"https://github.com/koalahamlet\",\"followers_url\":\"https://api.github.com/users/koalahamlet/followers\",\"following_url\":\"https://api.github.com/users/koalahamlet/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/koalahamlet/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/koalahamlet/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/koalahamlet/subscriptions\",\"organizations_url\":\"https://api.github.com/users/koalahamlet/orgs\",\"repos_url\":\"https://api.github.com/users/koalahamlet/repos\",\"events_url\":\"https://api.github.com/users/koalahamlet/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/koalahamlet/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":3},{\"login\":\"alexgyori\",\"id\":1882366,\"avatar_url\":\"https://avatars.githubusercontent.com/u/1882366?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/alexgyori\",\"html_url\":\"https://github.com/alexgyori\",\"followers_url\":\"https://api.github.com/users/alexgyori/followers\",\"following_url\":\"https://api.github.com/users/alexgyori/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/alexgyori/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/alexgyori/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/alexgyori/subscriptions\",\"organizations_url\":\"https://api.github.com/users/alexgyori/orgs\",\"repos_url\":\"https://api.github.com/users/alexgyori/repos\",\"events_url\":\"https://api.github.com/users/alexgyori/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/alexgyori/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":2},{\"login\":\"benoitdion\",\"id\":573574,\"avatar_url\":\"https://avatars.githubusercontent.com/u/573574?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/benoitdion\",\"html_url\":\"https://github.com/benoitdion\",\"followers_url\":\"https://api.github.com/users/benoitdion/followers\",\"following_url\":\"https://api.github.com/users/benoitdion/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/benoitdion/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/benoitdion/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/benoitdion/subscriptions\",\"organizations_url\":\"https://api.github.com/users/benoitdion/orgs\",\"repos_url\":\"https://api.github.com/users/benoitdion/repos\",\"events_url\":\"https://api.github.com/users/benoitdion/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/benoitdion/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":2},{\"login\":\"xian\",\"id\":40298,\"avatar_url\":\"https://avatars.githubusercontent.com/u/40298?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/xian\",\"html_url\":\"https://github.com/xian\",\"followers_url\":\"https://api.github.com/users/xian/followers\",\"following_url\":\"https://api.github.com/users/xian/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/xian/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/xian/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/xian/subscriptions\",\"organizations_url\":\"https://api.github.com/users/xian/orgs\",\"repos_url\":\"https://api.github.com/users/xian/repos\",\"events_url\":\"https://api.github.com/users/xian/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/xian/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":2},{\"login\":\"dlew\",\"id\":514850,\"avatar_url\":\"https://avatars.githubusercontent.com/u/514850?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/dlew\",\"html_url\":\"https://github.com/dlew\",\"followers_url\":\"https://api.github.com/users/dlew/followers\",\"following_url\":\"https://api.github.com/users/dlew/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/dlew/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/dlew/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/dlew/subscriptions\",\"organizations_url\":\"https://api.github.com/users/dlew/orgs\",\"repos_url\":\"https://api.github.com/users/dlew/repos\",\"events_url\":\"https://api.github.com/users/dlew/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/dlew/received_events\",\"type\":\"User\",\"site_admin\":false,\"contributions\":2}]";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
network();
}
public void network() {
//setup cache
File httpCacheDirectory = new File(getCacheDir(), "responses");
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(httpCacheDirectory, cacheSize);
//add cache to the client
OkHttpClient client = new OkHttpClient.Builder().cache(cache)
.addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.build();
// Create a very simple REST adapter which points the GitHub API.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
// Create an instance of our GitHub API interface.
GitHub github = retrofit.create(GitHub.class);
// Create a call instance for looking up Retrofit contributors.
Call<List<Contributor>> call = github.contributors("square", "dagger");
// Fetch and print a list of the contributors to the library.
call.enqueue(new Callback<List<Contributor>>() {
@Override
public void onResponse(Call<List<Contributor>> call,
Response<List<Contributor>> response) {
for (Contributor contributor : response.body()) {
System.out.println(contributor.login + " (" + contributor.contributions + ")");
}
}
@Override
public void onFailure(Call<List<Contributor>> call, Throwable t) {
t.printStackTrace();
}
});
}
private static class Contributor {
final String login;
final int contributions;
public Contributor(String login, int contributions) {
this.login = login;
this.contributions = contributions;
}
}
interface GitHub {
@GET("/repos/{owner}/{repo}/contributors")
Call<List<Contributor>> contributors(@Path("owner") String owner,
@Path("repo") String repo);
}
public static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Response originalResponse;
try {
originalResponse = chain.proceed(chain.request());
} catch (IOException e) {
return new okhttp3.Response.Builder()
.request(chain.request())
.code(200)
.protocol(Protocol.HTTP_1_1)
.body(ResponseBody.create(MediaType.parse("application/json"),
SAMPLE_RESPONSE))
.build();
}
return originalResponse;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment