如何上传JSON请求Android的多部分图像数据吗?图像、上传、部分、数据

2023-09-06 18:14:12 作者:只有不甘心

我使用JSON请求,以便任何人都可以帮助我在这里多部分后上传图像数据。

I have to upload image data in multipart post using json request so can anyone help me out here.

下面是我的JSON字符串请求发送服务器(我能够在字符串创建JSON请求,但如何发送code要求)

Here is my json string request to send server (I am able to create JSON request in string but how to send that code is required)

{
"Request": {
    "data": {
        "Key": "keydata",
        "info": {},
        "param": {
            "title": "Testingpostmultipartimageupload",
            "photo": **"IMAGE BYTE DATA"**,
            "photoFilename": "Test.png"
        }
    },
    "info": {}
}

}

所以,我怎么可以在JSON图像字节数据web服务一起发布此数据...

So how can I post this data along with image byte data in JSON to webservice...

先谢谢了。

推荐答案

我发现我的解决方案,在此基础上一篇:http://prativas.word$p$pss.com/2014/03/03/uploading-image-from-android-app-to-server-programmatically/:

I found my solution based on this post: http://prativas.wordpress.com/2014/03/03/uploading-image-from-android-app-to-server-programmatically/:

和与JSON数据一个多合这样的:

And combined this with a multipart for the json data:

// the json values:
multiPartEntityBuilder.addPart("fieldname", new StringBody("value",ContentType.APPLICATION_JSON));

//the file:
File file = new File(filename);
FileBody fileBody = new FileBody(file);
multiPartEntityBuilder.addPart("FIle_Fieldname_depending_on_the_api", fileBody);

这文件上载到一个REST API(Django的restframework)

This uploads the file to a rest api (django restframework)