Search in sources :

Example 1 with ProgressRequestBody

use of com.okhttplib.progress.ProgressRequestBody in project OkHttp3 by MrZhousf.

the class DownUpLoadHelper method uploadFile.

/**
     * 文件上传
     */
void uploadFile(OkHttpHelper helper) {
    try {
        final HttpInfo info = httpInfo;
        List<UploadFileInfo> uploadFileList = helper.getUploadFileInfoList();
        String url = info.getUrl();
        if (TextUtils.isEmpty(url)) {
            showLog("文件上传接口地址不能为空");
            return;
        }
        StringBuilder log = new StringBuilder("PostParams: ");
        MultipartBody.Builder mBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        ProgressCallback progressCallback = helper.getProgressCallback();
        String logInfo;
        if (null != info.getParams() && !info.getParams().isEmpty()) {
            for (String key : info.getParams().keySet()) {
                mBuilder.addFormDataPart(key, info.getParams().get(key));
                logInfo = key + " =" + info.getParams().get(key) + ", ";
                log.append(logInfo);
            }
        }
        for (UploadFileInfo fileInfo : uploadFileList) {
            if (progressCallback == null) {
                progressCallback = fileInfo.getProgressCallback();
            }
            String filePath = fileInfo.getFilePathWithName();
            String interfaceParamName = fileInfo.getInterfaceParamName();
            File file = new File(filePath);
            log.append(interfaceParamName);
            log.append("=");
            log.append(filePath);
            log.append(",");
            mBuilder.addFormDataPart(interfaceParamName, file.getName(), RequestBody.create(MediaTypeUtil.fetchFileMediaType(filePath), file));
        }
        showLog(log.toString());
        RequestBody requestBody = mBuilder.build();
        Request.Builder requestBuilder = new Request.Builder();
        requestBuilder.url(url).post(new ProgressRequestBody(requestBody, progressCallback, timeStamp, requestTag));
        helper.getHttpHelper().addHeadsToRequest(info, requestBuilder);
        Request request = requestBuilder.build();
        helper.setRequest(request);
        helper.getHttpHelper().responseCallback(helper.doRequestSync(), progressCallback, OkMainHandler.RESPONSE_UPLOAD_CALLBACK, requestTag);
    } catch (Exception e) {
        showLog("上传文件失败:" + e.getMessage());
    }
}
Also used : ProgressCallback(com.okhttplib.callback.ProgressCallback) Request(okhttp3.Request) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) HttpInfo(com.okhttplib.HttpInfo) UploadFileInfo(com.okhttplib.bean.UploadFileInfo) MultipartBody(okhttp3.MultipartBody) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ProgressRequestBody(com.okhttplib.progress.ProgressRequestBody) RequestBody(okhttp3.RequestBody) ProgressRequestBody(com.okhttplib.progress.ProgressRequestBody)

Example 2 with ProgressRequestBody

use of com.okhttplib.progress.ProgressRequestBody in project OkHttp3 by MrZhousf.

the class HttpHelper method buildRequest.

/**
     * 构建Request
     */
private Request buildRequest(HttpInfo info, @RequestMethod int method, ProgressCallback progressCallback) {
    Request request;
    Request.Builder requestBuilder = new Request.Builder();
    final String url = info.getUrl();
    if (method == RequestMethod.POST) {
        if (info.getParamBytes() != null) {
            RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), info.getParamBytes());
            requestBuilder.url(url).post(new ProgressRequestBody(fileBody, progressCallback, timeStamp, requestTag));
        } else if (info.getParamFile() != null) {
            RequestBody fileBody = RequestBody.create(MediaType.parse("text/x-markdown; charset=utf-8"), info.getParamFile());
            requestBuilder.url(url).post(new ProgressRequestBody(fileBody, progressCallback, timeStamp, requestTag));
        } else {
            requestBuilder.url(url).post(packageFormBody(info, url, requestBuilder).build());
        }
    } else if (method == RequestMethod.GET) {
        StringBuilder params = new StringBuilder();
        params.append(url);
        if (null != info.getParams() && !info.getParams().isEmpty()) {
            if (!url.contains("?") && !url.endsWith("?"))
                params.append("?");
            String logInfo;
            String value;
            boolean isFirst = params.toString().endsWith("?");
            for (String name : info.getParams().keySet()) {
                value = info.getParams().get(name);
                value = value == null ? "" : value;
                if (isFirst) {
                    logInfo = name + "=" + value;
                    isFirst = false;
                } else {
                    logInfo = "&" + name + "=" + value;
                }
                params.append(logInfo);
            }
        }
        requestBuilder.url(params.toString()).get();
    } else if (method == RequestMethod.PUT) {
        requestBuilder.url(url).put(packageFormBody(info, url, requestBuilder).build());
    } else if (method == RequestMethod.DELETE) {
        requestBuilder.url(url).delete(packageFormBody(info, url, requestBuilder).build());
    } else {
        requestBuilder.url(url).get();
    }
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR2) {
        requestBuilder.addHeader("Connection", "close");
    }
    addHeadsToRequest(info, requestBuilder);
    request = requestBuilder.build();
    return request;
}
Also used : Request(okhttp3.Request) ProgressRequestBody(com.okhttplib.progress.ProgressRequestBody) RequestBody(okhttp3.RequestBody) ProgressRequestBody(com.okhttplib.progress.ProgressRequestBody)

Aggregations

ProgressRequestBody (com.okhttplib.progress.ProgressRequestBody)2 Request (okhttp3.Request)2 RequestBody (okhttp3.RequestBody)2 HttpInfo (com.okhttplib.HttpInfo)1 UploadFileInfo (com.okhttplib.bean.UploadFileInfo)1 ProgressCallback (com.okhttplib.callback.ProgressCallback)1 File (java.io.File)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 MultipartBody (okhttp3.MultipartBody)1