我想从我的Android应用程序向我的网络托管数据库中插入一个值。它会触发我的create.php脚本,但它不会将值插入我从编辑文本中键入的标题列。
这是我插入的方式
try {
URL url = new URL("mydburl/create.php");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
//conn.setDoInput(true);
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
//conn.setRequestProperty("Accept", "application/json");
//conn.connect();
//Log.d("In Async param",params[0]);
String title = params[0];
// Log.d("In Async title",title);
jsonObj = new JSONObject();
jsonObj.put("title", title);
//Log.d("In Async Json toString",jsonObj.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
try {
os = (conn.getOutputStream());
os.write(jsonObj.toString().getBytes("UTF-8"));
//os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
Log.e("connection status",conn.getResponseMessage());
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
strBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
strBuilder.append(line);
}
os.close();
reader.close();
conn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
在我的android studio log cat上,反映出连接状态正常。
我也在异步任务方法中做了一些调试。(fffg是我在编辑文本中输入的内容)
Log.d("In Async param",params[0]);
Log.d("In Async Json toString",jsonObj.toString());
这是我的php脚本(create.php)。
<?php
$response = array();
$servername = "myservername";
$username = "user";
$password = "pwd";
$dbname = "myDB";
$con=mysql_connect("$servername","$username","$password");
mysql_select_db("$dbname", $con);
$title=$_POST['title'];
mysql_query("INSERT INTO Test(title) VALUES ('$title')");
header('Content-type: application/json; charset=UTF-8');
?>
请帮忙。
答案 0 :(得分:0)
确保标题不为空白。
<?php
$response = array();
$servername = "myservername";
$username = "user";
$password = "pwd";
$dbname = "myDB";
$con=mysql_connect("$servername","$username","$password");
mysql_select_db("$dbname", $con);
if (isset($_POST['title'])) {
$title=$_POST['title'];
mysql_query("INSERT INTO Test(title) VALUES ('$title')");
header('Content-type: application/json; charset=UTF-8');
}
else {
echo "Required field is missing";
}
?>
答案 1 :(得分:0)
我认为原因是,当服务器读取为JsonObject
时,您正在发送key-value-pair
。尝试这样设置。
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("title", title);
String query = builder.build().getEncodedQuery();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
答案 2 :(得分:0)
管理解决它
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$json = file_get_contents('php://input');
$obj = json_decode($json);
$title = $obj->{'title'};
$sql = "INSERT INTO Test (title) VALUES ('$title')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();