use of io.chelizi.amokhttp.upload.ProgressRequestListener 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();
}
}
Aggregations