我目前正在使用Retrofit 2,我想在我的服务器上传一些照片.
我知道,旧版本使用Typedfile类进行上传.如果我们想要使用进度条,我们应该覆盖Typedfile类中的writeto方法.
使用改造2库时是否可以显示进度?
解决方法:
首先,您应该使用等于或高于2.0 beta2的Retrofit 2版本.
二,创建新类扩展Requestbody:
public class ProgressRequestbody extends Requestbody { private file mfile; private String mPath; private UploadCallbacks mListener; private String content_type; private static final int DEFAulT_BUFFER_SIZE = 2048; public interface UploadCallbacks { voID onProgressUpdate(int percentage); voID one rror(); voID onFinish(); }
Take note, I added content type so it can accommodate other types asIDe image
public ProgressRequestbody(final file file, String content_type, final UploadCallbacks Listener) { this.content_type = content_type; mfile = file; mListener = Listener; }@OverrIDe public MediaType ContentType() { return MediaType.parse(content_type+"/*"); }@OverrIDepublic long contentLength() throws IOException { return mfile.length();}@OverrIDepublic 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") Call<JsonObject> uploadImage(@Part Multipartbody.Part file);
/* JsonObject above can be replace with you own model, just want to
make this notable. */Now you can get progress of your upload.
In youractivity
(orfragment
):
class MyActivity extends AppCompatActivity implements ProgressRequestbody.UploadCallbacks { Progressbar progressbar; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); progressbar = findVIEwByID(R.ID.progressbar); ProgressRequestbody fileBody = new ProgressRequestbody(file, this); Multipartbody.Part filePart = Multipartbody.Part.createFormData("image", file.getname(), fileBody);Call<JsonObject> request = RetrofitClIEnt.uploadImage(filepart); request.enqueue(new Callback<JsonObject>() { @OverrIDe public voID onResponse(Call<JsonObject> call, Response<JsonObject> response) { if(response.isSuccessful()){ /* here we can equally assume the file has been downloaded successfully because for some reasons the onFinish method might not be called, I have tested it myself and it really not consistent, but the onProgressUpdate is efficIEnt and we can use that to update out progress on the UIThread, and we can then set our progress to 100% right here because the file already downloaded finish. */ } } @OverrIDe public voID onFailure(Call<JsonObject> call, Throwable t) { /* we can also stop our progress update here, although I have not check if the one rror is being called when the file Could not be downloaded, so I will just use this as a backup plan just incase the one rror dID not get called. So I can stop the progress right here. */ } }); } @OverrIDe public voID onProgressUpdate(int percentage) { // set current progress progressbar.setProgress(percentage); } @OverrIDe public voID one rror() { // do something on error } @OverrIDe public voID onFinish() { // do something on upload finished // for example start next uploading at queue progressbar.setProgress(100); } }
总结 以上是内存溢出为你收集整理的android – 通过Retrofit 2上传图片时是否可以显示进度条?全部内容,希望文章能够帮你解决android – 通过Retrofit 2上传图片时是否可以显示进度条?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)