0
I have a problem and would like you to help me solve it. I created a java application to consume a webservice that I made in php. After a lot of searching I managed to create a class capable of making http requests. The problem is that I am encountering an error with the url I am using to access the webservice. The Webclient class is responsible for connecting to the webservice and returning a response (which would be a string in JSON format), it receives the data (also a string in JSON format) and tries to request with the server where the webservice is hosted. However I am encountering a malformation error in the url. Could anyone help me to solve this problem?
Webclient class
public class WebClient {
public String post(String json) {
try {
URL url = new URL("localhost/CWS/cadastrar_guia.php");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);
PrintStream output = new PrintStream(connection.getOutputStream());
output.println(json);
connection.connect();
Scanner scanner = new Scanner(connection.getInputStream());
String resposta = scanner.next();
return resposta;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
On Main
String guia = "{\"nome\":\"NomeUsuario\",\"sobrenome\":\"SobrenomeUsuario\",\"cpf\":\"99999999999\",\"email\":\"[email protected]\",\"senha\":\"00000\"}";
WebClient wc = new WebClient();
resposta = wc.post(guia);
System.out.println(resposta);
Error:
java.net.MalformedURLException: no protocol: localhost//CitourWS//cadastrar_guia.php
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at webservice.WebClient.post(WebClient.java:13)
at Main.main(Main.java:76)
It worked right here. Thank you very much!
– CloudAC