如何在jsonobject

时间:2019-06-25 19:49:55

标签: java android json websocket base64

我想将图像放在jsonobject中,因此使用(base64-encodetostring)将其转换为字符串,由于base64 encodetostring标志或idk,我不认为我尝试了多种方法来解决该问题,但没有帮助,也没有解决方案互联网。

我已经尝试过base64.DEFAULT标记,并且任何单个FLAG都没有帮助,因为我认为这是因为base64.encodetostring并没有为字符串或idk设置到底是什么

                pdp.compress(Bitmap.CompressFormat.JPEG,100,bytes);

                byte[] bytes1=bytes.toByteArray();

                Pdp= Base64.encodeToString(bytes1,Base64.NO_WRAP|Base64.URL_SAFE);

然后我将确切的Pdp字符串放入jsono

                query.put("new_pdp",",pdp=cast('"+pdp+"==' as varbinary(max))");
            }else query.put("new_pdp","");

我将其记录到控制台,这是仅有的编码字节的结果:

_9j_4AAQSkZJRgABAQAAAQABAAD_4gIoSUNDX1BST0ZJTEUAAQEAAAIYAAAAAAIQAABtbnRyUkdCIFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAAHRyWFlaAAABZAAAABRnWFlaAAABeAAAABRiWFlaAAABjAAAABRyVFJDAAABoAAAAChnVFJDAAABoAAAAChiVFJDAAABoAAAACh3dHB0AAAByAAAABRjcHJ0AAAB3AAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAFgAAAAcAEEAZABvAGIAZQBSAEcAQgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFhZWiAAAAAAAACcGAAAT6UAAAT8WFlaIAAAAAAAADSNAACgLAAAD5VYWVogAAAAAAAAJjEAABAvAAC-nHBhcmEAAAAAAAQAAAACMzMAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABYWVogAAAAAAAA9tYAAQAAAADTLW1sdWMAAAAAAAAAAQAAAAxlblVTAAAAIAAAABwARwBvAG8AZwBsAGUAIABJAG4AYwAuACAAMgAwADEANv_bAEMAAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf_bAEMBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf_AABEIBRQFFAMBIgACEQEDEQH_xAAfAAEAAAYDAQEAAAAAAAAAAAAABAUGCQoLAwcIAQL_xAB-EAABAgYAAwIJBQgLCAwHAh8BAAIDBAUGBxEIEiEJMQoTFBUWIkFRYTJxgZHwFyMkJjahsdElJzU3QkZSVsHh8RgzNEVVYmaCQ0RUZGVncoOWorLndHV2d4aSlbMZGihTY5SXpra3x0eFtdI4OVeTxddYaHN4hIejpcLT5v_EABoBAQEBAQEBAQAAAAAAAAAAAAAEBQEGAwL_xAA-EQACAQEGAwUFBwQCAgMBAQAAAREhAgQxQVFhcZHwEhSBobEFIsHR8QMyNEJisuEVJFKicoITkiNTwkRD_9oADAMBAAIRAxEAPwDP4REQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBFK5yqU6nj8OnpOU33iZm2s-sOI7_sFNEAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREARQ6iEAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBQsxHgwIXjo8bydvTbnEdO_pojr9XT4L5MRoEtCMePGEvLwB6xJ5W6AAG-hJ9wbon86pKHLGuEzNSlzBorWl0lT5kN1Pahhxq9ZZ6umAMHiobj0B5nknvA6jrU4JyaqUadbNTEw0Bs40SGg1rQAGjXcAAABoa9gXaVkNjQKC2WcIfkcvPlsmH9OYdBEG-8u8_iYAPTp07lI6PbFCqk1UY3iY8ajyw8hkpvyqJJDoG-MDRDdBe4NLmgRDyjqNs2QB2nLwYEtCECBBEvLwB6oA5W6AJOupJ95dsn8yAi0REAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBSmqz0aQptQnZeAZmZlZN022T36zixriW7HwY4nX8n4hRczGl5GAY8eKJeWgN9Yno3XsHtJHs1r4_FUZMUZtxPNRqomZaXldijSgLAZB7QC2tbDgTEIOwHb5Wg779gDqHx_wCGeUeUfhXl-_Lf8b67t-7f0_H4rvK0JiZnrepsxPxPKZkwtmc9Xc5rm--jfXTurSD09Tv0DqhKBa8nVIE1HnGzRlvKnyMg-TJpbZ-lt5OSuOaNPPjOZx2CNch2CXBdtS8GBLQhAgQRLy8AeqAOVugCTrqSfeXbJ_MgItERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBFCTEaBLQjHjxhLy8AesSeVugABvoSfcG6J_OqNdX6_PQTMU62XTEsNBhm6zDpbpwHoTDBhuAB0d87S36OiA5mObctUmxGI8zUOdfJGWcARPVSHrmiOPQFkLxgDW76ucPe4j7PeNuKcmKRBEaW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AUGCQoLAwcIAQL_xAB-EAABAgYAAwIJBQgLCAwHAh8BAAIDBAUGBxEIEiEJMQoTFBUWIkFRYTJxgZHwFyMkJjahsdElJzU3QkZSVsHh8RgzNEVVYmaCQ0RUZGVncoOWorLndHV2d4aSlbMZGihTY5SXpra3x0eFtdI4OVeTxddYaHN4hIejpcLT5v_EABoBAQEBAQEBAQAAAAAAAAAAAAAEBQEGAwL_xAA-EQACAQEGAwUFBwQCAgMBAQAAAREhAgQxQVFhcZHwEhSBobEFIsHR8QMyNEJisuEVJFKicoITkiNTwkRD_9oADAMBAAIRAxEAPwDP4REQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBFK5yqU6nj8OnpOU33iZm2s-sOI7_sFNEAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREARQ6iEAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBQsxHgwIXjo8bydvTbnEdO_pojr9XT4L5MRoEtCMePGEvLwB6xJ5W6AAG-hJ9wbon86pKHLGuEzNSlzBorWl0lT5kN1Pahhxq9ZZ6umAMHiobj0B5nknvA6jrU4JyaqUadbNTEw0Bs40SGg1rQAGjXcAAABoa9gXaVkNjQKC2WcIfkcvPlsmH9OYdBEG-8u8_iYAPTp07lI6PbFCqk1UY3iY8ajyw8hkpvyqJJDoG-MDRDdBe4NLmgRDyjqNs2QB2nLwYEtCECBBEvLwB6oA5W6AJOupJ95dsn8yAi0REAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBSmqz0aQptQnZeAZmZlZN022T36zixriW7HwY4nX8n4hRczGl5GAY8eKJeWgN9Yno3XsHtJHs1r4_FUZMUZtxPNRqomZaXldijSgLAZB7QC2tbDgTEIOwHb5Wg779gDqHx_wCGeUeUfhXl-_Lf8b67t-7f0_H4rvK0JiZnrepsxPxPKZkwtmc9Xc5rm--jfXTurSD09Tv0DqhKBa8nVIE1HnGzRlvKnyMg-TJpbZ-lt5OSuOaNPPjOZx2CNch2CXBdtS8GBLQhAgQRLy8AeqAOVugCTrqSfeXbJ_MgItERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBFCTEaBLQjHjxhLy8AesSeVugABvoSfcG6J_OqNdX6_PQTMU62XTEsNBhm6zDpbpwHoTDBhuAB0d87S36OiA5mObctUmxGI8zUOdfJGWcARPVSHrmiOPQFkLxgDW76ucPe4j7PeNuKcmKRBEaWo0qXSlYmxyAVB3yIlGh707XLF1FiAnTYnKNl-nyGmVmSpcSpxnCZgU6YqDXVqSqDS-qUCrV-Ix4c4aZDZbpAHI7ryPJewFjXME_nbVkajMmfl5iYlBND9mBJANZXWBg5WxDzHuLSOZns00DptARIMapMdIUotpdOlmmTM9LBsMkBvTzK6GCwBmuUxCA0AAAO11iqJOx5p1Qk5qYl5mZp014t0eWGgecFwD2ddRG9WuHXvI9nXimJjx0Y0ikkS_koIm5tuvwAAEgAOOi4gHvOiPhsqdycnLSMHyeVgNgQG9Q0Hod9_TZI6e8_nQEYiIgCIiAIiIAiIgCIiAIiIAiIgCIiAIiIAiIgCIiAIiIAiIgCIiAIiIAiIgCIiAIiIAqdnrikJKaFPPlUzPlocZSRlYk9EAI3shobrQ79uaR12FC1OqRpma80UnTqgQfLJwFj20RjmN094IG3OG9AHrvZ3zACFPktFYKRRILZiqPBnXCbcTyl4ax1ZrkUCHEO3jvDRELgOVrTyhgHKbp8SXCPQa_LSwH-FmRJHvBIB_Od9N6B0p7IT9PqUHx8jMy000deeUcCPm3vfu66A30VOxajdsoJeNFpNNqUDR8sNKm4jXg6PrMbMNBcBrZ0CO_ehpS0SdqXHMPq0jPTVMqZBkZqJIT3mqqDuHi3gO5g7qCCByubvR2CgK5nZ2RkYJjT8zLSsD2vmphsNvf_AJ5Id7O4nSl0Cu0ediS8GDVaZMTEyHGTbJzrXl4DdkgMc7r07j1PQaVNy8vZVDmxLy8FtUqw7w0edaofn3s-7uI6d_Rc4gWxXI0tJTNJdTZotE9JsmpEUypBwOnOaOXn31cCOb2a5dgFAV6ioqlz0xTJqWoNWjTEyXcopNYiaJqI5dljywub4xrSN723WgSRylVqgCIiAIiIAiIgCIiAIiI"
}

3 个答案:

答案 0 :(得分:1)

对Base64进行解码和编码可能会占用大量内存。如果图像在服务器上,只需获取URL并查询。

答案 1 :(得分:0)

这是我的操作方式:

     String photoEncodedString = ""; 

    if (this.theBitmap != null) {           
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        this.theBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] bytes = stream.toByteArray();
        photoEncodedString = Base64.encodeToString(bytes, Base64.DEFAULT);
    }

    String dataString = "[{";

    dataString = dataString + "\"PhotoImage\": \"" + photoEncodedString + "\" ";        
    dataString = dataString + ",\"Name\": \"" + etYourName.getText().toString() + "\" ";
    dataString = dataString + ",\"Email\": \"" + etEmail.getText().toString() + "\" ";
    dataString = dataString + ",\"Comments\": \"" + tvCommentsValue.getText().toString() + "\" ";

    dataString = dataString + "}]";

    JSONObject jObj = new JSONObject(dataString);

我认为您的.put错误与JSON格式错误有关。你有

    { "operation":"edit_profile",
"username":"frsyescd",
"password":"123Ch@rif",
"new_email":"",
"new_pdp":",pdp=cast('_9j_4AAQSkZJRgABAQAAAQABAAD

pdp = cast之前有逗号的地方。
另外“ new_pdp”:“ pdp = cast(”也不是有效的JSON。

我只是说这不是有效的JSON,因此,它将在某处导致错误:

 "new_pdp":",pdp=cast('_9j_4AAQSkZJRgABAQAAAQABAAD..._svmCveT8v

根据上面发布的内容,您还没有完成字符串操作,因此您需要使其看起来更像这样才能使其成为有效的JSON:

"new_pdp":"_9j_4AAQSkZJRgABAQAAAQABAAD..._svmCveT8v"

这又是我的代码,但是具有所需的JSON格式:

 String photoEncodedString = ""; 

if (pdp != null) {           
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    pdp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] bytes = stream.toByteArray();
    photoEncodedString = Base64.encodeToString(bytes, Base64.DEFAULT);
}

String dataString = "[{";

dataString = dataString + "\"operation\":\"edit_profile\" ";        
dataString = dataString + ",\"username\":\"frsyescd\" ";
dataString = dataString + ",\"password\":\"123Ch@rif\" ";
dataString = dataString + ",\"new_pdp\":\"" + photoEncodedString + "\""; 

dataString = dataString + "}]";

JSONObject jObj = new JSONObject(dataString);

答案 2 :(得分:0)

如上文@Haider Malik编码和解码所述,

base64对内存不敏感,作为一种解决方案,您可以使用firebase中的firestore作为数据库提供程序,并将图像上传到那里。 示例上传图片将如下所示:

    InputStream stream = new FileInputStream(new File("path/to/images/rivers.jpg"));

uploadTask = mountainsRef.putStream(stream);
uploadTask.addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle unsuccessful uploads
    }
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        // taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.
        // ...
    }
});StorageActivity.java

Official documentation