【问题标题】:Android Base64 Decoding Encoding incorrect when added to JSONObject添加到 JSONObject 时,Android Base64 解码编码不正确
【发布时间】:2017-02-06 15:09:19
【问题描述】:

我正在将图像编码为 Base64,因此我可以将其放入 JSONObject 并将其上传到 Web 服务器。

我遇到的这个问题是,当将编码的字符串放入 JSONObject 时,它会出现,它会将其更改为无效的内容。我一直在使用Online Base64 Decoder 来检查结果。

public static Triple<Boolean, String, JSONObject> validateAppC(Context context, String userId, String mParam1, String pack_status_id){

//**This is a larger method so I've only included code that creates the JSONObject directly**//

JSONObject getFields = new JSONObject();
    try{
        getFields.put("work_from_date", appCArray[0]);
        getFields.put("work_from_time", appCArray[1]);
        getFields.put("work_to_date", appCArray[2]);
        getFields.put("work_to_time", appCArray[3]);
        getFields.put("question_1", appCArray[4]);
        getFields.put("question_2", appCArray[5]);
        getFields.put("question_3", appCArray[6]);
        getFields.put("question_4", appCArray[7]);
        getFields.put("question_5", appCArray[8]);
        getFields.put("question_6", appCArray[9]);
        getFields.put("question_7", appCArray[10]);
        getFields.put("question_8", appCArray[11]);
        getFields.put("question_9", appCArray[12]);
        getFields.put("question_10", appCArray[13]);
        getFields.put("question_11", appCArray[14]);
        getFields.put("question_12", appCArray[15]);
        getFields.put("question_13", appCArray[16]);
        getFields.put("question_14", appCArray[17]);
        getFields.put("question_15", appCArray[18]);
        getFields.put("coss_accept_reject", appCArray[19]);
        getFields.put("coss_same_shift", appCArray[20]);
        getFields.put("coss_same_shift_reason", appCArray[21]);
        getFields.put("coss_rejected_reason", appCArray[22]);
        getFields.put("coss_recipient_name", appCArray[23]);
        getFields.put("coss_location", appCArray[24]);
        getFields.put("manager_name", appCArray[25]);
        getFields.put("manager_signature", appCArray[26]);

        String dir = context.getString(R.string.directoryUrl) + context.getPackageName() + "/";
        String filename = "user_" + userId + "_sig.png";

        if(pack_status_id.equals("4") || pack_status_id.equals("6")) {
            dir = context.getString(R.string.directoryUrl) + context.getPackageName() + "/files/" + pack_id + "/appendix_c/";
            filename = "appendix_c_coss_sig.png";
        }

        File directory = new File(dir);
        File mypath= new File(directory,filename);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap myBitmap = BitmapFactory.decodeFile(mypath.getAbsolutePath(), options);

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        myBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();

        String encoded = Base64.encodeToString(byteArray, Base64.NO_WRAP);

        //Here the encoded string is valid and produces my image perfectly.

        final int chunkSize = 2048;
        for (int i = 0; i < encoded.length(); i += chunkSize) {
            Log.d("encoded", encoded.substring(i, Math.min(encoded.length(), i + chunkSize)));
        }

        JSONObject cossSignatureData = new JSONObject();

        cossSignatureData.put("image_type", "image/png");
        cossSignatureData.put("image_data", encoded);

        getFields.put("coss_signature_data", cossSignatureData);

    }catch(JSONException e){
        e.printStackTrace();
    }

   return new Triple<>(isValid, str, getFields);
}

我使用以下代码调用此方法:

Triple validationResults = PackViewActivity.validateAppC(getApplicationContext(), userId, pack_id, pack_status_id);

        JSONObject getFields = (JSONObject)validationResults.third;

        data = "data=" + getFields;

        //Here the encoded string is NOT valid and produces nothing.

        final int chunkSize = 2048;
        for (int i = 0; i < data.length(); i += chunkSize) {
            Log.d("json upload appC", data.substring(i, Math.min(data.length(), i + chunkSize)));
        }

【问题讨论】:

    标签: android json encoding base64


    【解决方案1】:

    更新

    正如 cmets 中提到的 OP,问题在于 JSON 字符串中有反斜杠。该问题已通过执行 replaceAll 以消除反斜杠得到解决。


    原答案

    如果您在返回数据后将validateAppC() 内的循环输出与循环输出进行比较,那么这可能是您的问题...我假设您正在使用日志输出进行复制/粘贴到在线解码器中,所以这对我来说很有意义。

    1. 在第一个循环中,您将转储编码图像的子字符串
    2. 在第二个循环中,您将输出 getFields 对象的 toString() 方法的子字符串

    尝试将第二个 for 循环(第二个代码 sn-p 中的那个)更改为:

    String encoded = getFields.getString("image_data");
    for (int i = 0; i < encoded.length(); i += chunkSize) {
        Log.d("encoded", encoded.substring(i, Math.min(encoded.length(), i + chunkSize)));
    }
    

    【讨论】:

    • 感谢@thomaspsk 的回复当我按照您的建议打印日志时,我得到了相同的有效 base64 字符串。但是当它被放入 JSONObject 时,它变得无效。
    • 回过头来看,我发现 JSON 字符串包含反斜杠。用 replaceAll 删除这些就可以了。当您的回答导致我的解决方案时,如果您相应地更新您的回答,我会给您绿色的勾号。谢谢
    • 完成。很高兴你能查明真相
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    相关资源
    最近更新 更多