gpt4 book ai didi

java发送json到php并插入mysql,但返回null

转载 作者:行者123 更新时间:2023-11-29 15:36:15 25 4
gpt4 key购买 nike

我要通过java程序发送一个php并将其插入MYSQL

我可以通过浏览器成功插入(在MYSQL列中不返回null)但从 java 返回空列

URL url = new URL(urlWebService);
DataOutputStream os;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type","application/json; charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestMethod("POST");
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
// add header
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
JSONObject jsonParam = new JSONObject();
jsonParam.put("type", type);
jsonParam.put("question", question);
jsonParam.put("answerA", answerA);
jsonParam.put("answerB", answerB);
jsonParam.put("answerC", answerC);
jsonParam.put("answerD", answerD);
jsonParam.put("creator", corrctAnswer);

//Log.i("JSON", jsonParam.toString());
os = new DataOutputStream(conn.getOutputStream());
os.writeUTF(URLEncoder.encode(jsonParam.toString(),"UTF-8"));
//os.writeUTF(jsonParam.toString());
os.flush();
os.close();

enter image description here

如何解决“null”问题

这是我的 PHP 代码

<?php
header("Content-Type: application/json; charset=UTF-8");
$json = $_GET["json"];
$obj = json_decode($json);

$type = $obj -> {"type"};
$question = $obj -> {"question"};
$answerA = $obj -> {"answerA"};
$answerB = $obj -> {"answerB"};
$answerC = $obj -> {"answerC"};
$answerD = $obj -> {"answerD"};
$creator = $obj -> {"creator"};


$servername = "localhost";
$username = "root";
$password = "";
$dbname = "DB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

mysqli_set_charset($conn,"utf8");
$stmt = $conn->prepare("INSERT INTO stjohn (type, question, answerA, answerB, answerC, answerD, creator) VALUES
(?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssss", $type, $question, $answerA, $answerB, $answerC, $answerD, $creator);
$stmt->execute();
echo "New records created successfully";

$stmt->close();
$conn->close();
?>

希望有人能帮忙我不擅长 json 和 php

最佳答案

您的代码中有几件事我不太确定。其中之一是在多行中设置多个请求属性。

URL url = new URL(urlWebService);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-type", "application/json");

JSONObject jsonParam = new JSONObject();
jsonParam.put("type", type);
jsonParam.put("question", question);
jsonParam.put("answerA", answerA);
jsonParam.put("answerB", answerB);
jsonParam.put("answerC", answerC);
jsonParam.put("answerD", answerD);
jsonParam.put("creator", corrctAnswer);

OutputStream os = connection.getOutputStream();
os.write(jsonParam.toString().getBytes());
os.flush();
os.close();

并在文档明确提到字节输入时将字符串放入 os.write 中。我很确定这个片段会为您完成这项工作。

关于java发送json到php并插入mysql,但返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58215879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com