【问题标题】:Android passing JSON data using POST method to serverAndroid 使用 POST 方法将 JSON 数据传递到服务器
【发布时间】:2015-12-08 07:24:47
【问题描述】:

我有以下带有 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"
        }
    ]
}

【问题讨论】:

  • 既然是Base64编码的图片,不应该直接发送一个普通的application/json请求吗?过去OkHttp Recipes。它有你需要的一切。
  • 确实如此。将 json 作为多部分发送是没有意义的。
  • @Ravi,您是否尝试过以下答案并且是否有效?

标签: android json android-parser


【解决方案1】:

一种简单的方法是,首先将您的请求 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;
    }

【讨论】:

  • 你能解释一下,当我共享 JSONString 时,上述方法将如何适用于我的 JSON 字符串
  • 您可以将上面的 json 转换为 Map,例如:map.put("TakeoffID", "2");等等..使用此地图引用调用上述方法。
  • map.put("LineItems", "[ { "OrderLineid": "544", "OrderLineTypeid": "Post Light", "OrderLineQty": "2", "OrderLinePhoto1": "base64ImageContent ", "OrderLinePhoto2": "base64ImageContent", "OrderLinePhoto3": "base64ImageContent", "OrderLineNotes": "", "OrderLineLocation": "Post Lights" } ]");
猜你喜欢
  • 1970-01-01
  • 2020-11-07
  • 1970-01-01
  • 1970-01-01
  • 2015-10-05
  • 1970-01-01
  • 2012-07-03
  • 2018-02-07
  • 2015-02-26
相关资源
最近更新 更多