計算的下載速度,舉例說明一下吧。
在成都網(wǎng)站制作、成都做網(wǎng)站過程中,需要針對客戶的行業(yè)特點、產(chǎn)品特性、目標(biāo)受眾和市場情況進(jìn)行定位分析,以確定網(wǎng)站的風(fēng)格、色彩、版式、交互等方面的設(shè)計方向。創(chuàng)新互聯(lián)建站還需要根據(jù)客戶的需求進(jìn)行功能模塊的開發(fā)和設(shè)計,包括內(nèi)容管理、前臺展示、用戶權(quán)限管理、數(shù)據(jù)統(tǒng)計和安全保護(hù)等功能。
例如每隔N秒就改變當(dāng)前下載速度數(shù)字的,N秒前的下載進(jìn)度百分比m%,
N秒后的下載進(jìn)度百分比n%,當(dāng)前下載問題的總大小Size【換算成多少kb】
(m%-n%)*Size/N就是
這段時間的下載或上傳速度了。
注意:記得增加判斷一些邊界問題。
實現(xiàn)在android實現(xiàn)帶進(jìn)度條的上傳效果效果如圖:用到以下兩個類就可實現(xiàn)帶進(jìn)度條的文件上傳:1、CustomMultiPartEntity extends MultipartEntity, 2、HttpMultipartPost extends AsyncTask代碼如下:import java/UploadImage.php");try{CustomMultipartEntitymultipartContent = newCustomMultipartEntity(newProgressListener() {@Overridepublic void transferred(longnum){publishProgress((int) ((num / (float) totalSize) * 100));}});// We use FileBody to transfer an imagemultipartContent.addPart("uploaded_file", newFileBody(newFile(m_userSelectedImagePath)));totalSize= multipartContent.getContentLength();// Send ithttpPost.setEntity(multipartContent);HttpResponseresponse = httpClient.execute(httpPost, httpContext);String serverResponse = EntityUtils.toString(response.getEntity());ResponseFactoryrp = newResponseFactory(serverResponse);return(TypeImage) rp.getData();}catch(Exception e) {System.out.println(e);}returnnull;}@OverrideprotectedvoidonProgressUpdate(Integer... progress){pd.setProgress((int) (progress[0]));}@OverrideprotectedvoidonPostExecute(TypeUploadImageui) {pd.dismiss();}} 在 transferred()函數(shù)中調(diào)用publishProgress((int) ((num / (float) totalSize) * 100));在onProgressUpdate()實現(xiàn)上傳進(jìn)度的更新操作
Android文件上傳可能是一個比較耗時的操作,如果為上傳操作帶上進(jìn)度提示則可以更好的提高用戶體驗,最后效果如下圖:
可使用android自帶的httpclient框架實現(xiàn),附件中已經(jīng)現(xiàn)成的示例代碼,帶上傳進(jìn)度。
1. GET 方式傳遞參數(shù)
//先將參數(shù)放入List,再對參數(shù)進(jìn)行URL編碼
ListBasicNameValuePair params = new LinkedListBasicNameValuePair();
params.add(new BasicNameValuePair("param1", "數(shù)據(jù)"));? ? //增加參數(shù)1
params.add(new BasicNameValuePair("param2", "value2"));//增加參數(shù)2
String param = URLEncodedUtils.format(params, "UTF-8");//對參數(shù)編碼
String baseUrl = "服務(wù)器接口完整URL";
HttpGet getMethod = new HttpGet(baseUrl + "?" + param);//將URL與參數(shù)拼接
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(getMethod); //發(fā)起GET請求
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應(yīng)碼
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//獲取服務(wù)器響應(yīng)內(nèi)容
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
2.?POST方式 方式傳遞參數(shù)
//和GET方式一樣,先將參數(shù)放入List
params = new LinkedListBasicNameValuePair();
params.add(new BasicNameValuePair("param1", "Post方法"));//增加參數(shù)1
params.add(new BasicNameValuePair("param2", "第二個參數(shù)"));//增加參數(shù)2
try {
HttpPost postMethod = new HttpPost(baseUrl);//創(chuàng)建一個post請求
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //將參數(shù)填入POST Entity中
HttpResponse response = httpClient.execute(postMethod); //執(zhí)行POST方法
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應(yīng)碼
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //獲取響應(yīng)內(nèi)容
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
計算的下載速度,舉例說明一下吧。
例如每隔N秒就改變當(dāng)前下載速度數(shù)字的,N秒前的下載進(jìn)度百分比m%,
N秒后的下載進(jìn)度百分比n%,當(dāng)前下載問題的總大小Size【換算成多少kb】
(m%-n%)*Size/N就是 這段時間的下載或上傳速度了。
注意:記得增加判斷一些邊界問題。
網(wǎng)站標(biāo)題:android上傳進(jìn)度,android下載進(jìn)度
分享路徑:http://jinyejixie.com/article16/dsdijdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、網(wǎng)站導(dǎo)航、用戶體驗、移動網(wǎng)站建設(shè)、服務(wù)器托管、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)