android下载blob总是得到与原始不同的字节

时间:2014-02-16 15:12:28

标签: android

我有一个Android项目,它将从服务器下载blob数据并读取字节,blob数据来自我之前上传的原始文件。但是当我从android应用程序下载blob数据并读取字节时,它返回的字节大小不同于服务器中的字节大小。长度总是1340,有人可以告诉我有什么问题所以我总是得到这个长度吗?在android中下载blob数据有什么限制吗?或者如果您有任何下载blob数据的示例代码,请与我分享,以便我分析我的代码有什么问题。这是doInBackground代码。

@Override
protected byte[] doInBackground(String... params) {
// TODO Auto-generated method stub
byte[] result = null;
String str="";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);// in this case, params[0] is URL
try {
    // set up post data
    ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
    Iterator<String> it = mData.keySet().iterator();
    while (it.hasNext()) {
        String key = it.next();
        nameValuePair.add(new BasicNameValuePair(key, mData.get(key)));
    }

    post.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8"));
    HttpResponse response = client.execute(post);
    StatusLine statusLine = response.getStatusLine();
    if(statusLine.getStatusCode() == HttpURLConnection.HTTP_OK){
        result =EntityUtils.toByteArray(response.getEntity());
        str = new String(result, "UTF-8");

    }
}
catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}
catch (Exception e) {
}
return result;
}

请帮助我,我在这里度过了2天没有进展,非常感谢....

1 个答案:

答案 0 :(得分:0)

检查String API

The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.