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?