Android使用POST方法将JSON数据传递到服务器

用户名

我有以下带有base64图片内容的JSON字符串。您能帮我如何将该JSON发布为Multipart:

{
    "TakeoffID": "2",
    "address": ",nexhex",
    "city": "Xrk Zed",
    "state": "AZ",
    "date": "12/08/2015",
    "ViewNote": "",
    "ViewPhoto1": "base64ImageContent",
    "ViewPhoto2": "base64ImageContent",
    "ViewPhoto3": "base64ImageContent",
    "TakeoffDoneBy": "Jxehx",
    "AcctName": "Gsgve",
    "LoginUserID": "46669",
    "jobId": "whshs",
    "LineItems": [
        {
            "OrderLineid": "544",
            "OrderLineTypeid": "Post Light",
            "OrderLineQty": "2",
            "OrderLinePhoto1": "base64ImageContent",
            "OrderLinePhoto2": "base64ImageContent",
            "OrderLinePhoto3": "base64ImageContent",
            "OrderLineNotes": "",
            "OrderLineLocation": "Post Lights"
        }
    ]
}
命中

一种简单的方法是,首先将您的请求json转换为简单的地图,例如

Map<String, String> map = new HashMap<>();

对于“ LineItems”:将“ LineItems”作为键,并将json的值作为字符串格式并添加到此映射中。

然后使用以下方法调用Webservice。

private JSONObject sendRequest(String urlString, Map<String, String> map, String fileKey,  File file) {
        StringBuilder strData= null;
        JSONObject resObj = null;
        try {
            Log.i("Send request", urlString+"="+map);
            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(50000);
            conn.setConnectTimeout(50000);
            conn.setRequestMethod("POST");
            conn.setUseCaches(false);
            conn.setDoInput(true);
            conn.setDoOutput(true);

            if(map == null)
            {
                map = new HashMap<>();
            }
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            for (HashMap.Entry<String, String> entry : map.entrySet()) {
                String k = entry.getKey();
                String v = entry.getValue();
                reqEntity.addPart(k, new StringBody(v));
            }

            if(file != null && !TextUtils.isEmpty(fileKey))
            {
                FileBody filebody = new FileBody(file, "image/*");
                reqEntity.addPart(fileKey, filebody);
            }

            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.addRequestProperty("Content-length", reqEntity.getContentLength() + "");
            conn.addRequestProperty(reqEntity.getContentType().getName(), reqEntity.getContentType().getValue());
            OutputStream os = conn.getOutputStream();
            reqEntity.writeTo(os);
            os.close();
            conn.connect();
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String sResponse;
                strData = new StringBuilder();
                while ((sResponse = reader.readLine()) != null) {
                    strData = strData.append(sResponse);
                }
            }
            if(strData != null)
                resObj = new JSONObject(strData.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resObj;
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用FastAPI将JSON数据传递到服务器的简约方法

将图像数据传递到Flask服务器

TcpClient的write方法是否保证将数据传递到服务器?

标准表单提交呈现页面,但是通过$ .post()将数据传递到服务器不会呈现页面

使用内容安全策略将服务器数据传递到客户端脚本

将服务器凭据传递到SSRS

将数据传递到服务器时进行深度链接

如何将服务器数据传递到React组件

将服务器端数据传递到Angular

将数据传递到后端(本地)服务器的“Access-Control-Allow-Origin”问题

使用Flask将JSON数据从服务器传递到客户端

将数据传递到Android中的服务

使用http.Post将json数据发布到远程服务器

Android Volley将json数据发布到服务器

使用php post通过json从android发布到服务器后,exif数据丢失

使用 MVP Android 将数据发布到服务器

如何使用retrofit2将json数据发布到android中的服务器?

如何将 JSON 数据从服务器传递到客户端并在 Javascript 中使用数据?

使用Ajax将表单数据传递到控制器方法

使用POST方法和HttpURLConnection将Android的JSON对象发送到PHP服务器

将表单数据传递给 JSP 而不传递给服务器

将数据传递给服务-Android?

将Json数据传递到HTML

将JSON数据传递到模板

如何使用无服务器功能和 axios 将开放的天气 API 数据传递给 React 前端

如何将 Nexus 凭据传递到 Azure DevOps 服务器

如何使用post方法(JSON)传递自定义标头并从服务器获取数据

使用 AJAX Post 将 Excel 文件传递到服务器

将数据从React传递到节点服务器