I have a JSON object in Java ( using Eclipse ) that I wanna send to a webserver, decode it from JSON format and display it on the page.
I tried to send the JSON object and return the response code, it gives me 200, but when I go to the webpage and refresh it doesn't display anything as if it never received anything.
I tried to look through similar questions, however I believe I'm missing something or I misunderstood something.
Here is my Java code: (My json object "obj" is populated, but I didn't include the code for creating and populating it below)
String url = "http://localhost/scrapper.php";
URL obju = new URL(url);
HttpURLConnection con = (HttpURLConnection) obju.openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.addRequestProperty("json", obj.toString());
OutputStream os = con.getOutputStream();
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
//wr.write(new String("json=" + json).getBytes());
String param = "json=" + URLEncoder.encode(obj.toString(), "UTF-8");
wr.write(param.getBytes());
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println(responseCode);
And here is my php file:
<?php
$string=$_POST['json'];
$decoded = json_decode($string);
echo $decoded;
?>
<!DOCTYPE html>
<html>
<body>
<h1> hello</h1>
</body>
</html>
Edit: I created a MySQL database to store the json string coming from Java, here is the updated version of the php file through which I am trying to store the json string from the java script and access it to display it on the page.
<?php
require("config.php");
$string=$_POST['json'];
$decoded = json_decode($string);
mysqli_query($con, "Insert into champion VALUES('', '$decoded')");
?>
<!DOCTYPE html>
<html>
<body>
<h1> hello</h1>
<?php
$sql=mysqli_query($con, "Select * from champion");
while($row=mysqli_fetch_assoc($sql)){
$text=$row['text'];
echo $text;
}
?>
</body>
</html>