Android Open Source - qiniu-android Form Uploader






From Project

Back to project page qiniu-android.

License

The source code is released under:

MIT License

If you think the Android project qiniu-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.qiniu.android.storage;
/* w w w  . j  a v a 2 s.com*/
import com.qiniu.android.common.Config;
import com.qiniu.android.http.CompletionHandler;
import com.qiniu.android.http.HttpManager;
import com.qiniu.android.http.PostArgs;
import com.qiniu.android.http.ProgressHandler;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.utils.Crc32;

import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

final class FormUploader {
    static void upload(HttpManager httpManager, byte[] data, String k, String token, final UpCompletionHandler completionHandler,
                       final UploadOptions options) {
        post(data, null, k, token, completionHandler, options, httpManager);
    }

    static void upload(HttpManager httpManager, File file, String key, String token, UpCompletionHandler completionHandler,
                       UploadOptions options) {
        post(null, file, key, token, completionHandler, options, httpManager);
    }

    private static void post(byte[] data, File file, String k, String token, final UpCompletionHandler completionHandler,
                             final UploadOptions options, final HttpManager httpManager) {
        final String key = k;
        Map<String, String> params = new HashMap<String, String>();
        final PostArgs args = new PostArgs();
        if (k != null) {
            params.put("key", key);
            args.fileName = key;
        } else {
            args.fileName = "?";
        }

        params.put("token", token);
        if (options != null) {
            params.putAll(options.params);
        }

        String mimeType = "application/octet-stream";
        if (options != null && options.mimeType != null && options.mimeType.equals("")) {
            mimeType = options.mimeType;
        }

        if (options != null && options.checkCrc) {
            long crc = 0;
            if (file != null) {
                try {
                    crc = Crc32.file(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                crc = Crc32.bytes(data);
            }
            params.put("crc32", "" + crc);
        }


        args.data = data;
        args.file = file;
        args.mimeType = mimeType;
        args.params = params;

        ProgressHandler progress = null;
        if (options != null && options.progressHandler != null) {
            progress = new ProgressHandler() {
                @Override
                public void onProgress(int bytesWritten, int totalSize) {
                    double percent = (double) bytesWritten / (double) totalSize;
                    if (percent > 0.95) {
                        percent = 0.95;
                    }
                    options.progressHandler.progress(key, percent);
                }
            };
        }

        final ProgressHandler progress2 = progress;

        CompletionHandler completion = new CompletionHandler() {
            @Override
            public void complete(ResponseInfo info, JSONObject response) {
                if (info.isOK()) {
                    if (progress2 != null) {
                        options.progressHandler.progress(key, 1.0);
                    }
                    completionHandler.complete(key, info, response);
                    return;
                }
                CompletionHandler retried = new CompletionHandler() {
                    @Override
                    public void complete(ResponseInfo info, JSONObject response) {
                        if (info.isOK() && progress2 != null) {
                            options.progressHandler.progress(key, 1.0);
                        }
                        completionHandler.complete(key, info, response);
                    }
                };
                String host = Config.UP_HOST;
                if (info.isNetworkBroken()) {
                    host = Config.UP_HOST_BACKUP;
                }
                httpManager.multipartPost("http://" + host, args, progress2, retried);
            }
        };

        httpManager.multipartPost("http://" + Config.UP_HOST, args, progress, completion);
    }
}




Java Source Code List

com.qiniu.android.Base64Test.java
com.qiniu.android.CrcTest.java
com.qiniu.android.EtagTest.java
com.qiniu.android.FormUploadTest.java
com.qiniu.android.HttpTest.java
com.qiniu.android.ResumeUploadTest.java
com.qiniu.android.TempFile.java
com.qiniu.android.TestConfig.java
com.qiniu.android.TestFileRecorder.java
com.qiniu.android.common.Config.java
com.qiniu.android.http.ByteArrayEntity.java
com.qiniu.android.http.CompletionHandler.java
com.qiniu.android.http.HttpManager.java
com.qiniu.android.http.PostArgs.java
com.qiniu.android.http.ProgressHandler.java
com.qiniu.android.http.ResponseHandler.java
com.qiniu.android.http.ResponseInfo.java
com.qiniu.android.storage.FormUploader.java
com.qiniu.android.storage.KeyGenerator.java
com.qiniu.android.storage.Recorder.java
com.qiniu.android.storage.ResumeUploader.java
com.qiniu.android.storage.UpCancellationSignal.java
com.qiniu.android.storage.UpCompletionHandler.java
com.qiniu.android.storage.UpProgressHandler.java
com.qiniu.android.storage.UploadManager.java
com.qiniu.android.storage.UploadOptions.java
com.qiniu.android.storage.persistent.FileRecorder.java
com.qiniu.android.utils.AsyncRun.java
com.qiniu.android.utils.Crc32.java
com.qiniu.android.utils.Etag.java
com.qiniu.android.utils.StringUtils.java
com.qiniu.android.utils.UriToFile.java
com.qiniu.android.utils.UrlSafeBase64.java