androidjsonandroid-parser

Android passing JSON data using POST method to server


I have following JSON string with base64 image content. Will you help me how can I post this JSON as 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"
        }
    ]
}

Solution

  • One simple way to do this is, first convert your request json into simple map like

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

    For "LineItems" : "LineItems" as key and it values json as string format and add into this map.

    Then use below method to call the 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;
        }