All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dreamlu.mica.http.AsyncCall Maven / Gradle / Ivy

There is a newer version: 3.3.2
Show newest version
/*
 * Copyright (c) 2019-2029, Dreamlu ([email protected] & www.dreamlu.net).
 * 

* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* http://www.gnu.org/licenses/lgpl.html *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.dreamlu.mica.http; import okhttp3.Call; import okhttp3.Request; import java.io.IOException; import java.util.function.BiConsumer; import java.util.function.Consumer; /** * 异步执行器 * * @author L.cm */ public class AsyncCall { private final static Consumer DEFAULT_CONSUMER = (r) -> {}; private final static BiConsumer DEFAULT_FAIL_CONSUMER = (r, e) -> {}; private final Call call; private Consumer successConsumer; private Consumer responseConsumer; private BiConsumer failedBiConsumer; AsyncCall(Call call) { this.call = call; this.successConsumer = DEFAULT_CONSUMER; this.responseConsumer = DEFAULT_CONSUMER; this.failedBiConsumer = DEFAULT_FAIL_CONSUMER; } public void onSuccessful(Consumer consumer) { this.successConsumer = consumer; this.execute(); } public void onResponse(Consumer consumer) { this.responseConsumer = consumer; this.execute(); } public AsyncCall onFailed(BiConsumer biConsumer) { this.failedBiConsumer = biConsumer; return this; } private void execute() { call.enqueue(new AsyncCallback(this)); } void onResponse(HttpResponse httpResponse) { responseConsumer.accept(httpResponse); } void onSuccessful(HttpResponse httpResponse) { successConsumer.accept(httpResponse); } void onFailure(Request request, IOException e) { failedBiConsumer.accept(request, e); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy