通过改造2是否可以显示进度条,当上传图片

问题描述:

我'目前使用改造2 ,我想上传一些照片在我的服务器。 我知道,这较旧的版本使用 TypedFile 类上传。如果我们想用进度条,有了它我们应该在 TypedFile 类中重写的writeTo 方法。

I'am currently using Retrofit 2 and i want to upload some photo at my server. I know, that older version uses TypedFile class for uploading. And if we want to use progress bar with it we should override writeTo method in TypedFile class.

是否有可能使用时显示进度加装2 库?

Is it possible to show progress when using retrofit 2 library?

首先,你应该使用改造2版本等于或高于2.0的Beta2。 二,创建新类扩展 RequestBody

First of all, you should use Retrofit 2 version equal or above 2.0 beta2. Second, create new class extends RequestBody:

    public class ProgressRequestBody extends RequestBody {
    private File mFile;
    private String mPath;
    private UploadCallbacks mListener;

    private static final int DEFAULT_BUFFER_SIZE = 2048;

    public interface UploadCallbacks {
        void onProgressUpdate(int percentage);
        void onError();
        void onFinish();
    }

    public ProgressRequestBody(final File file, final  UploadCallbacks listener) {
        mFile = file;
        mListener = listener;            
    }

    @Override
    public MediaType contentType() {
        // i want to upload only images
        return MediaType.parse("image/*");
    }

    @Override
    public void writeTo(BufferedSink sink) throws IOException {
        long fileLength = mFile.length();
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        FileInputStream in = new FileInputStream(mFile);
        long uploaded = 0;

        try {
            int read;
            Handler handler = new Handler(Looper.getMainLooper());
            while ((read = in.read(buffer)) != -1) {

                // update progress on UI thread
                handler.post(new ProgressUpdater(uploaded, fileLength));

                uploaded += read;
                sink.write(buffer, 0, read);
            }
        } finally {
            in.close();
        }
    }

    private class ProgressUpdater implements Runnable {
        private long mUploaded;
        private long mTotal;
        public ProgressUpdater(long uploaded, long total) {
            mUploaded = uploaded;
            mTotal = total;
        }

        @Override
        public void run() {
            mListener.onProgressUpdate((int)(100 * mUploaded / mTotal));            
        }
    }
}

三,创建界面

Third, create interface

@Multipart
    @POST("/upload")        
    // without filename field it doesn't work :(
    // upload is a POST field
    Call<JsonObject> uploadImage(@Part("upload\"; filename=\"1\" ") ProgressRequestBody file);

现在你可以得到你的上传进度。

Now you can get progress of your upload.

在你的活动(或片段):

class MyActivity extends AppCompatActivity implements ProgressRequestBody.UploadCallbacks {
        ProgressBar progressBar;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            progressBar = findViewById(R.id.progressBar);
        }

        @Override
        public void onProgressUpdate(int percentage) {
            // set current progress
            progressBar.setProgress(percentage);
        }

        @Override
        public void onError() {
            // do something on error
        }

        @Override
        public void onFinish() {
            // do something on upload finished
            // for example start next uploading at queue
            progressBar.setProgress(100);
        }


    }