Search in sources :

Example 1 with ProgressRequestBody

use of io.chelizi.amokhttp.upload.ProgressRequestBody in project amhttp by Eddieyuan123.

the class RequestManager method upload.

public <T> void upload(Context context, String url, CacheControl cacheControl, HashMap<String, String> headers, File file, String fileName, HashMap<String, String> params, Object tag, final OnUploadListener<T> listener) {
    try {
        IRequestBodyFactory requestBodyFactory = new RequestBodyFactoryImpl();
        RequestBody requestBody = requestBodyFactory.buildRequestBody(file, fileName, params);
        final Request request = new Request.Builder().cacheControl(cacheControl == null ? CacheControl.FORCE_NETWORK : cacheControl).headers(Headers.of(headers)).tag(tag == null ? context.hashCode() : tag).url(url).post(new ProgressRequestBody(requestBody, new ProgressRequestListener() {

            @Override
            public void onRequestProgress(final long bytesWritten, final long contentLength, final boolean done) {
                if (listener != null) {
                    Dispatcher dispatcher = Dispatcher.getDispatcher(Looper.getMainLooper());
                    dispatcher.setRequestListener(listener);
                    Message message = Message.obtain();
                    message.what = MessageConstant.MESSAGE_UPLOAD_PROGRESS;
                    Bundle bundle = new Bundle();
                    bundle.putLong("bytesWritten", bytesWritten);
                    bundle.putLong("contentLength", contentLength);
                    bundle.putBoolean("done", done);
                    message.obj = bundle;
                    dispatcher.sendMessage(message);
                }
            }
        })).build();
        RequestUtils.enqueue(mOkHttpClient, request, listener);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Message(android.os.Message) Bundle(android.os.Bundle) Request(okhttp3.Request) ProgressRequestListener(io.chelizi.amokhttp.upload.ProgressRequestListener) ProgressRequestBody(io.chelizi.amokhttp.upload.ProgressRequestBody) ProgressRequestBody(io.chelizi.amokhttp.upload.ProgressRequestBody) RequestBody(okhttp3.RequestBody)

Aggregations

Bundle (android.os.Bundle)1 Message (android.os.Message)1 ProgressRequestBody (io.chelizi.amokhttp.upload.ProgressRequestBody)1 ProgressRequestListener (io.chelizi.amokhttp.upload.ProgressRequestListener)1 Request (okhttp3.Request)1 RequestBody (okhttp3.RequestBody)1