【问题标题】:Android Base64 decoding for Android 2.1适用于 Android 2.1 的 Android Base64 解码
【发布时间】:2011-09-07 12:29:52
【问题描述】:

我正在使用 API 级别 7 (Android 2.1) 构建应用程序,我需要对服务器接收到的字符串进行解码,该字符串使用 Base64 编码。我知道 Base64 库包含在 Android 2.2 中,这就是为什么我将它的源代码直接包含在我的项目中。你可以在这里找到源代码:Base64

所以我的问题是,当我尝试解码字符串时,base64 会返回:BASE 64 : [B@46392d10

如果我在在线 base64 解码器上运行服务器发送给我的代码,我会得到:le":"en_US","id":2,"error_code":6003,"error_string":"Unsupported Client Version"}

这是我正在使用的代码:

HttpResponse response = httpclient.execute(httppost);
Log.v("Response ","Status line : "+ response.getStatusLine().toString());
String responseBody = EntityUtils.toString(response.getEntity()); //response
Log.v("Response ","Response : "+ responseBody);

String objectIdentificator = responseBody.substring(0,32);
Log.v("Response ","Object Identificator (LONGINT) : "+ objectIdentificator);

String type = responseBody.substring(33,36);
Log.v("Response ","TYPE (UNSIGNED BYTE) : "+ type);

String operation = responseBody.substring(37,40);
Log.v("Response ","OPERATION (UNSIGNED BYTE) : "+ operation);

String objectId = responseBody.substring(41, 73);
Log.v("Response ","UID (CHAR, length 32) : "+ objectId);

String id = responseBody.substring(74, 106);
Log.v("Response ","ID (LONGINT) : "+ id);

String size = responseBody.substring(107,139);
Log.v("Response ","Data Size (LONGINT) : "+ size);

String hash = responseBody.substring(140,172);
Log.v("Response ","Data Hash (CHAR, length 32 : "+ hash);

String dType = responseBody.substring(173,176);
Log.v("Response ","Data Type (UNSIGNED BYTE) : "+ dType);

String data = responseBody.substring(177, responseBody.length());
Log.v("Response ","Data (CHAR, any length, in BASE64) : "+ data);
String first = Base64.decode(data, Base64.DEFAULT).toString();
Log.v("Response ","BASE 64 : "+ first);

有什么建议可以解决这个问题吗?

【问题讨论】:

  • 以上链接失效。它似乎被移动了to here
  • 感谢您的评论!我希望它对其他人有帮助:)
  • 小心,我在移动应用程序中使用Base64的代码对图像进行编码,在服务器中解码图像时遇到了一些问题,我不得不移动到Android 2.2

标签: java android base64


【解决方案1】:

你的问题与Base64无关。

Base64 类正确返回包含数据的byte[]。你在那个byte[] 上调用toString(),它使用byte[]内容,但使用它的identityHashCode()

想要做的是通过提供正确的编码将您的byte[] 转换为String

byte[] bytes = Base64.decode(data, Base64.DEFAULT);
String string = new String(bytes, "UTF-8");

【讨论】:

    【解决方案2】:

    你读过如何使用它吗?它在您链接的页面的顶部,您不会错过它 :) decode() 返回一个字节数组,您需要将其转换为 String不是请致电toString()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多