0

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>
mrwhite
  • 105
  • 12
  • You're using two different clients, one being a Java application and the other being your browser. You're incorrectly assuming that changes in client 1 will be reflected in client 2. Your java application receives a response to its request and your web application receives a response to its request, but these are two different requests and two different responses. – Robby Cornelissen Mar 23 '17 at 05:41
  • Robby Cornelissen, what should I do then? Use the java code as an applet embedded in the html file? or? – mrwhite Mar 23 '17 at 05:44
  • Depends on what you're trying to achieve. – Robby Cornelissen Mar 23 '17 at 05:46
  • Ok, here is what I want to achieve: I created a web scrapper in java that would get information from a webpage and it stores it in a json object. From there I wanna send the json object to a php file where I can get that information from the json object and display it to the browser so that whenever I access that php file, it will get the info from the java script and display it. – mrwhite Mar 23 '17 at 05:49
  • You'd have to store it somewhere on the PHP side, either in memory, a file or a database. Far easier though would be to either 1) also implement the web scraping logic in PHP; or 2) also implement the presentation/web part in Java (JSP, servlet, ...) – Robby Cornelissen Mar 23 '17 at 05:53
  • Then I would use a MySQL database for storage, but how do I get the information into the php file if they are currently on two separate clients that can't exchange information? Or it is simply not showing it because it's not stored when it's received? – mrwhite Mar 23 '17 at 05:55
  • The clients are separate, but the server is the same. 1) Java app posts info to PHP server; 2) PHP server stores info; 3) Browser requests info from PHP server; 4) PHP server retrieves info from storage and returns it to browser – Robby Cornelissen Mar 23 '17 at 05:57
  • I created the database and updated the php file, you can see it to the bottom of the original post for I made an edit to add it. However, it seems that the json data doesn't reach to the php cause after I run my Java program and get the return code 200, it adds to the database 5 empty records. Do I need a form with an input field on the file that java tries to send the info? – mrwhite Mar 23 '17 at 06:13
  • A little bit of research and/or debugging goes a long way: http://stackoverflow.com/a/4206094/3558960 I suggest that if you run into any more problems, you ask a new question on SO. – Robby Cornelissen Mar 23 '17 at 06:30

0 Answers0