Engine cut on/off feature not working. Always comes Device is unavailable. message.
Here is my code for sendCommandToDevice function
public static String sendCommandToDevice(Long deviceId, String postData) throws IOException{
String response = "Message was not sent";
URL url = new URL("http://ec2-13-127-235-172.ap-south-1.compute.amazonaws.com:8082/api/commands/sendCommandToDevice/"+deviceId);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", String.valueOf(postData.length()));
OutputStream os = connection.getOutputStream();
os.write(postData.getBytes());
StringBuilder responseSB = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ( (line = br.readLine()) != null)
responseSB.append(line);
br.close();
os.close();
response = responseSB.toString();
System.out.println("\n***********************************************************");
System.out.println((new Date())+" CommandSent Response on =>"+deviceId+"<=: "+response);
System.out.println("***********************************************************\n");
if(response.contains("online")){
response = "Device is not online";
}
if(response.contains("supported")){
response = "Wrong command or Unsupported!";
}
return response;
}
Please help me its urgent.
Engine cut on/off feature not working. Always comes Device is unavailable. message.
Here is my code for sendCommandToDevice function
public static String sendCommandToDevice(Long deviceId, String postData) throws IOException{ String response = "Message was not sent"; //URL url = new URL("http://172.32.1.64:8082/api/commands/sendCommandToDevice/"+deviceId); URL url = new URL("http://ec2-13-127-235-172.ap-south-1.compute.amazonaws.com:8082/api/commands/sendCommandToDevice/"+deviceId); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Content-Length", String.valueOf(postData.length())); // Write datasss OutputStream os = connection.getOutputStream(); os.write(postData.getBytes()); // Read response StringBuilder responseSB = new StringBuilder(); //BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ( (line = br.readLine()) != null) responseSB.append(line); // Close streams br.close(); os.close(); response = responseSB.toString(); System.out.println("\n***********************************************************"); System.out.println((new Date())+" CommandSent Response on =>"+deviceId+"<=: "+response); System.out.println("***********************************************************\n"); if(response.contains("online")){ response = "Device is not online"; } if(response.contains("supported")){ response = "Wrong command or Unsupported!"; } return response; }
Please help me its urgent.