本文整理汇总了Java中com.squareup.okhttp.Callback类的典型用法代码示例。如果您正苦于以下问题:Java Callback类的具体用法?Java Callback怎么用?Java Callback使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Callback类属于com.squareup.okhttp包,在下文中一共展示了Callback类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: shouldSendGetRequestAndReceiveException
import com.squareup.okhttp.Callback; //导入依赖的package包/类
@Test
public void shouldSendGetRequestAndReceiveException() {
final Request request = new com.squareup.okhttp.Request.Builder()
.url(URI)
.method("GET", null)
.build();
when(okHttpClient.newCall(argThat(new RequestMatcher(request)))).thenReturn(call);
doAnswer(invocation -> {
final Callback callback = invocation.getArgument(0);
callback.onFailure(request, new IOException());
return Void.TYPE;
}).when(call).enqueue(isA(Callback.class));
final CompletionStage<com.spotify.apollo.Response<ByteString>> response =
client.send(com.spotify.apollo.Request.forUri(URI, "GET").withTtl(Duration.ofMillis(100)));
verify(okHttpClient).setReadTimeout(100, TimeUnit.MILLISECONDS);
assertTrue(response.toCompletableFuture().isCompletedExceptionally());
}
示例2: post
import com.squareup.okhttp.Callback; //导入依赖的package包/类
Call post(Callback callback) throws IOException {
OkHttpClient client = getUnsafeOkHttpClient();
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
client.setCookieHandler(cookieManager);
RequestBody requestBody = new FormEncodingBuilder()
.add("user_id", NetId)
.add("user_password", password)
.build();
Request request = new Request.Builder()
.url("https://studentmaintenance.webapps.snu.edu.in/students/public/studentslist/studentslist/loginauth")
.post(requestBody)
.build();
Call call = client.newCall(request);
call.enqueue(callback);
return call;
}
示例3: startParse
import com.squareup.okhttp.Callback; //导入依赖的package包/类
public static void startParse(final String storeName, final Context context, final SpiceManager spiceManager) {
Toast.makeText(context, AptoideUtils.StringUtils.getFormattedString(context, R.string.subscribing, storeName), Toast.LENGTH_SHORT).show();
GetSimpleStoreRequest request = new GetSimpleStoreRequest();
request.store_name = storeName;
CheckSimpleStoreListener checkStoreListener = new CheckSimpleStoreListener();
checkStoreListener.callback = new CheckSimpleStoreListener.Callback() {
@Override
public void onSuccess() {
addStoreOnCloud(storeName, context, spiceManager);
}
};
spiceManager.execute(request, checkStoreListener);
}
示例4: knock
import com.squareup.okhttp.Callback; //导入依赖的package包/类
/**
* Execute a simple request (knock at the door) to the given URL.
* @param url
*/
public static void knock(String url) {
OkHttpClient client = new OkHttpClient();
Request click = new Request.Builder().url(url).build();
client.newCall(click).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
}
});
}
示例5: execute
import com.squareup.okhttp.Callback; //导入依赖的package包/类
public void execute(final Task task) {
log.info("Started task with {} urls", task.getUrls().size());
task.start();
for(int i = 0; i < task.getUrls().size(); i++) {
final int index = i;
final long time = System.currentTimeMillis();
String url = task.getUrls().get(i);
Request req = new Request.Builder().get().url(url).build();
client.newCall(req).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
task.fail(index, time, request, e);
}
@Override
public void onResponse(Response response) throws IOException {
task.success(index, time, response);
}
});
}
}
示例6: downloadSubtitle
import com.squareup.okhttp.Callback; //导入依赖的package包/类
public void downloadSubtitle() {
if (listenerReference == null) throw new IllegalArgumentException("listener must not null. Call setSubtitleDownloaderListener() to sets one");
if (contextReference.get() == null) return;
Context context = contextReference.get();
SubsProvider.download(context, media, subtitleLanguage, new Callback() {
@Override
public void onFailure(Request request, IOException exception) {
onSubtitleDownloadFailed();
}
@Override
public void onResponse(Response response) throws IOException {
onSubtitleDownloadSuccess();
}
});
}
示例7: resetAccount
import com.squareup.okhttp.Callback; //导入依赖的package包/类
/**
* Remove the staled account and add a new one
*/
private void resetAccount() {
logout(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
}
});
// use Authenticator to update account
mCloudProvider.removeAccount(mAccount);
mCloudProvider.addAccount(getClass().getCanonicalName(), (Activity) mContext);
}
示例8: shouldSendGetRequest
import com.squareup.okhttp.Callback; //导入依赖的package包/类
@Test
public void shouldSendGetRequest() throws ExecutionException, InterruptedException {
final Request request = new com.squareup.okhttp.Request.Builder()
.url(URI)
.method("GET", null)
.build();
when(okHttpClient.newCall(argThat(new RequestMatcher(request)))).thenReturn(call);
doAnswer(invocation -> {
final Callback callback = invocation.getArgument(0);
callback.onResponse(new Response.Builder()
.request(request)
.protocol(Protocol.HTTP_1_1)
.code(Status.OK.code())
.message("OK")
.body(ResponseBody.create(CONTENT_TYPE, "{}"))
.header("foo", "bar")
.build());
return Void.TYPE;
}).when(call).enqueue(isA(Callback.class));
final com.spotify.apollo.Response<ByteString> response =
client.send(com.spotify.apollo.Request.forUri(URI, "GET")).toCompletableFuture().get();
verify(okHttpClient, never()).setReadTimeout(anyLong(), any());
assertEquals(Optional.of(ByteString.of("{}".getBytes())), response.payload());
assertEquals(Optional.of("bar"), response.header("foo"));
}
示例9: shouldSendPostRequest
import com.squareup.okhttp.Callback; //导入依赖的package包/类
@Test
public void shouldSendPostRequest() throws ExecutionException, InterruptedException {
final Request request = new com.squareup.okhttp.Request.Builder()
.url(URI)
.method("POST", RequestBody.create(CONTENT_TYPE, "{}"))
.build();
when(okHttpClient.newCall(argThat(new RequestMatcher(request)))).thenReturn(call);
doAnswer(invocation -> {
final Callback callback = invocation.getArgument(0);
callback.onResponse(new Response.Builder()
.request(request)
.protocol(Protocol.HTTP_1_1)
.code(Status.OK.code())
.message("OK")
.body(ResponseBody.create(CONTENT_TYPE, "{}"))
.header("foo", "bar")
.build());
return Void.TYPE;
}).when(call).enqueue(isA(Callback.class));
final com.spotify.apollo.Response<ByteString> response =
client.send(com.spotify.apollo.Request
.forUri(URI, "POST")
.withPayload(ByteString.of("{}".getBytes())))
.toCompletableFuture().get();
verify(okHttpClient, never()).setReadTimeout(anyLong(), any());
assertEquals(Optional.of(ByteString.of("{}".getBytes())), response.payload());
assertEquals(Optional.of("bar"), response.header("foo"));
}
示例10: AuthTask
import com.squareup.okhttp.Callback; //导入依赖的package包/类
AuthTask(String urlPart, String serverAddr, int method, String json, Callback callback) {
this.urlPart = urlPart;
this.serverAddr = serverAddr;
this.method = method;
this.json = json;
this.callback = callback;
}
示例11: post
import com.squareup.okhttp.Callback; //导入依赖的package包/类
Call post(String url, String json, Callback callback) {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.addHeader("Content-Type","application/json")
.addHeader("Authorization","key=AIzaSyAkeFnKc_r6TJSO7tm5OVnzkbni6dEk4Lw")
.url(url)
.post(body)
.build();
Call call = client.newCall(request);
call.enqueue(callback);
return call;
}
示例12: sendPushNotification
import com.squareup.okhttp.Callback; //导入依赖的package包/类
public void sendPushNotification(String token, String title, String message){
try {
JSONObject jsonObject = new JSONObject();
JSONObject notification = new JSONObject();
notification.put("title", title);
notification.put("body", message);
JSONObject data = new JSONObject();
data.put("sender_phone", sender.getPhone());
data.put("sender_photo", sender.getPhoto());
data.put("sender_name", sender.getName());
jsonObject.put("notification", notification);
jsonObject.put("data", data);
jsonObject.put("to", token);
post("https://fcm.googleapis.com/fcm/send", jsonObject.toString(), new Callback() {
@Override
public void onFailure(Request request, IOException e) {
FirebaseCrash.report(e);
}
@Override
public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
String responseStr = response.body().string();
Log.d("Response", responseStr);
// Do what you want to do with the response.
} else {
// Request not successful
}
}
});
} catch (JSONException ex) {
Log.d("Exception", "JSON exception", ex);
}
}
示例13: post
import com.squareup.okhttp.Callback; //导入依赖的package包/类
Call post(String mode, String username, String password, Callback callback) throws IOException {
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormEncodingBuilder()
.add("mode", mode)
.add("username", username + "@snu.in")
.add("password", password)
.build();
Request request = new Request.Builder()
.url("http://192.168.50.1/24online/servlet/E24onlineHTTPClient")
.post(formBody)
.build();
Call call = client.newCall(request);
call.enqueue(callback);
return call;
}
示例14: AuthTaskUrlShortener
import com.squareup.okhttp.Callback; //导入依赖的package包/类
public AuthTaskUrlShortener(final Callback mCallBack, final String longUrl, Activity context, Account account, JSONObject jsonObject, String requestType){
this.mOkHttpClient = new OkHttpClient();
this.mCallBack = mCallBack;
this.longUrl = longUrl;
this.mActivity = context;
this.mAccount = account;
this.mJsonObject = jsonObject;
this.mRequestType = requestType;
}
开发者ID:NordicSemiconductor,项目名称:Android-nRF-Beacon-for-Eddystone,代码行数:10,代码来源:AuthTaskUrlShortener.java
示例15: ListProjectsTask
import com.squareup.okhttp.Callback; //导入依赖的package包/类
public ListProjectsTask(Context context, String url, Callback callback, String token) {
this.mContext = context;
this.url = url;
this.callback = callback;
httpClient = new OkHttpClient();
this.mToken = token;
}