本文整理汇总了Java中com.squareup.okhttp.Call.execute方法的典型用法代码示例。如果您正苦于以下问题:Java Call.execute方法的具体用法?Java Call.execute怎么用?Java Call.execute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.squareup.okhttp.Call
的用法示例。
在下文中一共展示了Call.execute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: _getAsyn
import com.squareup.okhttp.Call; //导入方法依赖的package包/类
/**
* 同步的Get请求
*
* @param url
* @return Response
*/
private Response _getAsyn(String url) throws IOException {
final Request request = new Request.Builder()
.url(url)
.build();
Call call = mOkHttpClient.newCall(request);
Response execute = call.execute();
return execute;
}
示例2: invoke
import com.squareup.okhttp.Call; //导入方法依赖的package包/类
@Override
public <T> T invoke(Class<T> clazz) throws IOException
{
final Call call = mOkHttpClient.newCall(request);
Response response = call.execute();
return (T) saveFile(response, null);
}
示例3: _getAsyn
import com.squareup.okhttp.Call; //导入方法依赖的package包/类
/**
* 同步的Get请求
*
* @param url url
* @return Response
*/
private Response _getAsyn(String url) throws IOException {
final Request request = new Request.Builder()
.url(url)
.build();
Call call = mOkHttpClient.newCall(request);
return call.execute();
}
示例4: accept
import com.squareup.okhttp.Call; //导入方法依赖的package包/类
public void accept(MattermostPost inPost) {
List<MattermostPost> toSend = Lists.newArrayList();
/*
* the actual limit is 4000 in mattermost.
*/
if (inPost.getText().length() > 3900) {
for (String subPost : Splitter.fixedLength(3900).split(inPost.getText())) {
MattermostPost splitPost = new MattermostPost(subPost, inPost.getUsername(), inPost.getIconUrl());
toSend.add(splitPost);
}
} else {
toSend.add(inPost);
}
for (MattermostPost post : toSend) {
String payload = gson.toJson(post);
RequestBody body = new FormEncodingBuilder().add("payload", payload).build();
HttpUrl url = new HttpUrl.Builder().scheme(scheme).host(host).addPathSegment("hooks")
.addPathSegment(channel).build();
System.out.println(url);
Request request = new Request.Builder().url(url).post(body).build();
Response response;
try {
Call call = client.newCall(request);
response = call.execute();
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response + " canceled ?:" + call.isCanceled() + " payload:\n" + payload + "\n");
}
System.out.println(response.message());
response.body().close();
} catch (IOException e) {
System.out.println("Message was length:" + post.getText().length() + "\n" + post);
throw new RuntimeException(e);
}
}
}
示例5: execute
import com.squareup.okhttp.Call; //导入方法依赖的package包/类
/**
* Execute HTTP call and deserialize the HTTP response body into the given return type.
*
* @param returnType The return type used to deserialize HTTP response body
* @param <T> The return type corresponding to (same with) returnType
* @param call Call
* @return ApiResponse object containing response status, headers and
* data, which is a Java object deserialized from response body and would be null
* when returnType is null.
* @throws ApiException If fail to execute the call
*/
public <T> ApiResponse<T> execute(Call call, Type returnType) throws ApiException {
try {
Response response = call.execute();
LOGGER.info("Method {execute} was called, with params isExecuted: {}, returnType: {}", call.isExecuted(), returnType==null?null:returnType.getTypeName());
T data = handleResponse(response, returnType);
return new ApiResponse<T>(response.code(), response.headers().toMultimap(), data);
} catch (IOException e) {
LOGGER.error("Method {execute} was called, with params :{},{}", call.isExecuted(), returnType==null?null:returnType.getTypeName());
LOGGER.error("Method {execute} throw exception: "+ e);
throw new ApiException(e);
}
}
示例6: _getAsyn
import com.squareup.okhttp.Call; //导入方法依赖的package包/类
/**
* 同步的Get请求
*
* @param url
* @return Response
*/
private Response _getAsyn(String url) throws IOException {
final Request request = new Request.Builder().url(url).build();
Call call = mOkHttpClient.newCall(request);
Response execute = call.execute();
return execute;
}
示例7: requestNewToken
import com.squareup.okhttp.Call; //导入方法依赖的package包/类
private void requestNewToken(String grantType) throws IOException {
Map<String, String> localVarHeaderParams = new HashMap<>();
localVarHeaderParams.put("Accept", "application/json");
String method = "GET";
String[] localVarAuthNames = new String[] {};
List<Pair> localVarQueryParams = new ArrayList<>();
Map<String, Object> formParams = new HashMap<>();
localVarQueryParams.addAll(apiClient.parameterToPairs("", "grant_type", grantType));
if ("refresh_token".equals(grantType) || "password".equals(grantType)) {
String client = authenticationProperties.get(KEY_CLIENT);
String secret = authenticationProperties.get(KEY_SECRET);
byte[] creds = (client + ":" + secret).getBytes();
localVarHeaderParams.put("Authorization", "Basic " + Base64.getEncoder().encodeToString(creds));
}
if ("refresh_token".equals(grantType)) {
localVarQueryParams.addAll(apiClient.parameterToPairs("", "refresh_token", currentToken.getRefreshToken()));
}
if ("password".equals(grantType)) {
localVarHeaderParams.put("Content-Type", "multipart/form-data");
method = "POST";
formParams.put("username", authenticationProperties.get(KEY_USERNAME));
formParams.put("password", authenticationProperties.get(KEY_PASSWORD));
}
Response response = null;
try {
Request r = apiClient.buildRequest("/oauth/token", method, localVarQueryParams, null, localVarHeaderParams, formParams, localVarAuthNames, null);
logger.debug(r.toString());
Call call = apiClient.buildCall(r);
response = call.execute();
if (response.isSuccessful()) {
MyOAuthToken newToken = apiClient.deserialize(response, myOAuthTokenType);
newToken.computeExpirationDate();
currentToken = newToken;
} else {
throw new IOException("Unable to get an authentication token (" + response.code() + ") : Msg(" + response.message() + ") Body("
+ (response.body() != null ? response.body().string() : "") + ")");
}
} catch (ApiException e) {
throw new IOException(e);
} finally {
if (response != null) {
response.body().close();
}
}
}
示例8: getInputStream
import com.squareup.okhttp.Call; //导入方法依赖的package包/类
private Response getInputStream() throws IOException
{
Call call = mOkHttpClient.newCall(request);
return call.execute();
}
示例9: execute
import com.squareup.okhttp.Call; //导入方法依赖的package包/类
@Override
public Response execute(Request request) throws Exception {
Call call = mClient.newCall(makeRequest(request));
request.handle().setHandle(new CallHandle(call));
return new OkResponse(call.execute(),request);
}
示例10: get
import com.squareup.okhttp.Call; //导入方法依赖的package包/类
/**
* 通用的方法
*/
public Response get(Request request) throws IOException {
Call call = mOkHttpClient.newCall(request);
Response execute = call.execute();
return execute;
}
示例11: doInBackground
import com.squareup.okhttp.Call; //导入方法依赖的package包/类
@Override
protected Boolean doInBackground(Request... params) {
Call call = getInstance().mOkHttpClient.newCall(params[0]);
try {
Response response = call.execute();
if (response.code() == 200) {
InputStream inputStream = null;
OutputStream output = null;
try {
inputStream = response.body().byteStream();
File file = new File(context.getCacheDir(), "download.liubo");
output = new FileOutputStream(file);
byte[] buff = new byte[1024 * 4];
long downloaded = 0;
long target = response.body().contentLength();
publishProgress(0L, target);
while (true) {
int readed = inputStream.read(buff);
if (readed == -1) {
break;
}
//write buff
output.write(buff, 0, readed);
downloaded += readed;
publishProgress(downloaded, target);
if (isCancelled()) {
return false;
}
}
output.flush();
return downloaded == target;
} catch (IOException ignore) {
return false;
} finally {
if (inputStream != null) {
inputStream.close();
}
if (output != null) {
output.close();
}
}
} else {
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
示例12: invoke
import com.squareup.okhttp.Call; //导入方法依赖的package包/类
@Override
public <T> T invoke(Class<T> clazz) throws IOException {
final Call call = mOkHttpClient.newCall(request);
Response response = call.execute();
return (T) saveFile(response, null);
}
示例13: postRpc
import com.squareup.okhttp.Call; //导入方法依赖的package包/类
private static String postRpc(Method method, Object[] args) {
String responseStr = null;
boolean retry = true;
int executionCount = 0;
while (retry) {
try {
if (executionCount > 0) {
Thread.sleep(RETRY_SLEEP_TIME_MILLIS * executionCount);
}
executionCount++;
Request.Builder builder = new Request.Builder().url(Config.HTTP_START + "/androidRpc?method=" + method.getName());
if (args != null) {
// String encode = ToolStream.encode(args);
// LOG.debug("encoded string : " + encode.length());
// LOG.debug("gzipped string : " + ToolStream.encodeGzip(args).length());
Object[] args_logs = new Object[3];
args_logs[0] = args;
builder.post(RequestBody.create(MediaType.parse("*/*"), Streams.encodeGzip(args_logs)));
} else {
builder.post(RequestBody.create(MediaType.parse("*/*"), ""));
}
LOG.info("postRpc " + method.getName() + "(" + Joiner.on(',').useForNull("null").join(args) + ")");
Request request = builder.build();
Call call = client.newCall(request);
Response response = call.execute();
responseStr = response.body().string();
retry = false;
} catch (Throwable e) {
LOG.error("Failed androidRpc (" + e.getClass().getCanonicalName() + ") executionCount=" + executionCount + " : " + method.getName() + " : " + Arrays.toString(args));
if (e instanceof InterruptedIOException || e instanceof SSLHandshakeException) {
retry = false;
} else if (executionCount >= DEFAULT_MAX_RETRIES) {
retry = false;
}
if (e instanceof UnknownHostException || e instanceof SocketException) {
notifyNetworkError();
}
}
}
return responseStr;
}
示例14: execute
import com.squareup.okhttp.Call; //导入方法依赖的package包/类
/**
* Execute HTTP call and deserialize the HTTP response body into the given return type.
*
* @param returnType The return type used to deserialize HTTP response body
* @param <T> The return type corresponding to (same with) returnType
* @param call Call
* @return ApiResponse object containing response status, headers and
* data, which is a Java object deserialized from response body and would be null
* when returnType is null.
* @throws ApiException If fail to execute the call
*/
public <T> ApiResponse<T> execute(Call call, Type returnType) throws ApiException {
try {
Response response = call.execute();
T data = handleResponse(response, returnType);
return new ApiResponse<T>(response.code(), response.headers().toMultimap(), data);
} catch (IOException e) {
throw new ApiException(e);
}
}
示例15: testStuff
import com.squareup.okhttp.Call; //导入方法依赖的package包/类
@Test
public void testStuff() throws IOException {
Call call = client.newRequestToken("oob");
Response response = call.execute();
assertThat(response.code()).isEqualTo(200);
assertThat(response.body().string()).isNull();
}