Java post请求返回200,但网页没有更改

时间:2017-03-23 05:38:33

标签: java php json

我在Java中使用JSON对象(使用Eclipse),我想将其发送到网络服务器,从JSON格式解码并在页面上显示。

我尝试发送JSON对象并返回响应代码,它给了我200,但是当我进入网页并刷新它并没有显示任何东西,好像它从未收到任何东西。

我试图查看类似的问题,但我相信我错过了一些东西或者我误解了一些东西。

这是我的Java代码: (我的json对象" obj"已填充,但我没有包含用于在下面创建和填充它的代码)

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);

这是我的php文件:

<?php
$string=$_POST['json'];
$decoded = json_decode($string);
echo $decoded;
?>
<!DOCTYPE html>
<html>
<body>
<h1> hello</h1>
</body>
</html>

编辑:我创建了一个MySQL数据库来存储来自Java的json字符串,这里是php文件的更新版本,我试图通过它来存储java脚本中的json字符串并访问它以显示它页。

<?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>

0 个答案:

没有答案