【问题标题】:Android Upload file Percentage ProgressBar with OKHTTP3Android 使用 OKHTTP3 上传文件百分比进度条
【发布时间】:2017-06-14 21:06:00
【问题描述】:

有谁知道我如何添加一个进度条,告诉我已上传了多少文件。这就是我在下面的代码中设法做到的,我设法从我的手机中挑选了一个文件并设法发送它,但问题是如果我要上传一个巨大的文件,我一直在等待,不知道什么时候会完成,这就是为什么我需要一个进度条来显示已传输到服务器的数量。这是我尝试过不同实现但无济于事的代码。

public class MainActivity extends AppCompatActivity {
private Button fileUploadBtn;
protected static String IPADDRESS = "http://10.42.0.1/hugefile/save_file.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    fileUploadBtn = (Button) findViewById(R.id.btnFileUpload);
    pickFile();

}

private void pickFile() {

    fileUploadBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new MaterialFilePicker()
                    .withActivity(MainActivity.this)
                    .withRequestCode(10).start();
        }
    });
}

ProgressDialog progress;

@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
    progress = new ProgressDialog(MainActivity.this);
    progress.setTitle("Uploading File(s)");
    progress.setMessage("Please wait...");
    progress.show();
    if (requestCode == 10 && resultCode == RESULT_OK) {

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {

                File f = new File(data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH));

                String content_type = getMimeType(f.getPath());
                String file_path = f.getAbsolutePath();

                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", file_path.substring(file_path.lastIndexOf("/") + 1), file_body).build();

                Request request = new Request.Builder()
                        .url(IPADDRESS)
                        .post(request_body)
                        .build();
                try {

                    Response response = client.newCall(request).execute();
                    Log.d("Server response", response.body().string());
                    if (!response.isSuccessful()) {
                        throw new IOException("Error : " + response);
                    }
                    progress.dismiss();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
        });
        t.start();

    }
}

private String getMimeType(String path) {
    String extension = MimeTypeMap.getFileExtensionFromUrl(path);
    return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);

}

}

【问题讨论】:

    标签: java android okhttp3


    【解决方案1】:

    我认为您的整个解决方案需要一些更正。通过一些对话框暂停用户是不好的做法,如果配置更改,您的上传进度也可能会丢失。而新的线程我现在真的太粗鲁了。所以首先我建议你将上传过程的代码移动到Service或IntentService中。您可以在通知中显示进度和状态,然后通过对话框或小吃栏等提醒用户。其次,没有直接的方法来监控上传进度。通常最好的方法是实现自定义 RequestBody ,它将通知通过监听器上传的字节。请参考Tracking progress of multipart file upload using OKHTTP 然后您可以使用广播或事件总线来发布进度。

    【讨论】:

      【解决方案2】:

      我设法通过一些改进来解决它,这里是遇到相同问题的任何人的解决方案的链接 https://github.com/MakuSimz/Android-Multipart-Upload-with-Progress

      【讨论】:

        猜你喜欢
        • 2014-03-05
        • 1970-01-01
        • 2019-12-04
        • 1970-01-01
        • 1970-01-01
        • 2018-12-11
        • 2016-05-04
        • 1970-01-01
        相关资源
        最近更新 更多