有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我将通过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

如何解决“空”问题

这是我的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


共 (1) 个答案

  1. # 1 楼答案

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

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

    {cd1}在放入文档时显式地提到{cd1}。我很确定这段代码可以帮你完成任务