本文整理汇总了Java中org.apache.http.StatusLine.getStatusCode方法的典型用法代码示例。如果您正苦于以下问题:Java StatusLine.getStatusCode方法的具体用法?Java StatusLine.getStatusCode怎么用?Java StatusLine.getStatusCode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.StatusLine
的用法示例。
在下文中一共展示了StatusLine.getStatusCode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: postAttempt
import org.apache.http.StatusLine; //导入方法依赖的package包/类
public static String postAttempt(int id) throws IOException {
final CloseableHttpClient client = EduStepicAuthorizedClient.getHttpClient();
if (client == null || StudySettings.getInstance().getUser() == null) return "";
final HttpPost attemptRequest = new HttpPost(EduStepicNames.STEPIC_API_URL + EduStepicNames.ATTEMPTS);
String attemptRequestBody = new Gson().toJson(new StepicWrappers.AttemptWrapper(id));
attemptRequest.setEntity(new StringEntity(attemptRequestBody, ContentType.APPLICATION_JSON));
final CloseableHttpResponse attemptResponse = client.execute(attemptRequest);
final HttpEntity responseEntity = attemptResponse.getEntity();
final String attemptResponseString = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
final StatusLine statusLine = attemptResponse.getStatusLine();
EntityUtils.consume(responseEntity);
if (statusLine.getStatusCode() != HttpStatus.SC_CREATED) {
LOG.warn("Failed to make attempt " + attemptResponseString);
return "";
}
return attemptResponseString;
}
示例2: sendResponseMessage
import org.apache.http.StatusLine; //导入方法依赖的package包/类
@Override
public void sendResponseMessage(HttpResponse response) throws IOException {
if (!Thread.currentThread().isInterrupted()) {
StatusLine status = response.getStatusLine();
if (status.getStatusCode() == HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE) {
//already finished
if (!Thread.currentThread().isInterrupted())
sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), null);
} else if (status.getStatusCode() >= 300) {
if (!Thread.currentThread().isInterrupted())
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), null, new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()));
} else {
if (!Thread.currentThread().isInterrupted()) {
Header header = response.getFirstHeader(AsyncHttpClient.HEADER_CONTENT_RANGE);
if (header == null) {
append = false;
current = 0;
} else {
Log.v(LOG_TAG, AsyncHttpClient.HEADER_CONTENT_RANGE + ": " + header.getValue());
}
sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), getResponseData(response.getEntity()));
}
}
}
}
示例3: getEntity
import org.apache.http.StatusLine; //导入方法依赖的package包/类
private String getEntity(URI url) throws IOException {
final HttpGet get = new HttpGet(url);
get.setConfig(requestConfig);
get.setHeader("Accept", "application/json");
HttpClientBuilder clientBuilder = HttpClients.custom();
if (sslContext != null) {
clientBuilder.setSslcontext(sslContext);
}
try (CloseableHttpClient httpClient = clientBuilder.build()) {
try (CloseableHttpResponse response = httpClient.execute(get)) {
final StatusLine statusLine = response.getStatusLine();
final int statusCode = statusLine.getStatusCode();
if (200 != statusCode) {
final String msg = String.format("Failed to get entity from %s, response=%d:%s",
get.getURI(), statusCode, statusLine.getReasonPhrase());
throw new RuntimeException(msg);
}
final HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
}
}
}
示例4: getStatusCode
import org.apache.http.StatusLine; //导入方法依赖的package包/类
protected int getStatusCode() {
StatusLine sl = null;
try {
sl = response.getStatusLine();
}
catch (Exception e) {
// ignore
}
if (null == sl) {
// as BAT REQUEST
return 400;
}
return sl.getStatusCode();
}
示例5: sendResponseMessage
import org.apache.http.StatusLine; //导入方法依赖的package包/类
@Override
public void sendResponseMessage(HttpResponse response) throws IOException {
// do not process if request has been cancelled
if (!Thread.currentThread().isInterrupted()) {
StatusLine status = response.getStatusLine();
byte[] responseBody;
responseBody = getResponseData(response.getEntity());
// additional cancellation check as getResponseData() can take non-zero time to process
if (!Thread.currentThread().isInterrupted()) {
if (status.getStatusCode() >= 300) {
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), responseBody, new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()));
} else {
sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), responseBody);
}
}
}
}
示例6: sendResponseMessage
import org.apache.http.StatusLine; //导入方法依赖的package包/类
public void sendResponseMessage(HttpResponse response) throws IOException {
if (!Thread.currentThread().isInterrupted()) {
StatusLine status = response.getStatusLine();
byte[] responseBody = getResponseData(response.getEntity());
if (!Thread.currentThread().isInterrupted()) {
if (status.getStatusCode() >= 300) {
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(),
responseBody, new HttpResponseException(status.getStatusCode(),
status.getReasonPhrase()));
} else {
sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(),
responseBody);
}
}
}
}
示例7: getFromStepic
import org.apache.http.StatusLine; //导入方法依赖的package包/类
static <T> T getFromStepic(String link, final Class<T> container, @NotNull final CloseableHttpClient client) throws IOException {
if (!link.startsWith("/")) link = "/" + link;
final HttpGet request = new HttpGet(EduStepicNames.STEPIC_API_URL + link);
final CloseableHttpResponse response = client.execute(request);
final StatusLine statusLine = response.getStatusLine();
final HttpEntity responseEntity = response.getEntity();
final String responseString = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
EntityUtils.consume(responseEntity);
if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
throw new IOException("Stepic returned non 200 status code " + responseString);
}
return deserializeStepicResponse(container, responseString);
}
示例8: enrollToCourse
import org.apache.http.StatusLine; //导入方法依赖的package包/类
public static boolean enrollToCourse(final int courseId, @Nullable final StepicUser stepicUser) {
if (stepicUser == null) return false;
HttpPost post = new HttpPost(EduStepicNames.STEPIC_API_URL + EduStepicNames.ENROLLMENTS);
try {
final StepicWrappers.EnrollmentWrapper enrollment = new StepicWrappers.EnrollmentWrapper(String.valueOf(courseId));
post.setEntity(new StringEntity(new GsonBuilder().create().toJson(enrollment)));
final CloseableHttpClient client = EduStepicAuthorizedClient.getHttpClient(stepicUser);
CloseableHttpResponse response = client.execute(post);
StatusLine line = response.getStatusLine();
return line.getStatusCode() == HttpStatus.SC_CREATED;
}
catch (IOException e) {
LOG.warn(e.getMessage());
}
return false;
}
示例9: load
import org.apache.http.StatusLine; //导入方法依赖的package包/类
@Override
public boolean load(BuildCacheKey key, BuildCacheEntryReader reader) throws BuildCacheException {
final URI uri = root.resolve("./" + key.getHashCode());
HttpGet httpGet = new HttpGet(uri);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Response for GET {}: {}", safeUri(uri), statusLine);
}
int statusCode = statusLine.getStatusCode();
if (statusCode >= 200 && statusCode < 300) {
reader.readFrom(response.getEntity().getContent());
return true;
} else if (statusCode == 404) {
return false;
} else {
// TODO: We should consider different status codes as fatal/recoverable
throw new BuildCacheException(String.format("For key '%s', using %s response status %d: %s", key, getDescription(), statusCode, statusLine.getReasonPhrase()));
}
} catch (IOException e) {
// TODO: We should consider different types of exceptions as fatal/recoverable.
// Right now, everything is considered recoverable.
throw new BuildCacheException(String.format("loading key '%s' from %s", key, getDescription()), e);
} finally {
HttpClientUtils.closeQuietly(response);
}
}
示例10: postUnit
import org.apache.http.StatusLine; //导入方法依赖的package包/类
public static void postUnit(int lessonId, int position, int sectionId, Project project) {
if (!checkIfAuthorized(project, "postTask")) return;
final HttpPost request = new HttpPost(EduStepicNames.STEPIC_API_URL + EduStepicNames.UNITS);
final StepicWrappers.UnitWrapper unitWrapper = new StepicWrappers.UnitWrapper();
final StepicWrappers.Unit unit = new StepicWrappers.Unit();
unit.setLesson(lessonId);
unit.setPosition(position);
unit.setSection(sectionId);
unitWrapper.setUnit(unit);
String requestBody = new Gson().toJson(unitWrapper);
request.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
try {
final CloseableHttpClient client = EduStepicAuthorizedClient.getHttpClient();
if (client == null) return;
final CloseableHttpResponse response = client.execute(request);
final HttpEntity responseEntity = response.getEntity();
final String responseString = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
final StatusLine line = response.getStatusLine();
EntityUtils.consume(responseEntity);
if (line.getStatusCode() != HttpStatus.SC_CREATED) {
LOG.error(FAILED_TITLE + responseString);
showErrorNotification(project, FAILED_TITLE, responseString);
}
}
catch (IOException e) {
LOG.error(e.getMessage());
}
}
示例11: ResponseImpl
import org.apache.http.StatusLine; //导入方法依赖的package包/类
public ResponseImpl(HttpResponse response, HttpRequestBase httpMethod)
{
this.httpMethod = httpMethod;
this.response = response;
if( response != null )
{
final StatusLine statusLine = response.getStatusLine();
this.code = statusLine.getStatusCode();
this.message = statusLine.getReasonPhrase();
}
}
示例12: onPostExecute
import org.apache.http.StatusLine; //导入方法依赖的package包/类
@Override
protected void onPostExecute(Object result) {
if (result instanceof StatusLine) {
notificationManager.cancel(Util.NOTIFY_UPDATE);
StatusLine status = (StatusLine) result;
if (status.getStatusCode() == 204) { // No Content
String none = mContext.getString(R.string.title_update_none);
Toast.makeText(mContext, none, Toast.LENGTH_LONG).show();
} else
Toast.makeText(mContext, status.getStatusCode() + " " + status.getReasonPhrase(), Toast.LENGTH_LONG)
.show();
} else if (result instanceof Throwable) {
notificationManager.cancel(Util.NOTIFY_UPDATE);
Throwable ex = (Throwable) result;
Toast.makeText(mContext, ex.toString(), Toast.LENGTH_LONG).show();
} else {
File download = (File) result;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(download), "application/vnd.android.package-archive");
PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Update notification
builder.setContentText(mContext.getString(R.string.title_update_install));
builder.setWhen(System.currentTimeMillis());
builder.setAutoCancel(true);
builder.setOngoing(false);
builder.setContentIntent(pi);
notification = builder.build();
notificationManager.notify(Util.NOTIFY_UPDATE, notification);
}
}
示例13: handleResponse
import org.apache.http.StatusLine; //导入方法依赖的package包/类
@Override
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
final StatusLine statusLine = response.getStatusLine();
int status = statusLine.getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
}
}
示例14: handleResponse
import org.apache.http.StatusLine; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private ResponseInfo<T> handleResponse(HttpResponse response) throws HttpException, IOException {
if (response == null) {
throw new HttpException("response is null");
}
if (isCancelled()) return null;
StatusLine status = response.getStatusLine();
int statusCode = status.getStatusCode();
if (statusCode < 300) {
Object result = null;
HttpEntity entity = response.getEntity();
if (entity != null) {
isUploading = false;
if (isDownloadingFile) {
autoResume = autoResume && OtherUtils.isSupportRange(response);
String responseFileName = autoRename ? OtherUtils.getFileNameFromHttpResponse(response) : null;
result = mFileDownloadHandler.handleEntity(entity, this, fileSavePath, autoResume, responseFileName);
} else {
// Set charset from response header info if it's exist.
String responseCharset = OtherUtils.getCharsetFromHttpResponse(response);
charset = TextUtils.isEmpty(responseCharset) ? charset : responseCharset;
result = mStringDownloadHandler.handleEntity(entity, this, charset);
HttpUtils.sHttpGetCache.put(requestUrl, (String) result, expiry);
}
}
return new ResponseInfo<T>(response, (T) result, false);
} else if (statusCode == 301 || statusCode == 302) {
if (httpRedirectHandler == null) {
httpRedirectHandler = new DefaultHttpRedirectHandler();
}
HttpRequestBase request = httpRedirectHandler.getDirectRequest(response);
if (request != null) {
return this.sendRequest(request);
}
} else if (statusCode == 416) {
throw new HttpException(statusCode, "maybe the file has downloaded completely");
} else {
throw new HttpException(statusCode, status.getReasonPhrase());
}
return null;
}
示例15: postCourse
import org.apache.http.StatusLine; //导入方法依赖的package包/类
private static void postCourse(final Project project, @NotNull Course course) {
if (!checkIfAuthorized(project, "post course")) return;
final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
if (indicator != null) {
indicator.setText("Uploading course to " + EduStepicNames.STEPIC_URL);
indicator.setIndeterminate(false);
}
final HttpPost request = new HttpPost(EduStepicNames.STEPIC_API_URL + "/courses");
final StepicUser currentUser = EduStepicAuthorizedClient.getCurrentUser();
if (currentUser != null) {
final List<StepicUser> courseAuthors = course.getAuthors();
for (int i = 0; i < courseAuthors.size(); i++) {
if (courseAuthors.size() > i) {
final StepicUser courseAuthor = courseAuthors.get(i);
currentUser.setFirstName(courseAuthor.getFirstName());
currentUser.setLastName(courseAuthor.getLastName());
}
}
course.setAuthors(Collections.singletonList(currentUser));
}
String requestBody = new Gson().toJson(new StepicWrappers.CourseWrapper(course));
request.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
try {
final CloseableHttpClient client = EduStepicAuthorizedClient.getHttpClient();
if (client == null) {
LOG.warn("Http client is null");
return;
}
final CloseableHttpResponse response = client.execute(request);
final HttpEntity responseEntity = response.getEntity();
final String responseString = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
final StatusLine line = response.getStatusLine();
EntityUtils.consume(responseEntity);
if (line.getStatusCode() != HttpStatus.SC_CREATED) {
final String message = FAILED_TITLE + "course ";
LOG.error(message + responseString);
showErrorNotification(project, FAILED_TITLE, responseString);
return;
}
final RemoteCourse postedCourse = new Gson().fromJson(responseString, StepicWrappers.CoursesContainer.class).courses.get(0);
postedCourse.setLessons(course.getLessons(true));
postedCourse.setAuthors(course.getAuthors());
postedCourse.setCourseMode(CCUtils.COURSE_MODE);
postedCourse.setLanguage(course.getLanguageID());
final int sectionId = postModule(postedCourse.getId(), 1, String.valueOf(postedCourse.getName()), project);
int position = 1;
for (Lesson lesson : course.getLessons()) {
if (indicator != null) {
indicator.checkCanceled();
indicator.setText2("Publishing lesson " + lesson.getIndex());
}
final int lessonId = postLesson(project, lesson);
postUnit(lessonId, position, sectionId, project);
if (indicator != null) {
indicator.setFraction((double)lesson.getIndex()/course.getLessons().size());
indicator.checkCanceled();
}
position += 1;
}
ApplicationManager.getApplication().runReadAction(() -> postAdditionalFiles(course, project, postedCourse.getId()));
StudyTaskManager.getInstance(project).setCourse(postedCourse);
showNotification(project, "Course published");
}
catch (IOException e) {
LOG.error(e.getMessage());
}
}