本文整理汇总了Java中retrofit2.adapter.rxjava.HttpException类的典型用法代码示例。如果您正苦于以下问题:Java HttpException类的具体用法?Java HttpException怎么用?Java HttpException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpException类属于retrofit2.adapter.rxjava包,在下文中一共展示了HttpException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: searchUsers_OtherHttpError_SearchTerminatedWithError
import retrofit2.adapter.rxjava.HttpException; //导入依赖的package包/类
@Test
public void searchUsers_OtherHttpError_SearchTerminatedWithError() {
//Given
when(githubUserRestService.searchGithubUsers(anyString())).thenReturn(get403ForbiddenError());
//When
TestSubscriber<List<User>> subscriber = new TestSubscriber<>();
userRepository.searchUsers(USER_LOGIN_MOYHEEN).subscribe(subscriber);
//Then
subscriber.awaitTerminalEvent();
subscriber.assertError(HttpException.class);
verify(githubUserRestService).searchGithubUsers(USER_LOGIN_MOYHEEN);
verify(githubUserRestService, never()).getUser(USER_LOGIN_MOYHEEN);
verify(githubUserRestService, never()).getUser(USER_LOGIN_2_MOYHEEN);
}
示例2: onLogInError
import retrofit2.adapter.rxjava.HttpException; //导入依赖的package包/类
public static void onLogInError(Throwable e) {
if (e instanceof HttpException) {
int code = ((HttpException) e).code();
switch (code) {
case 403: ToastUtil.showShort(R.string.wrong_password); break;
case 404: ToastUtil.showShort(R.string.user_not_exist); break;
case 409: ToastUtil.showShort(R.string.user_already_exist); break;
default: ToastUtil.showShort(R.string.network_failure); break;
}
} else {
if (TextUtils.isEmpty(e.getMessage())) {
ToastUtil.showShort(R.string.network_failure);
} else {
ToastUtil.showShort(e.getMessage());
}
}
e.printStackTrace();
}
示例3: onUploadError
import retrofit2.adapter.rxjava.HttpException; //导入依赖的package包/类
public static void onUploadError(Throwable e) {
if (e instanceof HttpException) {
int code = ((HttpException) e).code();
switch (code) {
case 404: ToastUtil.showShort(R.string.file_wrong_upload); break;
case 409: ToastUtil.showShort(R.string.name_already_exist); break;
default: ToastUtil.showShort(R.string.network_failure); break;
}
} else {
if (TextUtils.isEmpty(e.getMessage())) {
ToastUtil.showShort(R.string.network_failure);
} else {
ToastUtil.showShort(e.getMessage());
}
}
e.printStackTrace();
}
示例4: onError
import retrofit2.adapter.rxjava.HttpException; //导入依赖的package包/类
@Override
public void onError(Throwable e) {
e.printStackTrace();
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
//httpException.response().errorBody().string()
int code = httpException.code();
String msg = httpException.getMessage();
if (code == 504) {
msg = "网络不给力";
}
if (code == 502 || code == 404) {
msg = "服务器异常,请稍后再试";
}
onFailure(msg);
} else {
onFailure(e.getMessage());
}
onFinish();
}
示例5: isServerError
import retrofit2.adapter.rxjava.HttpException; //导入依赖的package包/类
public static boolean isServerError(Throwable error) {
if (error != null && error instanceof HttpException) {
HttpException exception = (HttpException) error;
return exception.code() >= 500;
}
return false;
}
示例6: searchMakeUp_OtherHttpError_searchTerminatedWithError
import retrofit2.adapter.rxjava.HttpException; //导入依赖的package包/类
@Test
public void searchMakeUp_OtherHttpError_searchTerminatedWithError() {
// Given
when(makeUpProductRestService.searchMakeUpProducts(anyString(), anyString())).thenReturn
(get403ForbiddenError().toList());
// When
TestSubscriber<List<MakeUp>> subscriber = new TestSubscriber<>();
makeUpRepository.searchMakeUp(BRAND_COVERGIRL, PRODUCT_TYPE_LIPSTICK).subscribe(subscriber);
// Then
subscriber.awaitTerminalEvent();
subscriber.assertError(HttpException.class);
verify(makeUpProductRestService).searchMakeUpProducts(BRAND_COVERGIRL,
PRODUCT_TYPE_LIPSTICK);
}
示例7: groupApishouldReturnBadRequest
import retrofit2.adapter.rxjava.HttpException; //导入依赖的package包/类
@Test
public void groupApishouldReturnBadRequest() {
// Given
login = "wrongLogin";
password = "wrongPassword";
TestSubscriber<SigfoxData<Group>> testObserver = new TestSubscriber<>();
// When
sigfoxClient.getGroupList(null, null, null).subscribe(testObserver);
// Then
testObserver.assertError(HttpException.class);
List<Throwable> errorEvents = testObserver.getOnErrorEvents();
assertThat(errorEvents).isNotNull().isNotEmpty();
assertThat(errorEvents.get(0)).hasMessage("HTTP 400 Bad Request");
}
示例8: checkFailureSessionRequestBehavior
import retrofit2.adapter.rxjava.HttpException; //导入依赖的package包/类
@Test
public void checkFailureSessionRequestBehavior(){
PowerMockito.mockStatic(ErrorMessageFactory.class);
when(ErrorMessageFactory.createMessageOnLogin(any(), any())).thenReturn("");
when(sessionService
.getSession(any(SessionRequest.class)))
.thenReturn(Observable.error(new HttpException(
Response.error(422, ResponseBody.create(null, new byte[0])))
));
// presenter.requestSession();
verify(getTokenUseCase).execute(any(Subscriber.class));
verify(sessionRepository).getSession();
verify(sessionService).getSession(any(SessionRequest.class));
verify(view).showError(anyString());
}
示例9: checkLoginErrorMessages
import retrofit2.adapter.rxjava.HttpException; //导入依赖的package包/类
@Test
public void checkLoginErrorMessages(){
String message = ErrorMessageFactory.createMessageOnLogin(
context,
new IllegalStateException());
Assert.assertEquals(message, wrongLoginOrPassword);
message = ErrorMessageFactory.createMessageOnLogin(
context,
new HttpException(Response.error(401, ResponseBody.create(null, new byte[0]))));
Assert.assertEquals(message, sessionExpired);
message = ErrorMessageFactory.createMessageOnLogin(
context,
new HttpException(Response.error(500, ResponseBody.create(null, new byte[0]))));
Assert.assertEquals(message, serverCommonError);
message = ErrorMessageFactory.createMessageOnLogin(
context,
new HttpException(Response.error(503, ResponseBody.create(null, new byte[0]))));
Assert.assertEquals(message, serverIsBusy);
message = ErrorMessageFactory.createMessageOnLogin(context, new IOException());
Assert.assertEquals(message, messageNoConnection);
}
示例10: checkRegistrationErrorMessages
import retrofit2.adapter.rxjava.HttpException; //导入依赖的package包/类
@Test
public void checkRegistrationErrorMessages(){
String message = ErrorMessageFactory.createMessageOnRegistration(
context,
new HttpException(Response.error(422, ResponseBody.create(null, new byte[0]))));
Assert.assertEquals(message, userExists);
message = ErrorMessageFactory.createMessageOnRegistration(
context,
new HttpException(Response.error(401, ResponseBody.create(null, new byte[0]))));
Assert.assertEquals(message, sessionExpired);
message = ErrorMessageFactory.createMessageOnRegistration(
context,
new HttpException(Response.error(500, ResponseBody.create(null, new byte[0]))));
Assert.assertEquals(message, serverCommonError);
message = ErrorMessageFactory.createMessageOnRegistration(
context,
new HttpException(Response.error(503, ResponseBody.create(null, new byte[0]))));
Assert.assertEquals(message, serverIsBusy);
message = ErrorMessageFactory.createMessageOnRegistration(context, new IOException());
Assert.assertEquals(message, messageNoConnection);
}
示例11: searchUsers_OtherHttpError_SearchTerminatedWithError
import retrofit2.adapter.rxjava.HttpException; //导入依赖的package包/类
@Test
public void searchUsers_OtherHttpError_SearchTerminatedWithError() {
//Given
when(githubUserRestService.searchGithubUsers(anyString())).thenReturn(get403ForbiddenError());
//When
TestSubscriber<List<User>> subscriber = new TestSubscriber<>();
userRepository.searchUsers(USER_LOGIN_RIGGAROO).subscribe(subscriber);
//Then
subscriber.awaitTerminalEvent();
subscriber.assertError(HttpException.class);
verify(githubUserRestService).searchGithubUsers(USER_LOGIN_RIGGAROO);
verify(githubUserRestService, never()).getUser(USER_LOGIN_RIGGAROO);
verify(githubUserRestService, never()).getUser(USER_LOGIN_2_REBECCA);
}
示例12: onError
import retrofit2.adapter.rxjava.HttpException; //导入依赖的package包/类
@Override
public void onError(Throwable e) {
if (e instanceof HttpException) {
Toast.makeText(mContext, "网络连接不可用,请检查网络设置", Toast.LENGTH_SHORT).show();
} else if (e instanceof UnknownHostException) {
Toast.makeText(mContext, "网络连接不可用,请检查网络设置", Toast.LENGTH_SHORT).show();
} else if (e instanceof WebResponseException) {
Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
} else if (e instanceof TokenExpriedException) {
Toast.makeText(mContext, "Token 失效,请重新登录", Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClass(mContext, LoginActivity.class);
mContext.startActivity(intent);
} else {
Timber.e(e, "Subscriber onError");
}
}
示例13: testBackoffCompletes1
import retrofit2.adapter.rxjava.HttpException; //导入依赖的package包/类
/**
* Test demonstrates the usage of {@link com.rzagorski.retrofitrxerrorhandler.backoff.strategies.Simple}
* backoff strategy. The server responds with 500 server error every time. The backoff strategy
* is executed and after {@link com.rzagorski.retrofitrxerrorhandler.backoff.strategies.Simple.Builder#setMaxRetries(int)}
* is run out, the appropriate error is passed to client.
* 4 times 10 second timeout.
* <br></br>
* Test created by Robert Zagorski on 19.10.2016
*/
@Test
public void testBackoffCompletes1() throws Exception {
mockWebServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
return new MockResponse().setResponseCode(500);
}
});
RxCallAdapter rxCallAdapter = new RxCallAdapter.Builder()
.addBackoffStrategy(Simple.init()
.addHttpCode(500)
.setMaxRetries(300).build())
.build();
GitHub github = createRetrofitInstance(mockWebServer.url("/").toString(),
new RxErrorHandingFactory(rxCallAdapter));
Observable observable = github.repos("square");
TestSubscriber testSubscriber = new TestSubscriber();
observable.subscribe(testSubscriber);
testSubscriber.awaitTerminalEvent();
testSubscriber.assertError(HttpException.class);
}
示例14: testBackoffCompletes
import retrofit2.adapter.rxjava.HttpException; //导入依赖的package包/类
@Test
public void testBackoffCompletes() throws Exception {
mockWebServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
return MockWebServerUtils.getSuccessfulResponse();
}
});
RxCallAdapter rxCallAdapter = new RxCallAdapter.Builder()
.addBackoffStrategy(Exponential.init()
.addThrowable(HttpException.class)
.setMaxRetries(3).build())
.build();
GitHub github = createRetrofitInstance(mockWebServer.url("/").toString(),
new RxErrorHandingFactory(rxCallAdapter));
Observable observable = github.repos("square");
TestSubscriber testSubscriber = new TestSubscriber();
observable.subscribe(testSubscriber);
testSubscriber.awaitTerminalEvent();
testSubscriber.assertCompleted();
}
示例15: testBackoffError1
import retrofit2.adapter.rxjava.HttpException; //导入依赖的package包/类
@Test
public void testBackoffError1() throws Exception {
mockWebServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
return new MockResponse().setResponseCode(500);
}
});
RxCallAdapter rxCallAdapter = new RxCallAdapter.Builder()
.addBackoffStrategy(Exponential.init()
.addThrowable(HttpException.class)
.setMaxRetries(3).build())
.setLoggingEnabled(true)
.build();
GitHub github = createRetrofitInstance(mockWebServer.url("/").toString(),
new RxErrorHandingFactory(rxCallAdapter));
Observable observable = github.repos("square");
TestSubscriber testSubscriber = new TestSubscriber();
observable.subscribe(testSubscriber);
testSubscriber.awaitTerminalEvent();
assertTrue(testSubscriber.getOnErrorEvents().size() == 1);
}