【问题标题】:RESTlet ClientResource with progress bar带有进度条的 RESTlet ClientResource
【发布时间】:2015-04-27 16:33:04
【问题描述】:

我正在创建一个将文件上传到 RESTlet 服务器的 android 应用程序。我正在使用下面的代码。

Representation file = new FileRepresentation(filePath, MediaType.IMAGE_ALL);
FormDataSet form = new FormDataSet();
form.setMultipart(true);
form.getEntries().add(new FormData("file", file));
ClientResource cr = new ClientResource(url);
cr.post(form);

问题是:如何监控上传过程?

【问题讨论】:

    标签: android file-upload restlet android-progressbar clientresource


    【解决方案1】:

    使用异步任务来完成。

    这里是一个简单的例子:

    private class UploadFilesTask extends AsyncTask<URL, Integer, Long> {
        protected Long doInBackground(URL... urls) {
            // Your upload code with rest client
         }
    
         protected void onProgressUpdate(Integer... progress) {
             setProgressPercent(progress[0]);
         }
    
         protected void onPostExecute(Long result) {
             showDialog("Downloaded " + result + " bytes");
         }
     }
    

    你可以看到一个更一致和完整的例子here

    【讨论】:

    • 也许我不清楚。我必须将进度参数传递给 publishProgress() 方法,但是如何从 ClientResource 对象中获取此变量?
    • 你不能。休息是单向交流。你只能在决赛中看到结果。您可以使用没有百分比的进度对话框。
    猜你喜欢
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多