上传OkHttpClient的进度条

时间:2018-04-12 11:23:14

标签: android file-upload progressdialog

我使用以下方法上传文件,但我无法显示增加上传过程的方法。 请指导我如何显示此代码[0 - %100]的进度条。我搜索了很多但失败了,我累了。 这是我的上传代码:

    private void upload_btn() {
        button_upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                progress = new ProgressDialog(UploaderActictivity.this);
                progress.setTitle("آپلود مقاله");
                progress.setMessage("لطفاً منتظر بمانید...");
                progress.setCancelable(false);
                progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progress.setProgress(0);
                progress.setMax(100);
                progress.show();
                final int[] mProgressStatus = {0};

                Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        File f = new File(messageText.getText().toString());
                        if (f.isFile()) {
                            try {
                                UploadFile(f);

                            }catch (Exception e){
                                progress.dismiss();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        imNotify.setVisibility(View.VISIBLE);
                                        messageText.setVisibility(View.VISIBLE);



                                    }
                                });
                            }

                        }else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    progress.dismiss();
                                    imNotify.setVisibility(View.VISIBLE);
                                    messageText.setVisibility(View.VISIBLE);
                                    messageText.setText(getResources().getString(uploadAgain));

                                }
                            });
                        }

                    }
                });

                t.start();


            }
        });
    }

    private String getMimeType(String path) {

        String extension = MimeTypeMap.getFileExtensionFromUrl(path);

        return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }

    public void UploadFile(final File f){
        String content_type = getMimeType(f.getPath());
        Log.e("ctt",content_type);
        String file_path = f.getAbsolutePath();
        Log.d("ctt2",file_path);

        SharedPreferences prefs =
                PreferenceManager.getDefaultSharedPreferences(UploaderActictivity.this);
        final int UserID = prefs.getInt("UserID", 0);

        OkHttpClient client = new OkHttpClient();
        RequestBody file_body = RequestBody.create(MediaType.parse(content_type), f);
        RequestBody request_body = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("type", content_type)
                .addFormDataPart("uploaded_file",
                        UserID + "_" +
                        f.getName()
                        , file_body)
                .build();

        Request request = new Request.Builder()
                .url(AppConfig.upLoadServerUri + "/upload")
                .post(request_body)
                .build();

        try {
            Response response = client.newCall(request).execute();

            if (!response.isSuccessful()) {
                throw new IOException("Error : " + response);
            }
            progress.dismiss();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    imNotify.setVisibility(View.VISIBLE);
                    messageText.setVisibility(View.VISIBLE);
                    messageText.setText(getResources().getString(DescriptionForUpload));
                    button_upload.setEnabled(false);
                }
            });

        } catch (IOException e) {
            progress.dismiss();
            e.printStackTrace();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    imNotify.setVisibility(View.VISIBLE);
                    messageText.setVisibility(View.VISIBLE);
                   messageText.setText(getResources().getString(uploadAgain));
                    button_upload.setEnabled(false);

                }
            });
        }
    }

如果我可以在此代码中解释,我可以做哪些更改?谢谢你

0 个答案:

没有答案
相关问题