在我的应用程序中,我编写了将文件上传到dropbox的代码。我成功地能够做到这一点。我想在上传文件时显示Progressbar。我在我的Asynctask类中实现了onProgressUpdate方法。但我是没有得到如何计算我将在doInBackground()中的publishProgress方法传递的值。或者如何获取我将在发布进度方法中传递的值。我使用下面的代码,它是我的doInBackGround方法中DropBox API的一部分。
File file = new File("working-draft.txt");
FileInputStream inputStream = new FileInputStream(file);
Entry response = mDBApi.putFile("/magnum-opus.txt", inputStream,
file.length(), null, null);
Log.i("DbExampleLog", "The uploaded file's rev is: " + response.rev);
请提供您对上述查询的建议。谢谢:)
答案 0 :(得分:1)
为时已晚,但肯定会帮助其他人 现在,Dropbox API v2 Java SDK提供了用于上载和下载的进度侦听器。该版本已在v3.0.9中发布:
您可以通过以下方式使用ProgressListner
:
dbxClient.files().uploadBuilder("/" + file.getName()) //Path in the user's Dropbox to save the file.
.withMode(WriteMode.ADD)
.uploadAndFinish(inputStream, new IOUtil.ProgressListener() {
@Override
public void onProgress(long bytesWritten) {
percentWritten =(bytesWritten/uploadFile.length())*100; //Progress Percentage
}
});
干杯。快乐编码
答案 1 :(得分:0)
经过一些R& D,我终于得到了here.
的解决方案我没有将null传递给putFile方法的最后一个参数,而是实现了Progress Listener并获得了通过onProgress方法传输的字节数值。
Thanx建议您的答案。 :)
答案 2 :(得分:-1)
public class uploadfile extends AsyncTask<Void, Integer, String>
{
private ProgressDialog dialog;
protected Context mContext;
long totalSize;
public uploadfile(Context context) {
mContext = context;
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(mContext);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setTitle("Chance");
dialog.setCancelable(false);
//dialog.setIndeterminate(true);
dialog.setMessage("uploading...");
dialog.show();
//dialog.setProgress(0);
}
@Override
protected String doInBackground(Void... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpPost httppost = new HttpPost("your url");
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy( policy);
String responseMessage = "";
try {
CustomMultiPartEntity entity = new CustomMultiPartEntity(new ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
//post request to send the file
FileBody file1 = new FileBody(new File("path of file"));
entity.addPart("file", file1);
totalSize = entity.getContentLength();
httppost.setEntity(entity);
// DEBUG
System.out.println( "executing request " + httppost.getRequestLine( ) );
HttpResponse response = httpclient.execute(httppost, httpContext);
responseMessage = EntityUtils.toString(response.getEntity());
System.out.println( "Server response" + responseMessage );
}
catch (SocketTimeoutException e) {
e.printStackTrace();
responseMessage = "unreachable";
}
catch (ConnectTimeoutException e) {
e.printStackTrace();
responseMessage = "unreachable";
}
catch (Exception e) {
e.printStackTrace();
responseMessage = "unreachable";
}
return responseMessage;
}
@Override
protected void onProgressUpdate(Integer... progress) {
dialog.setProgress((int) (progress[0]));
}
protected void onPostExecute(String result) {
//result is the response back from "doInBackground"
dialog.cancel();
//check if there were errors upoloading
if (result.equalsIgnoreCase("unreachable")) {
Toast.makeText(mContext, "Error while uploading...Please check your internet connection and retry", Toast.LENGTH_LONG).show();
return;
}
if(result.equals("[\"uploaded\"]"))
{
Toast.makeText(mContext, "uploaded", Toast.LENGTH_LONG).show();
finish();
}
}
}
CustomMultiPartEntity.java
public class CustomMultiPartEntity extends MultipartEntity
{
private final ProgressListener listener;
public CustomMultiPartEntity(final ProgressListener listener)
{
super();
this.listener = listener;
}
public CustomMultiPartEntity(final HttpMultipartMode mode, final ProgressListener listener)
{
super(mode);
this.listener = listener;
}
public CustomMultiPartEntity(HttpMultipartMode mode, final String boundary, final Charset charset, final ProgressListener listener)
{
super(mode, boundary, charset);
this.listener = listener;
}
@Override
public void writeTo(final OutputStream outstream) throws IOException
{
super.writeTo(new CountingOutputStream(outstream, this.listener));
}
public static interface ProgressListener
{
void transferred(long num);
}
public static class CountingOutputStream extends FilterOutputStream
{
private final ProgressListener listener;
private long transferred;
public CountingOutputStream(final OutputStream out, final ProgressListener listener)
{
super(out);
this.listener = listener;
this.transferred = 0;
}
public void write(byte[] b, int off, int len) throws IOException
{
out.write(b, off, len);
this.transferred += len;
this.listener.transferred(this.transferred);
}
public void write(int b) throws IOException
{
out.write(b);
this.transferred++;
this.listener.transferred(this.transferred);
}
}
}