0

I am uploading a video to youtube successfully with multipart form data retrofit 2 .

I want to show progress while uploading. How can I do this ?

Any advice or sample code please ? I searched but couldn't found any solution about this.

when user click the upload button, get token func is calling and video is starting to upload.

here is my upload 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());
        }
    });
}
podgradle
  • 336
  • 1
  • 10
  • 17
  • 3
    Possible duplicate of [Is it possible to show progress bar when upload image via Retrofit 2](https://stackoverflow.com/questions/33338181/is-it-possible-to-show-progress-bar-when-upload-image-via-retrofit-2) – AskNilesh Mar 05 '18 at 11:31
  • Good example here : https://futurestud.io/tutorials/retrofit-2-download-files-with-progress-updates – Gowthaman M Mar 05 '18 at 11:50
  • I think your question was answered in this link https://stackoverflow.com/questions/33338181/is-it-possible-to-show-progress-bar-when-upload-image-via-retrofit-2 – Amr Sakr Jun 10 '19 at 15:14

0 Answers0