本文整理匯總了Java中retrofit.client.Response.getStatus方法的典型用法代碼示例。如果您正苦於以下問題:Java Response.getStatus方法的具體用法?Java Response.getStatus怎麽用?Java Response.getStatus使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類retrofit.client.Response
的用法示例。
在下文中一共展示了Response.getStatus方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: httpError
import retrofit.client.Response; //導入方法依賴的package包/類
private static void httpError(int expectedStatus, Response response) throws CommandException {
if (null == response) {
throw new IllegalArgumentException("Response was null");
}
// obtain response body
String body = null;
if (null != response.getBody()) {
try (InputStream errStream = response.getBody().in()) {
body = CharStreams.toString(new InputStreamReader(errStream));
} catch (IOException ignored) {
}
}
throw new CommandException("HTTP " + response.getStatus() + " "
+ response.getReason() + " but expected " + expectedStatus + ":\n" + body);
}
示例2: handleError
import retrofit.client.Response; //導入方法依賴的package包/類
@Override
public Throwable handleError(RetrofitError cause) {
Response r = cause.getResponse();
if (r != null) {
switch (r.getStatus()) {
case 500:
Log.d(TAG, "500 error during request to " + r.getUrl());
return new ServerErrorException(cause.getMessage());
case 403:
Log.d(TAG, "403 error during request to " + r.getUrl());
return new ForbiddenException(cause.getMessage());
case 404:
Log.d(TAG, "404 error during request to " + r.getUrl());
return new NotFoundException(cause.getMessage());
case 400:
Log.d(TAG, "400 error during request to " + r.getUrl());
return new BadRequestException(cause.getMessage());
}
}
return cause;
}
示例3: getErrorMessage
import retrofit.client.Response; //導入方法依賴的package包/類
public static String getErrorMessage(Response response) {
String text = responseToString(response);
try {
JSONObject object = new JSONObject(text);
if (object.has("errors")) {
JSONArray errors = object.getJSONArray("errors");
if (errors.length() > 0) {
JSONObject error = errors.getJSONObject(0);
if (error.has("message")) {
return error.getString("message");
}
}
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("Api", "getErrorMessage: "+text );
}
if (response.getStatus() > 500) {
return App.getContext().getString(R.string.check_network);
}
return null;
}
示例4: register
import retrofit.client.Response; //導入方法依賴的package包/類
private void register(final boolean legacy) {
try {
if (legacy) {
collectorRegistrationService.legacyRegister(this.collectorId, this.collectorRegistrationRequest);
} else {
collectorRegistrationService.register(this.collectorId, this.collectorRegistrationRequest);
}
} catch (RetrofitError e) {
final Response response = e.getResponse();
if (response != null) {
if (!legacy && response.getStatus() == 404) {
// Try again with the Graylog 1.x URL if we didn't try yet.
register(true);
} else {
LOG.warn("Unable to send heartbeat to Graylog server, result was: {} - {}", response.getStatus(), response.getReason());
}
} else {
final String message;
if (e.getCause() != null) {
message = e.getCause().getClass().getSimpleName() + ": " + e.getCause().getMessage();
} else {
message = e.getClass().getSimpleName() + ": " + e.getMessage();
}
LOG.warn("Unable to send heartbeat to Graylog server: {}", message);
}
}
}
示例5: intercept
import retrofit.client.Response; //導入方法依賴的package包/類
@Override
public void intercept(RequestFacade request) {
if (!loggedIn) {
try {
FormUrlEncodedTypedOutput to = new FormUrlEncodedTypedOutput();
to.addField("username", username);
to.addField("password", password);
to.addField("grant_type", "password");
String base64Auth = BaseEncoding.base64().encode(new String(clientId + ":" + clientSecret).getBytes());
List<Header> headers = new ArrayList<Header>();
headers.add(new Header("Authorization", "Basic " + base64Auth));
Request req = new Request("POST", tokenIssuingEndpoint, headers, to);
Response resp = client.execute(req);
if (resp.getStatus() < 200 || resp.getStatus() > 299) {
throw new SecuredRestException("Login failure: "
+ resp.getStatus() + " - " + resp.getReason());
} else {
String body = IOUtils.toString(resp.getBody().in());
accessToken = new Gson().fromJson(body, JsonObject.class).get("access_token").getAsString();
request.addHeader("Authorization", "Bearer " + accessToken);
loggedIn = true;
}
} catch (Exception e) {
throw new SecuredRestException(e);
}
} else {
request.addHeader("Authorization", "Bearer " + accessToken);
}
}
示例6: handleError
import retrofit.client.Response; //導入方法依賴的package包/類
@Override
public Throwable handleError(RetrofitError cause) {
Response r = cause.getResponse();
if (r != null) {
if (r.getStatus() == 401) {
return new OAuthUnauthorizedException(cause);
}
if (r.getUrl().endsWith("/checkin") && r.getStatus() == 409) {
return new CheckinInProgressException(cause);
}
}
return cause;
}
示例7: waitForServer
import retrofit.client.Response; //導入方法依賴的package包/類
/**
* {@inheritDoc}
*/
@Override
public void waitForServer(int waitTime) {
if (waitTime == 0) {
return;
}
LOGGER.info("Waiting {} seconds for server to start...", waitTime);
final StatusApi apiClient = buildServerApiClient(StatusApi.class);
final long start = System.currentTimeMillis();
while (true) {
if (System.currentTimeMillis() - start > waitTime * 1000) {
throw new CommandException("Timed out after " + waitTime + " seconds waiting for server to start");
}
try {
final Response response = apiClient.checkStatus();
if (HttpURLConnection.HTTP_OK == response.getStatus()) {
LOGGER.info("Server started");
break;
}
Thread.sleep(STATUS_CHECK_INTERVAL);
} catch (Exception ignored) {
}
}
}
示例8: invokeAndCheckResponse
import retrofit.client.Response; //導入方法依賴的package包/類
public static void invokeAndCheckResponse(int expectedStatus, Supplier<Response> request) throws CommandException {
try {
// invoke the request
final Response response = request.get();
// check response code is successful
if (response.getStatus() != expectedStatus) {
httpError(expectedStatus, response);
}
} catch (RetrofitError e) {
httpError(expectedStatus, e.getResponse());
}
}
示例9: shouldResendRequest
import retrofit.client.Response; //導入方法依賴的package包/類
public static boolean shouldResendRequest(Exception e) {
if (e != null && e instanceof RetrofitError) {
Response response = ((RetrofitError) e).getResponse();
int status = -1;
if (response != null) status = response.getStatus();
return status == 429 || status == 502 || status == 520;
} else return false;
}
示例10: updateFollowingStatus
import retrofit.client.Response; //導入方法依賴的package包/類
private void updateFollowingStatus(Response response) {
stopMenuLoading();
if (response.getStatus() == 204) {
mIsFollowing = true;
} else if (response.getStatus() == 404) {
mIsFollowing = false;
}
mFollowMenu.setChecked(mIsFollowing);
mFollowMenu.setTitle(mIsFollowing ? R.string.action_unfollow : R.string.action_follow);
}
示例11: checkUnfollowingRes
import retrofit.client.Response; //導入方法依賴的package包/類
private void checkUnfollowingRes(Response res) {
stopMenuLoading();
if (res.getStatus() == 204) {
swapFollowStatus();
}
}
示例12: checkFollowingRes
import retrofit.client.Response; //導入方法依賴的package包/類
private void checkFollowingRes(Response res) {
stopMenuLoading();
if (res.getStatus() == 204) {
swapFollowStatus();
}
TypedInput body = res.getBody();
}
示例13: bucketCreated
import retrofit.client.Response; //導入方法依賴的package包/類
private void bucketCreated(Response data) {
Activity activity = getActivity();
if (activity == null) {
return;
}
UiUtils.dismissDialog(mDialog);
if (data.getStatus() == 202) {
UiUtils.showToast(activity, R.string.shot_created);
activity.setResult(Activity.RESULT_OK);
activity.finish();
}
}
示例14: handleError
import retrofit.client.Response; //導入方法依賴的package包/類
@Override
public Throwable handleError(final RetrofitError cause) {
Response r = cause.getResponse();
if (r != null && r.getStatus() >= 400) {
try (InputStreamReader reader = new InputStreamReader(r.getBody().in(), Charsets.UTF_8)) {
return new RuntimeException(CharStreams.toString(reader));
} catch (IOException ioe) {
logger.error("IOException during stream read: " + ioe.getMessage(), ioe);
}
}
return cause;
}
示例15: handleError
import retrofit.client.Response; //導入方法依賴的package包/類
@Override
public Throwable handleError(RetrofitError error) {
logger.debug("error: "+error.getKind());
if (error.getKind().equals(RetrofitError.Kind.NETWORK)) {
// Phone is not connected at all
return new NoConnectivityException(context.getString(R.string.no_internet_connection));
}
if (error.getKind().equals(RetrofitError.Kind.CONVERSION)) {
// Web services usually return JSON, so the phone is probably trying to use mobile data without credit
return new NoConnectivityException(context.getString(R.string.no_internet_connection_mobile_out_of_credit));
}
Response response = error.getResponse();
String errorDescription = "Unknown Retrofit error";
if (response == null) {
return error;
}
switch (response.getStatus()) {
case HttpStatus.SC_BAD_REQUEST:
// 404 returned from the server means there is no matching data
errorDescription = error.getMessage().replace("400", "");
break;
case HttpStatus.SC_UNAUTHORIZED:
//return new UnauthorizedException(error);
case HttpStatus.SC_NOT_FOUND:
if (!TextUtils.isEmpty(error.getMessage())) {
// 404 returned from the server means there is no matching data
errorDescription = error.getMessage().replace("404", "");
}
break;
case HttpStatus.SC_INTERNAL_SERVER_ERROR:
if (!TextUtils.isEmpty(error.getMessage())) {
// 500 returned from the server means the server error
errorDescription = error.getMessage().replace("500", "");
}
break;
case HttpStatus.SC_SERVICE_UNAVAILABLE:
return new NoConnectivityException("Server is not responding");
default:
try {
errorDescription = context.getString(R.string.error_network_http_error, error.getResponse().getStatus());
} catch (Exception ex2) {
logger.error("handleError: " + ex2.getLocalizedMessage());
errorDescription = context.getString(R.string.error_unknown);
}
break;
}
//return error;
return new Exception(errorDescription);
}