我正在将图像编码为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)));
}
答案 0 :(得分:1)
<强>更新强>
正如评论中提到的OP,问题是JSON字符串中有反斜杠。通过执行replaceAll来消除反斜杠解决了这个问题。
原始答案
如果您在返回数据后将validateAppC()
内的循环输出与循环输出进行比较,则可能是您的问题......我假设您正在使用日志输出复制/粘贴到在线解码器中,这对我来说很有意义。
getFields
对象的toString()
方法的子字符串尝试将第二个for循环(第二个代码段中的循环)更改为:
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)));
}