0

String jsondata = "{"system":"amer","boxInfo":{"customerId":"X999","boxNumber":"608159","boxImagedIndicator":true}}";

The above mentioned is the data I have to pass in the httpsurlconnection. Here is my code:

String jsondata = "{\"system\":\"amer\",\"boxInfo\":{\"customerId\":\"X999\",\"boxNumber\":\"608159\",\"boxImagedIndicator\":true}}";
        Map<String,Object> params = new LinkedHashMap<>();
        Map<String,Object> boxinfo = new LinkedHashMap<>();
        boxinfo.put("customerId", "X999");
        boxinfo.put("boxNumber", "608159");
        boxinfo.put("boxImagedIndicator", "true");
        params.put("system", "amer");
//        params.put("boxInfo", boxinfo);
        StringBuilder postBoxData = new StringBuilder();
        for (Map.Entry<String,Object> param : boxinfo.entrySet()) {
            if (postBoxData.length() != 0) postBoxData.append('&');
            postBoxData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
            postBoxData.append('=');
            postBoxData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
        }
        StringBuilder postData = new StringBuilder();
        for (Map.Entry<String,Object> param : params.entrySet()) {
            if (postData.length() != 0) postData.append('&');
            postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
            postData.append('=');
            postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
        }
        postData.append('&').append(URLEncoder.encode("boxInfo", "UTF-8"));
        postData.append('=').append(String.valueOf(postBoxData));
        byte[] postDataBytes = postData.toString().getBytes("UTF-8");
        String str = "https://url";
        URL url = new URL(str);
        SSLFix.execute();
        try{
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json; utf-8");
        connection.setConnectTimeout(30000);
        connection.setReadTimeout(30000);
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("ClientAuthorization", "Basic RTJFSU9ERGFzaGJvYXJkX0FQSV9LZXk6ZDRmMjI4NjYtMjM1MS00ZmE1LThmNzUtYWEwMWEyYzIzM2Vh");
        connection.setRequestProperty("UserID", "RTJFSU9ERGFzaGJvYXJkX0FQSQ==");
        connection.setDoOutput(true);
        OutputStream os = connection.getOutputStream();
        os.write(postDataBytes);
        os.close();
        connection.connect();
        BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));

But I get 400 errors. How to pass the above-mentioned JSON data correctly?

Richard Wilson
  • 297
  • 4
  • 17
  • You don't seem to be sending it as a valid JSON. Can you show us what is the value of `postData` before it is converted to `byte[]`? – aksappy Aug 11 '21 at 18:26
  • @aksappy postData = system=amer&boxInfo=customerId=X999&boxNumber=608159&boxImagedIndicator=true It will look like this. I dont know whether its correct – Kabilan Nakkeeran Aug 11 '21 at 18:54
  • So that is not a JSON. [Create a JSONObject](https://stackoverflow.com/questions/20117148/how-to-create-json-object-using-string) instead of appending attributes using &. – aksappy Aug 11 '21 at 19:55

0 Answers0