如果nUploadSize是下面的长整数,
nUploadSize += currentline.getBytes().length;
有没有办法获得实际的字节大小而不是字节的长度?如果我试试这个:
nUploadSize += currentline.getBytes();
不接受说:
The operator += is undefined for the argument type(s) long, byte[]
我问这个问题的原因是因为我正在尝试使用显示百分比进展的对话框进行httppost上传。问题是进度条在上传成功的Toast显示之前从未显示任何进展
我有以下代码:
boolean m_bSuccess = true; // initiating a boolean to be used later
try
{
//upload parameters done through multipartEntity
MultipartEntity me = new MultipartEntity();
File pFp = new File(strAttachedFile);
me.addPart("videoFile", new FileBody(pFp, "video/3gpp"));
httppost.setEntity(me);
// get the total size of the file
nTotalSize = pFp.length();
//httpPst execution
HttpResponse responsePOST = client.execute(httppost);
HttpEntity resEntity = responsePOST.getEntity();
//
InputStream inputstream = resEntity.getContent();//generate stream from post resEntity
BufferedReader buffered = new BufferedReader(new InputStreamReader(inputstream));//buffer initiated to read inputstream
StringBuilder stringbuilder = new StringBuilder();
String currentline = null;
while ((currentline = buffered.readLine()) != null)
{
// adding the
stringbuilder.append(currentline + "\n");// appending every line of upload to stringbuilder
String result = stringbuilder.toString();
nUploadSize += currentline.getBytes().length;// += upload size acquires all current line bites
//convert upload size from integer to string to be read
String s = String.valueOf(nUploadSize);
Log.i("Upload lineeeeeeeeeeeeeeeeee",s);
Log.v("HTTP UPLOAD REQUEST",result);
inputstream.close();}
}
catch (Exception e) {
e.printStackTrace();
m_bSuccess = false; //mbsucess is false on catch failure
}
publishProgress();
if(m_bSuccess)
return "success";
return null;
}
protected void onProgressUpdate(Integer... progress) {
if(m_vwProgress !=null){
m_vwProgress.setProgress((int)(nUploadSize/nTotalSize));
}
}
protected void onPostExecute(String result){
super.onPostExecute(result);
if(result==null){
if(m_vwProgress !=null)
m_vwProgress.dismiss();
Toast.makeText(m_activity, "Upload Successful", Toast.LENGTH_LONG).show();
}
}
}
尝试上传时,在Log.i("Upload lineeeeeeeeeeeeeeeeee",s)
期间;它显示我从nUploadSize += currentline.getBytes().length;
得到的最高字节大小为200.解释我的主要问题为什么进度对话框从不显示百分比进展,因为数字太小。这就是为什么我试图看看我是否真的得到字符串字节大小。如果有人知道如何解决这个问题,我将不胜感激。
答案 0 :(得分:3)
currentLine.getBytes().length
是“字节大小”,即字节数......
答案 1 :(得分:1)
字节数为currentline.getBytes().length
,原始数据(字节)为currentline.getBytes()