本文整理汇总了Java中com.github.kevinsawicki.http.HttpRequest.HttpRequestException方法的典型用法代码示例。如果您正苦于以下问题:Java HttpRequest.HttpRequestException方法的具体用法?Java HttpRequest.HttpRequestException怎么用?Java HttpRequest.HttpRequestException使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.github.kevinsawicki.http.HttpRequest
的用法示例。
在下文中一共展示了HttpRequest.HttpRequestException方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: uploadMeasurements
import com.github.kevinsawicki.http.HttpRequest; //导入方法依赖的package包/类
@Override
public RequestResult uploadMeasurements(String content) {
Log.d("uploadMeasurements(): Sending post request");
try {
HttpRequest request = HttpRequest.post(url)
.followRedirects(false)
.connectTimeout(CONN_TIMEOUT)
.readTimeout(READ_TIMEOUT);
// add API key
request.part("key", apiKey);
// add app name
request.part("appId", appId);
// add file data as file
request.part("datafile", "TowerCollector_measurements_" + System.currentTimeMillis() + ".csv", "text/csv", content);
return handleResponse(request.code(), request.body());
} catch (HttpRequest.HttpRequestException ex) {
Log.d("uploadMeasurements(): Errors encountered", ex);
reportExceptionWithSuppress(ex);
return RequestResult.Failure;
}
}
示例2: makeRequest
import com.github.kevinsawicki.http.HttpRequest; //导入方法依赖的package包/类
private static String makeRequest(OIDCAccountManager accountManager, String method, String url, String body, Account account,
boolean doRetry, AccountManagerCallback<Bundle> callback)
throws IOException, UserNotAuthenticatedWrapperException, AuthenticatorException, OperationCanceledException {
String accessToken = accountManager.getAccessToken(account, callback);
String cookies = accountManager.getCookies(account, callback);
// Prepare an API request using the accessToken
HttpRequest request = new HttpRequest(url, method);
request = prepareApiRequest(request, accessToken, cookies);
if (body != "") {
request.send(body);
}
if (request.ok()) {
return request.body();
} else {
int code = request.code();
String requestContent = "empty body";
try {
requestContent = request.body();
} catch (HttpRequest.HttpRequestException e) {
//Nothing to do, the response has no body or couldn't fetch it
e.printStackTrace();
}
if (doRetry && (code == HTTP_UNAUTHORIZED || code == HTTP_FORBIDDEN ||
(code == HTTP_BAD_REQUEST && (requestContent.contains("invalid_grant") || requestContent.contains("Access Token not valid"))))) {
// We're being denied access on the first try, let's renew the token and retry
accountManager.invalidateAuthTokens(account);
return makeRequest(accountManager, method, url, body, account, false, callback);
} else {
// An unrecoverable error or the renewed token didn't work either
throw new IOException(request.code() + " " + request.message() + " " + requestContent);
}
}
}
示例3: fetchUpdates
import com.github.kevinsawicki.http.HttpRequest; //导入方法依赖的package包/类
public String fetchUpdates() {
Log.d("fetchUpdates(): Sending get request");
try {
HttpRequest request = HttpRequest.get(url)
.followRedirects(false)
.connectTimeout(CONN_TIMEOUT)
.readTimeout(READ_TIMEOUT);
return handleResponse(request.code(), request.body());
} catch (HttpRequest.HttpRequestException ex) {
Log.d("fetchUpdates(): Errors encountered", ex);
reportExceptionWithSuppress(ex);
return null;
}
}
示例4: reportExceptionWithSuppress
import com.github.kevinsawicki.http.HttpRequest; //导入方法依赖的package包/类
protected void reportExceptionWithSuppress(HttpRequest.HttpRequestException ex) {
Throwable originalException = ex.getCause();
// suppress known exceptions
if (originalException instanceof UnknownHostException
|| originalException instanceof SocketTimeoutException
|| originalException instanceof SocketException
|| originalException instanceof EOFException) {
return;
}
reportException(ex);
}
示例5: makeRequest
import com.github.kevinsawicki.http.HttpRequest; //导入方法依赖的package包/类
private static String makeRequest(OIDCAccountManager accountManager, String method, String url, Account account,
boolean doRetry, AccountManagerCallback<Bundle> callback)
throws IOException, UserNotAuthenticatedWrapperException, AuthenticatorException, OperationCanceledException {
String accessToken = accountManager.getAccessToken(account, callback);
// Prepare an API request using the accessToken
HttpRequest request = new HttpRequest(url, method);
request = prepareApiRequest(request, accessToken);
if (request.ok()) {
return request.body();
} else {
int code = request.code();
String requestContent = "empty body";
try {
requestContent = request.body();
} catch (HttpRequest.HttpRequestException e) {
//Nothing to do, the response has no body or couldn't fetch it
e.printStackTrace();
}
if (doRetry && (code == HTTP_UNAUTHORIZED || code == HTTP_FORBIDDEN ||
(code == HTTP_BAD_REQUEST && (requestContent.contains("invalid_grant") || requestContent.contains("Access Token not valid"))))) {
// We're being denied access on the first try, let's renew the token and retry
accountManager.invalidateAuthTokens(account);
return makeRequest(accountManager, method, url, account, false, callback);
} else {
// An unrecoverable error or the renewed token didn't work either
throw new IOException(request.code() + " " + request.message() + " " + requestContent);
}
}
}
示例6: sendBody
import com.github.kevinsawicki.http.HttpRequest; //导入方法依赖的package包/类
public static void sendBody(final Request request, final HttpRequest httpRequest) throws IOException {
final String requestMethod = request.getMethod();
final TypedOutput requestBody = request.getBody();
// Only POST and PUT has body as far as I know
if ((METHOD_POST.equals(requestMethod)) || METHOD_PUT.equals(requestMethod)) {
if (requestBody != null) {
// Content-Type
httpRequest.contentType(requestBody.mimeType());
// Payload
ByteArrayOutputStream outputBytes = new ByteArrayOutputStream();
requestBody.writeTo(outputBytes);
byte[] dataBytes = outputBytes.toByteArray();
try {
httpRequest.send(dataBytes);
} catch (HttpRequest.HttpRequestException hre) {
// Throw original IOException since Retrofit handles this
throw hre.getCause();
}
}
}
}
示例7: exception
import com.github.kevinsawicki.http.HttpRequest; //导入方法依赖的package包/类
@Override
public boolean exception(Exception e, Context ctx, Object source) throws CocoException {
if (e instanceof CocoException || e instanceof HttpRequest.HttpRequestException)
e.printStackTrace();
else
Log.e(e);
return false;
}
示例8: error
import com.github.kevinsawicki.http.HttpRequest; //导入方法依赖的package包/类
@Override
public void error(Exception e, Context ctx, Object source) {
if (e instanceof CocoException || e instanceof HttpRequest.HttpRequestException)
e.printStackTrace();
else
Log.e(e);
}
示例9: routeServer
import com.github.kevinsawicki.http.HttpRequest; //导入方法依赖的package包/类
/**
* Route a server from remote
*/
void routeServer(){
Map<String, Object> params = Maps.newHashMap();
params.put("appName", client.getAppName());
params.put("clientPid", SysUtil.pid());
String[] serverList = client.getServers().split(",");
for (String server : serverList){
try {
// request route server
ServerRouteResp resp = doGet(server, ClientUris.SERVER_ROUTE, null, params, ServerRouteResp.class);
if(resp == null){
log.warn("server({}) route is null", server);
continue;
}
String routedServer = resp.getServer();
String clientId = resp.getClientId();
// register to the routed server
params.put("clientId", clientId);
params.put("appKey", client.getAppKey());
Boolean registerResp = doGet(routedServer, ClientUris.REGISTER, null, params, Boolean.class);
if (registerResp == Boolean.FALSE){
log.warn("failed to register server({})", routedServer);
continue;
}
// set current server
currentServer = routedServer;
// set headers for next requests
headers.put(Constants.CLIENT_ID_HEADER, clientId);
headers.put(Constants.APP_KEY_HEADER, client.getAppKey());
headers.put(Constants.CLIENT_VERSION_HEADER, CLIENT_VERSION);
headers.put(Constants.PULLING_TIMEOUT_HEADER, String.valueOf(client.getPullingTimeout()));
log.info("route successfully, current server is {}", currentServer);
// route successfully
return;
} catch (HttpRequest.HttpRequestException e){
log.warn("server({}) is unavailable, trying next server", server);
}
}
throw new Server503Exception("all servers are unavailable: " + Arrays.toString(serverList));
}