我需要在 MySQL 数据库中存储字符串“Wiadmość”,但更新的记录具有值“Wiadomość”。这是我的 POST 方法
private static String post(String endpoint, List<NameValuePair> params) throws IOException
{
HttpParams httpParameters = new BasicHttpParams();
HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8);
HttpProtocolParams.setHttpElementCharset(httpParameters, HTTP.UTF_8);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(endpoint);
String responseBody="NULL RESPONSE";
httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
ResponseHandler<String> responseHandler=new BasicResponseHandler();
responseBody = httpclient.execute(httppost, responseHandler);
return responseBody;
}
我的 PHP 脚本:
<?php
if (isset($_POST["did"]) && isset($_POST["dpw"]) && isset($_POST["msg"])) {
$did = $_POST["did"];
$dpw = $_POST["dpw"];
$msg = $_POST["msg"];
// Store user details in db
include_once './db_functions.php';
$db = new DB_Functions();
$res = $db->storeMessage($did, $dpw, $msg);
if ($res)
echo "1";
else
echo "0";
echo $msg;
} else {
// user details missing
}
?>
通过 Wireshark,我捕获了来自 php 脚本($msg 值)的回显响应,它看起来像“1Wiadomo\305\233\304\207”。由 android 设备发送的字符串也被wireshark 捕获,如下所示:“msg=Wiadomo%C5%9B%C4%87”。从“List NameValuePair params”获得的字符串看起来正确。数据库编码是正确的(utf8_general_ci),并且通过手动记录编辑它存储了正确的数据。
DB_Connect.php
class DB_Connect {
// constructor
function __construct() {
}
// destructor
function __destruct() {
// $this->close();
}
// Connecting to database
public function connect() {
require_once 'config.php';
// connecting to mysql
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_set_charset("utf8",$con);
// selecting database
mysql_select_db(DB_DATABASE);
// return database handler
return $con;
}
// Closing database connection
public function close() {
mysql_close();
}
}
?>