retrofit.Retrofit类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(193)

本文整理了Java中retrofit.Retrofit类的一些代码示例,展示了Retrofit类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Retrofit类的具体详情如下:
包路径:retrofit.Retrofit
类名称:Retrofit

Retrofit介绍

暂无

代码示例

代码示例来源:origin: yigit/dev-summit-architecture-demo

@Provides
  @Singleton
  public ApiService apiService(DemoConfig demoConfig) {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return new Retrofit.Builder()
        .baseUrl(demoConfig.getApiUrl())
        .addConverterFactory(JacksonConverterFactory.create(mapper)).build()
        .create(ApiService.class);
  }
}

代码示例来源:origin: stackoverflow.com

Call<BasicResponse> call = service.loadRepo();
call.enqueue(new Callback<BasicResponse>() {
  @Override
  public void onResponse(Response<BasicResponse> response, Retrofit retrofit) {
    if (response != null && !response.isSuccess() && response.errorBody() != null) {
      Converter<ResponseBody, BasicResponse> errorConverter =
        retrofit.responseConverter(BasicResponse.class, new Annotation[0]);
      BasicResponse error = errorConverter.convert(response.errorBody());
      //DO ERROR HANDLING HERE
      return;
    }
    RegistrationResponse registrationResponse = response.body();
    //DO SUCCESS HANDLING HERE
  }

  @Override
  public void onFailure(Throwable t) {
    //DO NETWORK ERROR HANDLING HERE
  }
});

代码示例来源:origin: stackoverflow.com

@Override
public void onResponse(Response<T> response, Retrofit retrofit) {
  List<Interceptor> data = retrofit.client().interceptors();
  if (data != null && data.size() > 0) {
    for (int i = 0; i < data.size(); i++) {
      if (data.get(i) instanceof LoggingInterceptor) {
        LoggingInterceptor intercept = (LoggingInterceptor) data.get(i);
        String url = intercept.getRequestUrl();
        // todo : do what's ever you want with url
        break;
      } else {
        continue;
      }
    }
  }

}

代码示例来源:origin: stackoverflow.com

Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl)
     .addConverterFactory(GsonConverterFactory.create())
     .build();
 retrofit.client().interceptors().add(new LoggingInterceptor());

代码示例来源:origin: boredream/DesignResCollection

public static AppService getApiService() {
  return retrofit.create(AppService.class);
}

代码示例来源:origin: stackoverflow.com

@Override
public void onResponse(Response<RegistrationResponse> response, 
            Retrofit retrofit) {
  if (response.isSuccess()) {
    // Do success handling here
  } else {
    try {
      MyError myError = (MyError)retrofit.responseConverter(
          MyError.class, MyError.class.getAnnotations())
        .convert(response.errorBody());
      // Do error handling here
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

代码示例来源:origin: macoscope/RoomBookerMVP

public RetrofitRestRepository(Retrofit retrofit) {
  apiService = retrofit.create(ApiService.class);
}

代码示例来源:origin: stackoverflow.com

public static APIError parseError(Response<?> response, Retrofit retrofit) {
  Converter<ResponseBody, APIError> converter =
      retrofit.responseConverter(APIError.class, new Annotation[0]);

  APIError error;

  try {
    error = converter.convert(response.errorBody());
  } catch (IOException e) {
    e.printStackTrace();
    return new APIError();
  }

  return error;
}

代码示例来源:origin: Leaking/WeGit

public Authenticator(Context context) {
  super(context);
  this.context = context;
  this.app = (GithubApplication) context.getApplicationContext();
  this.retrofit = RetrofitUtil.getRetrofitWithoutTokenInstance(context);
  this.service = retrofit.create(GithubService.class);
}

代码示例来源:origin: vinsol-spree-contrib/spree-android

public static ErrorResponse parseError(Response response, Retrofit retrofit) {
    Converter<ResponseBody, ErrorResponse> converter =
        retrofit.responseConverter(ErrorResponse.class, new Annotation[0]);

    ErrorResponse error;

    try {
      error = converter.convert(response.errorBody());
    } catch (IOException e) {
      return new ErrorResponse();
    }

    return error;
  }
}

代码示例来源:origin: Leaking/WeGit

public TokenInteractorImpl(Context context, final OnTokenCreatedListener listener){
  this.context = context;
  this.listener = listener;
  this.service = RetrofitUtil.getRetrofitWithoutTokenInstance(context).create(GithubService.class);
}

代码示例来源:origin: codepath/dagger2-example

@Provides
  @UserScope
  public GitHubApiInterface providesGitHubInterface(Retrofit retrofit) {
    return retrofit.create(GitHubApiInterface.class);
  }
}

代码示例来源:origin: Leaking/WeGit

public RepoInteractorImpl(Context context, final OnLoadRepoListener listener){
  this.context = context;
  this.listener = listener;
  this.service = RetrofitUtil.getJsonRetrofitInstance(context).create(GithubService.class);
  this.gitHubAccount = GitHubAccount.getInstance(context);
}

代码示例来源:origin: Leaking/WeGit

public FindItemsInteractorImpl(Context context, final OnLoadItemListListener listener) {
  this.context = context;
  this.service = RetrofitUtil.getJsonRetrofitInstance(context).create(GithubService.class);
  this.gitHubAccount = GitHubAccount.getInstance(context);
  this.listener = listener;
}

代码示例来源:origin: Leaking/WeGit

public RepoAndEventPreviewInteractorImpl(final Context context, final OnLoadRepoAndEventPreviewListener listener){
  this.context = context;
  this.listener = listener;
  this.service = RetrofitUtil.getJsonRetrofitInstance(context).create(GithubService.class);
  this.gitHubAccount = GitHubAccount.getInstance(context);
}

代码示例来源:origin: Leaking/WeGit

public CodeInteractorImpl(final Context context, final OnCodeListener listener){
  this.listener = listener;
  this.context = context;
  this.service = RetrofitUtil.getStringRetrofitInstance(context).create(GithubService.class);
  this.gitHubAccount = GitHubAccount.getInstance(context);
}

代码示例来源:origin: v1210012100/Gank.IO2

public static void getAllResult(final String type, final int count, final int pageIndex, final ICallBack<AllResult> callBack) {
    Call<AllResult> allData = retrofit.create(GankCall.class).getAllDate(type, count, pageIndex);
    final String key = type + count + pageIndex;
    allData.enqueue(new Callback<AllResult>() {
      @Override
      public void onResponse(Response<AllResult> response, Retrofit retrofit) {
        if (response.isSuccess()) {
          AllResult allResult = response.body();
          if (!allResult.getError()) {
            callBack.onSuccess(type, key, allResult);
          } else {
            callBack.onFailure(type, key, "数据错误");
          }
        }
      }

      @Override
      public void onFailure(Throwable t) {
        callBack.onFailure(type, key, "请求失败");
      }
    });
  }
}

代码示例来源:origin: huangdali/newkjdemo

@Override
public void getMovie(int start, int count, final OnHttpCallBack<Movies> callBack) {
  RetrofitUtils.newInstence(GlobalField.MOVIE_TOP250_URL)
      .create(APIService.class)
      .getMovies(start, count)
      .observeOn(AndroidSchedulers.mainThread())

代码示例来源:origin: jamorham/xDrip-plus

public ShareRest(Context context, OkHttpClient okHttpClient) {
  try {
    OkHttpClient httpClient = okHttpClient != null ? okHttpClient : getOkHttpClient();
    if (httpClient == null) httpClient = getOkHttpClient(); // try again on failure
    // if fails second time we've got big problems
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    Gson gson = new GsonBuilder()
        .excludeFieldsWithoutExposeAnnotation()
        .create();
    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(sharedPreferences.getBoolean("dex_share_us_acct", true) ? US_SHARE_BASE_URL : NON_US_SHARE_BASE_URL)
        .client(httpClient)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();
    dexcomShareApi = retrofit.create(DexcomShare.class);
    sessionId = sharedPreferences.getString("dexcom_share_session_id", null);
    username = sharedPreferences.getString("dexcom_account_name", null);
    password = sharedPreferences.getString("dexcom_account_password", null);
    serialNumber = sharedPreferences.getString("share_key", null);
    if (sharedPreferences.getBoolean("engineering_mode", false)) {
      final String share_test_key = sharedPreferences.getString("share_test_key", "").trim();
      if (share_test_key.length() > 4) serialNumber = share_test_key;
    }
    sharedPreferences.registerOnSharedPreferenceChangeListener(preferenceChangeListener);
    if ("".equals(sessionId)) // migrate previous empty sessionIds to null;
      sessionId = null;
  } catch (IllegalStateException e) {
    Log.wtf(TAG, "Illegal state exception: " + e);
  }
}

代码示例来源:origin: NightscoutFoundation/xDrip

public ShareRest(Context context, OkHttpClient okHttpClient) {
  try {
    OkHttpClient httpClient = okHttpClient != null ? okHttpClient : getOkHttpClient();
    if (httpClient == null) httpClient = getOkHttpClient(); // try again on failure
    // if fails second time we've got big problems
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    Gson gson = new GsonBuilder()
        .excludeFieldsWithoutExposeAnnotation()
        .create();
    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(sharedPreferences.getBoolean("dex_share_us_acct", true) ? US_SHARE_BASE_URL : NON_US_SHARE_BASE_URL)
        .client(httpClient)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();
    dexcomShareApi = retrofit.create(DexcomShare.class);
    sessionId = sharedPreferences.getString("dexcom_share_session_id", null);
    username = sharedPreferences.getString("dexcom_account_name", null);
    password = sharedPreferences.getString("dexcom_account_password", null);
    serialNumber = sharedPreferences.getString("share_key", null);
    if (sharedPreferences.getBoolean("engineering_mode", false)) {
      final String share_test_key = sharedPreferences.getString("share_test_key", "").trim();
      if (share_test_key.length() > 4) serialNumber = share_test_key;
    }
    sharedPreferences.registerOnSharedPreferenceChangeListener(preferenceChangeListener);
    if ("".equals(sessionId)) // migrate previous empty sessionIds to null;
      sessionId = null;
  } catch (IllegalStateException e) {
    Log.wtf(TAG, "Illegal state exception: " + e);
  }
}

相关文章