上传时如何通过改造2显示进度?

时间:2018-03-05 11:31:28

标签: android progress-bar retrofit2

我正在使用多部分表单数据改造2成功将视频上传到youtube。

我想在上传时显示进度。我怎么能这样做?

请提供任何建议或示例代码?我搜索过但无法找到解决方法。

当用户点击上传按钮时,get token func正在调用,视频开始上传。

这是我的上传功能。

public void getToken(final String title) {
    progessBlackView.setVisibility(View.VISIBLE);
    showProgressDialog("Loading");
    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
    Call<Token> call = apiService.GETTOKEN(tinyDb.getString(Constant.wkey));
    call.enqueue(new Callback<Token>() {
        @Override
        public void onResponse(Call<Token> call, Response<Token> response) {
            hideProgressDialog();
            if (response != null && response.body() != null) {
                if (response.body().getEx() == null) {
                    String token = response.body().getToken();
                    YouTubeAPIClient.YouTubeAPIService service = YouTubeAPIClient.getClient(token).create(YouTubeAPIClient.YouTubeAPIService.class);
                    JsonObject snippetJson = new JsonObject();
                    snippetJson.addProperty("title", title);
                    snippetJson.addProperty("description", "");
                    JsonObject statusJson = new JsonObject();
                    statusJson.addProperty("privacyStatus", "unlisted");
                    MediaType jsonM = MediaType.parse("application/json");
                    JsonObject partJson = new JsonObject();
                    partJson.add("snippet", snippetJson);
                    partJson.add("status", statusJson);
                    RequestBody part = RequestBody.create(jsonM, partJson.toString());
                    MultipartBody.Part video = prepareFilePart("video", videoFileName);
                    progessBlackView.setVisibility(View.VISIBLE);
                    Call<JsonObject> call2 = service.upload(part, video);
                    call2.enqueue(new Callback<JsonObject>() {
                        @Override
                        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                            if(response.code() == 200){
                                Log.d(TAG, "onResponse: " + response.body());
                                JsonElement tmp = response.body().get("id");
                                if(tmp  != null) {
                                    String vkey = tmp.toString();
                                    vkey = vkey.replace("\"","");
                                    String item = "";
                                    for (int i = 0; i < mtagIds.size(); i++) {
                                        item = item + mtagIds.get(i);
                                        if (i != mtagIds.size() - 1) {
                                            item = item + ", ";
                                        }
                                    }
                                    String videoKey = vkey.toString();
                                    JoinAllAllowedInno selectedJoin = DataHolder.getInstance().getSelectedJoin();
                                    addVideo(selectedJoin.getInnoId(), videoKey, title, item);
                                }
                                else {
                                    getAlertError((String) getText(R.string.videoNotFound));
                                }
                                progessBlackView.setVisibility(View.GONE);
                            }
                        }

                        @Override
                        public void onFailure(Call<JsonObject> call, Throwable t) {
                            Log.i("YoutubeUpload", t.getMessage());
                            getAlertError((String) getText(R.string.videoNotFound));
                            progessBlackView.setVisibility(View.GONE);
                        }
                    });

                } else {
                    getAlertError(response.body().getEx());
                }
            }
        }

        @Override
        public void onFailure(Call<Token> call, Throwable t) {
            hideProgressDialog();
            Log.e(CLASS_NAME, t.toString());
        }
    });
}

0 个答案:

没有答案