java - 从字符串中获取字节

标签 java android string http-post

如果 nUploadSize 是下面的 Long 整数,

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 lineeeeeeeeeeeeeeeeeee",s) 期间;它显示了我从 nUploadSize += currentline.getBytes().length; 获得的最大字节大小是 200。解释我的主要问题为什么进度对话框从不显示百分比进度,因为数字太小。这就是为什么我想看看我是否真的得到了字符串字节大小。如果有人知道如何解决这个问题,我将不胜感激。

最佳答案

currentLine.getBytes().length “字节大小”,即字节数...

关于java - 从字符串中获取字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7986632/

相关文章:

javascript - 无法在 JavaScript 中连接字符串

java - 自定义对象中的方法应该具有破坏性吗?

java - 了解 R.java 和 Android 中的本地化

java - Spring Boot AuthenticationFilterConfiguration.onStartup 未调用

android - RXjava和Retrofit处理空数据响应

java - StartActivityForResult - 检查是否存在额外的 Intent 或使用自定义结果代码?

java - 如何在 Java 中拆分字符串?

javascript - 匹配可能包含标点符号的单词边界的正则表达式

java - 无法在java字符串中找到字符的索引

java - JList 填充了不显示任何内容的 vector