有 Java 编程相关的问题?

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

java Android:将UTF8数据发送到MySQL

我用php和json将数据保存到mysql数据库,但我的数据转换为?,我在php和db connect以及java代码中使用utf-8

php

<?php
header("Content-type: application/json; charset=utf-8");
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');

$hostname='localhost';
$username='ekht3r44_6785h5h';
$password='IFVB!8Nw{#-S';
$response = array();

.
.
.
$dbh=new PDO("mysql:host=$hostname;dbname=db;charset=utf8mb4",$username,$password);

$sql="INSERT INTO contact_form (name,email,subject,message) VALUES (".$_POST['name'].",".$_POST['email'].",".$_POST['subject'].",".$_POST['message'].");";

$statement = $dbh->prepare("INSERT INTO contact_form (name,email,subject,message) VALUES (:name,:email,:subject,:message);");
$statement->execute(array(':name' => $_POST['name'],':email' => $_POST['email'],':subject' => $_POST['subject'],':message' => $_POST['message']));
.
.
.
?>

爪哇

protected String doInBackground(String... args) {
    try {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", et_name.getText().toString()));
        params.add(new BasicNameValuePair("email", et_email.getText().toString()));
        params.add(new BasicNameValuePair("subject", et_subj.getText().toString()));
        params.add(new BasicNameValuePair("message", et_msg.getText().toString()));
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        is.close();
        page_output = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    return page_output;
}

你能检查一下哪里出了问题吗


共 (1) 个答案

  1. # 1 楼答案

    请检查MySQL中表的字符集。“可能”就是原因