本文整理了Java中com.squareup.okhttp.Call.execute()
方法的一些代码示例,展示了Call.execute()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Call.execute()
方法的具体详情如下:
包路径:com.squareup.okhttp.Call
类名称:Call
方法名:execute
暂无
代码示例来源:origin: Javen205/IJPay
private String exec(com.squareup.okhttp.Request request) {
try {
com.squareup.okhttp.Response response = httpClient.newCall(request).execute();
if (!response.isSuccessful())
throw new RuntimeException("Unexpected code " + response);
return response.body().string();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
代码示例来源:origin: TommyLemon/APIJSON
/**
* @param client
* @param request
* @return
* @throws Exception
*/
private String getResponseJson(OkHttpClient client, Request request) throws Exception {
if (client == null || request == null) {
Log.e(TAG, "getResponseJson client == null || request == null >> return null;");
return null;
}
Response response = client.newCall(request).execute();
return response.isSuccessful() ? response.body().string() : null;
}
代码示例来源:origin: commonsguy/cw-omnibus
@Override
public void run() {
try {
OkHttpClient client=new OkHttpClient();
Request request=new Request.Builder().url(SO_URL).build();
Response response=client.newCall(request).execute();
if (response.isSuccessful()) {
Reader in=response.body().charStream();
BufferedReader reader=new BufferedReader(in);
SOQuestions questions=
new Gson().fromJson(reader, SOQuestions.class);
reader.close();
EventBus.getDefault().post(new QuestionsLoadedEvent(questions));
}
else {
Log.e(getClass().getSimpleName(), response.toString());
}
}
catch (Exception e) {
Log.e(getClass().getSimpleName(), "Exception parsing JSON", e);
}
}
}
代码示例来源:origin: commonsguy/cw-omnibus
.post(RequestBody.create(JSON, "{}"))
.build();
Response response=OKHTTP_CLIENT.newCall(request).execute();
.put(RequestBody.create(ZIP, backup))
.build();
response=OKHTTP_CLIENT.newCall(request).execute();
代码示例来源:origin: aa112901/remusic
Response response = mOkHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
String c = response.body().string();
代码示例来源:origin: aa112901/remusic
Response response = mOkHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
String c = response.body().string();
代码示例来源:origin: android10/Android-CleanArchitecture
private void connectToApi() {
OkHttpClient okHttpClient = this.createClient();
final Request request = new Request.Builder()
.url(this.url)
.addHeader(CONTENT_TYPE_LABEL, CONTENT_TYPE_VALUE_JSON)
.get()
.build();
try {
this.response = okHttpClient.newCall(request).execute().body().string();
} catch (IOException e) {
e.printStackTrace();
}
}
代码示例来源:origin: TommyLemon/APIJSON
/**
* @param client
* @param request
* @return
* @throws Exception
*/
private String getResponseJson(OkHttpClient client, Request request) throws Exception {
if (client == null || request == null) {
Log.e(TAG, "getResponseJson client == null || request == null >> return null;");
return null;
}
Response response = client.newCall(request).execute();
return response.isSuccessful() ? response.body().string() : null;
}
代码示例来源:origin: TommyLemon/APIJSON
/**
* @param client
* @param request
* @return
* @throws Exception
*/
private String getResponseJson(OkHttpClient client, Request request) throws Exception {
if (client == null || request == null) {
Log.e(TAG, "getResponseJson client == null || request == null >> return null;");
return null;
}
Response response = client.newCall(request).execute();
return response.isSuccessful() ? response.body().string() : null;
}
代码示例来源:origin: aa112901/remusic
public static void getOut(final String url) {
try {
mOkHttpClient.setConnectTimeout(1000, TimeUnit.MINUTES);
mOkHttpClient.setReadTimeout(1000, TimeUnit.MINUTES);
Request request = new Request.Builder()
.url(url)
.build();
Response response = mOkHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
FileOutputStream fo = new FileOutputStream("/storage/emulated/0/" + "gedangein" + ".json");
byte[] c = new byte[1024];
while (response.body().source().read(c) != -1) {
fo.write(c);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
代码示例来源:origin: commonsguy/cw-omnibus
@Override
public void onHandleWork(@NonNull Intent i) {
Request request=new Request.Builder()
.url(i.getData().toString())
.build();
try {
Response response=
BackupService.OKHTTP_CLIENT.newCall(request).execute();
File toRestore=new File(getCacheDir(), "backup.zip");
if (toRestore.exists()) {
toRestore.delete();
}
BufferedSink sink = Okio.buffer(Okio.sink(toRestore));
sink.writeAll(response.body().source());
sink.close();
ZipUtils.unzip(toRestore, getFilesDir(),
BackupService.ZIP_PREFIX_FILES);
ZipUtils.unzip(toRestore,
BackupService.getSharedPrefsDir(this),
BackupService.ZIP_PREFIX_PREFS);
ZipUtils.unzip(toRestore, getExternalFilesDir(null),
BackupService.ZIP_PREFIX_EXTERNAL);
EventBus.getDefault().post(new RestoreCompletedEvent());
}
catch (Exception e) {
Log.e(getClass().getSimpleName(),
"Exception restoring backup", e);
EventBus.getDefault().post(new RestoreFailedEvent());
}
}
代码示例来源:origin: nostra13/Android-Universal-Image-Loader
@Override
protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {
Request request = new Request.Builder().url(imageUri).build();
ResponseBody responseBody = client.newCall(request).execute().body();
InputStream inputStream = responseBody.byteStream();
int contentLength = (int) responseBody.contentLength();
return new ContentLengthInputStream(inputStream, contentLength);
}
}
代码示例来源:origin: aa112901/remusic
@Override
public void run() {
try {
mOkHttpClient.setConnectTimeout(1000, TimeUnit.MINUTES);
mOkHttpClient.setReadTimeout(1000, TimeUnit.MINUTES);
Request request = new Request.Builder()
.url(url)
.build();
Response response = mOkHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
FileOutputStream fo = new FileOutputStream("/storage/emulated/0/" + name + ".mp3");
byte[] c = new byte[1024];
while (response.body().source().read(c) != -1) {
fo.write(c);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
代码示例来源:origin: aa112901/remusic
public static String getResposeString(String action1) {
try {
mOkHttpClient.setConnectTimeout(1000, TimeUnit.MINUTES);
mOkHttpClient.setReadTimeout(1000, TimeUnit.MINUTES);
Request request = new Request.Builder()
.url(action1)
.build();
Response response = mOkHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
String c = response.body().string();
Log.e("billboard", c);
return c;
}
} catch (Exception e) {
e.printStackTrace();
}
// mOkHttpClient.setCookieHandler(new CookieManager(
// new PersistentCookieStore(getContext().getApplicationContext()),
// CookiePolicy.ACCEPT_ALL));
return null;
}
代码示例来源:origin: aa112901/remusic
public static Bitmap getBitmapStream(Context context, String url, boolean forceCache) {
try {
File sdcache = context.getExternalCacheDir();
//File cacheFile = new File(context.getCacheDir(), "[缓存目录]");
Cache cache = new Cache(sdcache.getAbsoluteFile(), 1024 * 1024 * 30); //30Mb
mOkHttpClient.setCache(cache);
mOkHttpClient.setConnectTimeout(1000, TimeUnit.MINUTES);
mOkHttpClient.setReadTimeout(1000, TimeUnit.MINUTES);
Request.Builder builder = new Request.Builder()
.url(url);
if (forceCache) {
builder.cacheControl(CacheControl.FORCE_CACHE);
}
Request request = builder.build();
Response response = mOkHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
return _decodeBitmapFromStream(response.body().byteStream(), 160, 160);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
代码示例来源:origin: jiangqqlmj/FastDev4Android
@Override
public void run() {
try {
response= client.newCall(request).execute();
Headers responseHeaders = response.headers();
for (int i = 0; i < responseHeaders.size(); i++) {
Log.d("zttjiangqq", responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
final String message=response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
tv_result.setText(message);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
代码示例来源:origin: Javen205/IJPay
@Override
public InputStream download(String url, String params) {
com.squareup.okhttp.Request request;
if (StrKit.notBlank(params)) {
com.squareup.okhttp.RequestBody body = com.squareup.okhttp.RequestBody.create(CONTENT_TYPE_FORM,
params);
request = new com.squareup.okhttp.Request.Builder().url(url).post(body).build();
} else {
request = new com.squareup.okhttp.Request.Builder().url(url).get().build();
}
try {
com.squareup.okhttp.Response response = httpClient.newCall(request).execute();
if (!response.isSuccessful())
throw new RuntimeException("Unexpected code " + response);
return response.body().byteStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
代码示例来源:origin: aa112901/remusic
public static void postUrl(Context context, String j) {
try {
String action = "https://music.163.com/weapi/login/";
RequestBody formBody = new FormEncodingBuilder()
// .add("",)
.build();
Log.e("post", "p");
Request request = new Request.Builder()
.url(action)
.header("Content-Type", "application/x-www-form-urlencoded")
.header("Host", "music.163.com")
.header("Cookie", "appver=1.5.0.75771")
.header("Referer", "http://music.163.com/")
.header("Connection", "keep-alive")
.header("Accept-Encoding", "gzip,deflate")
.header("Accept", "*/*")
.header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36")
.post(formBody)
.build();
mOkHttpClient.setCookieHandler(new CookieManager(
new PersistentCookieStore(context.getApplicationContext()),
CookiePolicy.ACCEPT_ALL));
Response response = mOkHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
Log.e("respose", response.body().string());
}
} catch (Exception e) {
e.printStackTrace();
}
}
代码示例来源:origin: facebook/stetho
@Test
public void testWithResponseCompression() throws IOException {
ByteArrayOutputStream capturedOutput = hookAlmostRealInterpretResponseStream(mMockEventReporter);
byte[] uncompressedData = repeat(".", 1024).getBytes();
byte[] compressedData = compress(uncompressedData);
MockWebServer server = new MockWebServer();
server.start();
server.enqueue(new MockResponse()
.setBody(new Buffer().write(compressedData))
.addHeader("Content-Encoding: gzip"));
Request request = new Request.Builder()
.url(server.url("/"))
.build();
Response response = mClientWithInterceptor.newCall(request).execute();
// Verify that the final output and the caller both saw the uncompressed stream.
assertArrayEquals(uncompressedData, response.body().bytes());
assertArrayEquals(uncompressedData, capturedOutput.toByteArray());
// And verify that the StethoInterceptor was able to see both.
Mockito.verify(mMockEventReporter)
.dataReceived(
anyString(),
eq(compressedData.length),
eq(uncompressedData.length));
server.shutdown();
}
代码示例来源:origin: facebook/stetho
.post(compressedBody)
.build();
Response response = mClientWithInterceptor.newCall(request).execute();
内容来源于网络,如有侵权,请联系作者删除!