android http上传进度条,Android使用服务将文件上传到带有进度条的...

我已经使用asynctask使用进度条实现了上传文件,但是大文件或上传多个文件使我的活动挂起,我现在想做同样的事情,但要使用服务而不是使用asynctask.这是我的代码.

private class UploadFileToServer extends AsyncTask {

@Override

protected void onPreExecute() {

// setting progress bar to zero

mBuilder.setProgress(100, 0, false);

mNotifyManager.notify(0, mBuilder.build());

super.onPreExecute();

}

@Override

protected void onProgressUpdate(Integer... progress) {

mBuilder.setProgress(100,

Integer.parseInt(String.valueOf(progress[0])), false);

mNotifyManager.notify(0, mBuilder.build());

}

@Override

protected String doInBackground(Void... params) {

return uploadFile();

}

private String uploadFile() {

String responseString = null;

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost(***);

try {

AndroidMultiPartEntity entity = new AndroidMultiPartEntity(

new AndroidMultiPartEntity.ProgressListener() {

public void transferred(long num) {

publishProgress((int) ((num / (float) totalSize) * 100));

}

});

File sourceFile = new File(orignal1);

entity.addPart("image", new FileBody(sourceFile));

totalSize = entity.getContentLength();

httppost.setEntity(entity);

// Making server call

HttpResponse response = httpclient.execute(httppost);

HttpEntity r_entity = response.getEntity();

int statusCode = response.getStatusLine().getStatusCode();

if (statusCode == 200) {

// Server response

responseString = EntityUtils.toString(r_entity);

} else {

responseString = "Error occurred! Http Status Code: "

+ statusCode;

}

} catch (ClientProtocolException e) {

responseString = e.toString();

} catch (IOException e) {

responseString = e.toString();

}

return responseString;

}

@Override

protected void onPostExecute(String result) {

mBuilder.setContentText("Upload complete").setProgress(0, 0,false);

mNotifyManager.notify(6532365, mBuilder.build());

mNotifyManager.cancel(6532365);

super.onPostExecute(result);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值