Java Code Examples for com.squareup.okhttp.Call#enqueue()

The following examples show how to use com.squareup.okhttp.Call#enqueue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Ok2Lite.java    From httplite with Apache License 2.0 6 votes vote down vote up
@Override
public void enqueue(final Request request,final Callback<Response> callback) {
    Call call = mClient.newCall(makeRequest(request));
    request.handle().setHandle(new CallHandle(call));
    com.squareup.okhttp.Callback ok2Callback = new com.squareup.okhttp.Callback() {
        @Override
        public void onFailure(com.squareup.okhttp.Request okReq, IOException e) {
            Exception throwable;
            if("Canceled".equals(e.getMessage())){
                throwable = new CanceledException(e);
            }else{
                throwable = e;
            }
            callback.onFailed(request,throwable);
        }

        @Override
        public void onResponse(com.squareup.okhttp.Response response) throws IOException {
            callback.onSuccess(request,response.headers().toMultimap(),new OkResponse(response,request));
        }
    };
    call.enqueue(ok2Callback);
}
 
Example 2
Source File: OkHttpProxy.java    From JianDan_OkHttp with Apache License 2.0 6 votes vote down vote up
public static Call post(String url, Map<String, String> params, Object tag, OkHttpCallback responseCallback) {

        Request.Builder builder = new Request.Builder().url(url);
        if (tag != null) {
            builder.tag(tag);
        }

        FormEncodingBuilder encodingBuilder = new FormEncodingBuilder();

        if (params != null && params.size() > 0) {
            for (String key : params.keySet()) {
                encodingBuilder.add(key, params.get(key));
            }
        }

        RequestBody formBody = encodingBuilder.build();
        builder.post(formBody);

        Request request = builder.build();
        Call call = getInstance().newCall(request);
        call.enqueue(responseCallback);
        return call;
    }
 
Example 3
Source File: UploadFileRequest.java    From lunzi with Apache License 2.0 5 votes vote down vote up
private void newRequestCall(Callback callback, String url, RequestBody requestBody) {
    Request request = new Request.Builder()
            .url(url)
            .post(requestBody)
            .build();
    Call call = mOkHttpClient.newCall(request);
    call.enqueue(callback);
}
 
Example 4
Source File: OkHttpProxy.java    From JianDan_OkHttp with Apache License 2.0 5 votes vote down vote up
public static Call get(String url, Object tag, OkHttpCallback responseCallback) {
    Request.Builder builder = new Request.Builder().url(url);
    if (tag != null) {
        builder.tag(tag);
    }
    Request request = builder.build();
    Call call = getInstance().newCall(request);
    call.enqueue(responseCallback);
    return call;
}