1
I’m facing a problem that I don’t know how to solve right now.
To put it bluntly, I’m having trouble sending data in json to a php page through a java application. Basically, I have a php page that receives the data through the POST method and created a java class that sends this data via post. The problem is that in java I do not insert the "identifier" that is requested in the php page. As you can see, I take the value on the php page from the code snippet filter_input(INPUT_POST, "user")
, only that in the java application I do not insert this "user" identity in the information I want to send. Therefore, there is no way that the php page "catch" the value that the java application is sending.
Does anyone have any idea how to solve this problem? Thanks in advance!
PHP page:
<?php
require_once './vendor/autoload.php';
$controller = new App\CWS\Controller();
if($_SERVER['REQUEST_METHOD'] == "POST"){
$controller->cadastrarUsuario(filter_input(INPUT_POST, "user"));
}
?>
Class responsible for connecting and sending data in the Java application:
public class WebClient {
public String post(String json) {
try {
URL url = new URL("http://localhost//CWS//cadastrar_usuario.php");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
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;
}
}
I found this researching.
– Jefferson Hillebrecht
I tried it here and it didn’t work buddy. Thanks for trying to help!
– CloudAC