retrofit.Retrofit.responseConverter()方法的使用及代码示例

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

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

Retrofit.responseConverter介绍

暂无

代码示例

代码示例来源: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<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: 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: 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;
  }
}

相关文章