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?

3
  • 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[]? Commented Aug 11, 2021 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 Commented Aug 11, 2021 at 18:54
  • So that is not a JSON. Create a JSONObject instead of appending attributes using &. Commented Aug 11, 2021 at 19:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.