Search in sources :

Example 6 with ProgressCallback

use of com.okhttplib.callback.ProgressCallback in project OkHttp3 by MrZhousf.

the class DownloadActivity method downloadFile.

private void downloadFile() {
    final HttpInfo info = HttpInfo.Builder().addDownloadFile(fileURL, "myMP4", new ProgressCallback() {

        @Override
        public void onProgressMain(int percent, long bytesWritten, long contentLength, boolean done) {
            downloadProgress.setProgress(percent);
            tvResult.setText(percent + "%");
            LogUtil.d(TAG, "下载进度:" + percent);
        }

        @Override
        public void onResponseMain(String filePath, HttpInfo info) {
            tvResult.setText(info.getRetDetail());
            LogUtil.d(TAG, "下载结果:" + info.getRetDetail());
        }
    }).build();
    OkHttpUtil.Builder().setReadTimeout(120).build(//绑定请求标识
    requestTag).doDownloadFileAsync(info);
}
Also used : HttpInfo(com.okhttplib.HttpInfo) ProgressCallback(com.okhttplib.callback.ProgressCallback)

Example 7 with ProgressCallback

use of com.okhttplib.callback.ProgressCallback in project OkHttp3 by MrZhousf.

the class DownloadBreakpointsActivity method download.

private void download() {
    if (null == fileInfo)
        fileInfo = new DownloadFileInfo(url, "test", new ProgressCallback() {

            @Override
            public void onProgressMain(int percent, long bytesWritten, long contentLength, boolean done) {
                downloadProgress.setProgress(percent);
                tvResult.setText(percent + "%");
                LogUtil.d(TAG, "下载进度:" + percent);
            }

            @Override
            public void onResponseMain(String filePath, HttpInfo info) {
                if (info.isSuccessful()) {
                    tvResult.setText(info.getRetDetail() + "\n下载状态:" + fileInfo.getDownloadStatus());
                } else {
                    Toast.makeText(DownloadBreakpointsActivity.this, info.getRetDetail(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    HttpInfo info = HttpInfo.Builder().addDownloadFile(fileInfo).build();
    OkHttpUtil.Builder().setReadTimeout(120).build(this).doDownloadFileAsync(info);
}
Also used : HttpInfo(com.okhttplib.HttpInfo) DownloadFileInfo(com.okhttplib.bean.DownloadFileInfo) ProgressCallback(com.okhttplib.callback.ProgressCallback)

Example 8 with ProgressCallback

use of com.okhttplib.callback.ProgressCallback in project OkHttp3 by MrZhousf.

the class UploadImageActivity method uploadImgTwo.

/**
     * 上传第二张图片
     */
private void uploadImgTwo() {
    HttpInfo info = HttpInfo.Builder().setUrl(url).addUploadFile("uploadFile", filePathTwo, new ProgressCallback() {

        @Override
        public void onProgressMain(int percent, long bytesWritten, long contentLength, boolean done) {
            uploadProgressTwo.setProgress(percent);
            LogUtil.d(TAG, "上传进度2:" + percent);
        }

        @Override
        public void onResponseMain(String filePath, HttpInfo info) {
            LogUtil.d(TAG, "上传结果2 Main:\n" + filePath + "\n" + info.getRetDetail());
        }

        @Override
        public void onResponseSync(String filePath, HttpInfo info) {
            LogUtil.d(TAG, "上传结果2 Sync:\n" + filePath + "\n" + info.getRetDetail());
        }
    }).build();
    doUploadImg(info);
}
Also used : HttpInfo(com.okhttplib.HttpInfo) ProgressCallback(com.okhttplib.callback.ProgressCallback)

Example 9 with ProgressCallback

use of com.okhttplib.callback.ProgressCallback in project OkHttp3 by MrZhousf.

the class UploadFileActivity method uploadFile.

private void uploadFile(String path) {
    if (TextUtils.isEmpty(path)) {
        Toast.makeText(this, "请选择上传文件!", Toast.LENGTH_LONG).show();
        return;
    }
    HttpInfo info = HttpInfo.Builder().setUrl(url).addUploadFile("uploadFile", path, new ProgressCallback() {

        @Override
        public void onProgressMain(int percent, long bytesWritten, long contentLength, boolean done) {
            uploadProgress.setProgress(percent);
            LogUtil.d(TAG, "上传进度:" + percent);
        }

        @Override
        public void onResponseMain(String filePath, HttpInfo info) {
            tvResult.setText(info.getRetDetail());
        }
    }).build();
    OkHttpUtil.getDefault(this).doUploadFileAsync(info);
}
Also used : HttpInfo(com.okhttplib.HttpInfo) ProgressCallback(com.okhttplib.callback.ProgressCallback)

Example 10 with ProgressCallback

use of com.okhttplib.callback.ProgressCallback in project OkHttp3 by MrZhousf.

the class UploadFileActivity method doUploadBatch.

/**
     * 单次批量上传:一次请求上传多个文件
     */
private void doUploadBatch() {
    imgList.clear();
    imgList.add("/storage/emulated/0/okHttp_download/test.apk");
    //        imgList.add("/storage/emulated/0/okHttp_download/test.rar");
    HttpInfo.Builder builder = HttpInfo.Builder().setUrl(url);
    //循环添加上传文件
    for (String path : imgList) {
        //若服务器为php,接口文件参数名称后面追加"[]"表示数组,示例:builder.addUploadFile("uploadFile[]",path);
        builder.addUploadFile("uploadFile", path);
    }
    final HttpInfo info = builder.build();
    new Thread(new Runnable() {

        @Override
        public void run() {
            OkHttpUtil.getDefault(UploadFileActivity.this).doUploadFileSync(info, new ProgressCallback() {

                @Override
                public void onProgressMain(int percent, long bytesWritten, long contentLength, boolean done) {
                    uploadProgress.setProgress(percent);
                    LogUtil.d(TAG, "上传进度:" + percent);
                }

                @Override
                public void onResponseMain(String filePath, HttpInfo info) {
                    LogUtil.d(TAG, "上传结果:" + info.getRetDetail());
                    tvResult.setText(info.getRetDetail());
                }
            });
        }
    }).start();
}
Also used : HttpInfo(com.okhttplib.HttpInfo) ProgressCallback(com.okhttplib.callback.ProgressCallback)

Aggregations

ProgressCallback (com.okhttplib.callback.ProgressCallback)10 HttpInfo (com.okhttplib.HttpInfo)9 DownloadFileInfo (com.okhttplib.bean.DownloadFileInfo)2 IOException (java.io.IOException)2 SocketException (java.net.SocketException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 Request (okhttp3.Request)2 Message (android.os.Message)1 ProgressMessage (com.okhttplib.bean.ProgressMessage)1 UploadFileInfo (com.okhttplib.bean.UploadFileInfo)1 ProgressRequestBody (com.okhttplib.progress.ProgressRequestBody)1 ProgressResponseBody (com.okhttplib.progress.ProgressResponseBody)1 File (java.io.File)1 RandomAccessFile (java.io.RandomAccessFile)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Interceptor (okhttp3.Interceptor)1 MultipartBody (okhttp3.MultipartBody)1 OkHttpClient (okhttp3.OkHttpClient)1 RequestBody (okhttp3.RequestBody)1 Response (okhttp3.Response)1