项目:RxJavaAndRetrofitSimple
文件:ResponseExceptionJobber.java
public static String analyze(Throwable e) {
if (!NetConnectionUtils.isNetConnected(BaseApplication.getInstance())) {
return "当前网络不可用,请检查网络设置";
} else if (e instanceof UnkNownHostException) {
return "网络错误,请重试";
} else if (e instanceof ConnectException) {
return "请求失败,请重试";
} else if (e instanceof MalformedJsonException) {
return "请求失败,请重试";
} else if (e instanceof SocketTimeoutException) {
return "请求超时,请重试";
} else {
return "请求失败,请重试";
}
}
@Test
public void testLoginWithFailure() throws IOException {
final MockWebServer server = new MockWebServer();
//On failure,the server's response code is HTTP_OK,//but it has a "success: false" parameter.
final MockResponse response = new MockResponse();
response.setResponseCode(HttpURLConnection.HTTP_OK);
response.setBody("test nonsense failure message");
server.enqueue(response);
server.start();
final ZooniverseClient client = createZooniverseClient(server);
try {
final LoginUtils.LoginResult result = client.loginSync("testusername","testpassword");
assertNotNull(result);
assertFalse(result.getSuccess());
} catch (final ZooniverseClient.LoginException e) {
assertTrue(e.getCause() instanceof MalformedJsonException);
}
server.shutdown();
}
private static String sanitizeString(String string) throws MalformedJsonException {
if (!checkString(string)) {
throw new MalformedJsonException("Invalid escape sequence");
}
return string;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。