I am getting mad with this code below.
When i send the request using the string directInput, it's working fine. Unfortunately when i used the json object converted into string jsonInput i got 404 error code. the directInput and jsonInput return the same values.
Someone could please help troubleshooting ? Thanks
conn.setRequestProperty("Content-Type", "application/json;");
conn.setRequestProperty("Accept", "application/json");
conn.setDoOutput(true);
conn.setRequestProperty("Authorization", basicAuth);
// String directInput = "{\"msisdn\":\"22997858711\",\"amount\":1400,\"transref\":\"QOVNPVTRATYCBK8VIL1A\",\"clientid\":\"UBHQ\"}";
DepositObject d = new DepositObject();
d.setMsisdn("22997858711");
d.setAmount(35);
d.setTransref("QOVNPVTRATYCBK8VIL1A");
d.setClientid("UHBQ");
Gson gson = new Gson();
String jsonInput = gson.toJson(d).toString();
OutputStream os = conn.getOutputStream();
os.write(jsonInput.getBytes());
os.flush();
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String output;
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}