It has Android application adds data to the database, But when you enter data in the Arab, Show "?????" . I tried all the ways.
它有Android应用程序向数据库添加数据,但是当您在阿拉伯语中输入数据时,显示“?????” 。我尝试了所有方法。
File config.inc.php :
文件config.inc.php:
<?php
$username = "*****";
$password = "****";
$host = "*****";
$dbname = "*****";
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try
{
$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
mysql_set_charset('utf8');
}
catch(PDOException $ex)
{
die("Failed to connect to the database: " . $ex->getMessage());
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
function undo_magic_quotes_gpc(&$array)
{
foreach($array as &$value)
{
if(is_array($value))
{
undo_magic_quotes_gpc($value);
}
else
{
$value = stripslashes($value);
}
}
}
undo_magic_quotes_gpc($_POST);
undo_magic_quotes_gpc($_GET);
undo_magic_quotes_gpc($_COOKIE);
}
?>
Addition file :
加法文件:
<?php
require("config.inc.php");
$query = "INSERT INTO news ( news_date, news_title, news_body ) VALUES ( :news_date, :news_title, :news_body ) ";
$query_params = array(
':news_date' => $_POST['news_date'],
':news_title' => $_POST['news_title'],
':news_body' => $_POST['news_body']
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex)
{
$response["success"] = 0;
$response["message"] = "Database Error. Couldn't add News!";
die(json_encode($response));
}
$response["success"] = 1;
$response["message"] = "News Successfully Added!";
echo json_encode($response);
?>
In the Android project : Java file :
在Android项目中:Java文件:
public class addnews extends Dialog {
private Activity mActivity;
private EditText mNewsTitleET;
private EditText mNewsET;
private Button mPublishBtn;
public String username;
JSONParser jsonParser = new JSONParser();
private String ADD_URL =
"http://yazanyazan3.esy.es/newssite/addnews.php";
public addnews(Activity mActivity)
{
super(mActivity);
this.mActivity = mActivity;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_addnews);
mNewsTitleET = (EditText) findViewById(R.id.title_news);
mNewsET = (EditText) findViewById(R.id.news_box);
mPublishBtn = (Button) findViewById(R.id.publish_btn);
Bundle use =getIntent().getExtras();
username = use.getString("name");
mPublishBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
attempAdding();
}
});
}
private void attempAdding()
{
if (!mNewsTitleET.getText().toString().equals("") &&
!mNewsET.getText().toString().equals(""))
{
new AddNewsTask().execute();
}
else
{
Toast.makeText(mActivity.getApplicationContext(),
"All fields are requires", Toast.LENGTH_LONG).show();
}
}
public Intent getIntent() {
return mActivity.getIntent();
}
private class AddNewsTask extends AsyncTask<Void, Void, Boolean>
{
private ProgressDialog mProgressDialog;
private JSONObject jsonObjectResult = null;
private String error;
@Override
protected void onPreExecute()
{
super.onPreExecute();
mProgressDialog = ProgressDialog.show(addnews.this.getContext(),
"Processing...", "Adding new news", false, false);
}
@Override
protected Boolean doInBackground(Void... params)
{
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("news_user",username));
pairs.add(new BasicNameValuePair("news_date", dateFormat.format(date).toString()));
pairs.add(new BasicNameValuePair("news_title", mNewsTitleET.getText().toString()));
pairs.add(new BasicNameValuePair("news_body", mNewsET.getText().toString()));
jsonObjectResult = jsonParser.makehttprequest(ADD_URL+"?user="+username+"", pairs);
if (jsonObjectResult == null)
{
error = "Error in the connection";
return false;
}
try
{
if (jsonObjectResult.getInt("success") == 1)
{
error = jsonObjectResult.getString("message");
return true;
}
else
error = jsonObjectResult.getString("message");
}
catch (Exception ex)
{
}
return false;
}
@Override
protected void onPostExecute(Boolean aBoolean)
{
super.onPostExecute(aBoolean);
mProgressDialog.dismiss();
Toast.makeText(mActivity.getApplicationContext(), error, Toast.LENGTH_LONG).show();
dismiss();
}
} }
In the database, I choose utf8 . And tried all the solutions did not work . Please help.
在数据库中,我选择utf8。并尝试了所有的解决方案都没有用。请帮忙。
1 个解决方案
#1
0
Hope these tips will help :
希望这些提示有所帮助:
Try adding a static text into database in your addition file (do not read it from request) so you can divide the problem into two parts and you can see if the problem is from android client to PHP server or from PHP server to database like :
尝试在你的附加文件中添加静态文本到数据库(不要从请求中读取),这样你就可以将问题分成两部分,你可以看到问题是从android客户端到PHP服务器,还是从PHP服务器到数据库,如:
<?php
/*
please type the Arabic string(salam) with your own keyboard and do not
copy my version because I used Persian keyboard.
*/
$arabic_string = 'سلام';
file_put_contents("arabic_file", $arabic_string);
$query_params = array(
':news_date' => $_POST['news_date'],
':news_title' => $_POST['news_title'],
':news_body' => $arabic_string
);
?>
now check both the "arabic_file" content and also the database.
现在检查“arabic_file”内容以及数据库。
-If the file was not ok(contained gibberish) then the problem is from your php server(like maybe it does not support arabic language,...).
- 如果文件不正确(包含乱码),那么问题来自你的php服务器(也许它不支持阿拉伯语,...)。
-If the database was not ok(gibberish again) then maybe the problem is your database or the way PHP's PDO is inserting data into the database, and you should specify the encoding for your connection like :
- 如果数据库不好(再次乱码),那么问题可能是您的数据库或PHP的PDO将数据插入数据库的方式,您应该为您的连接指定编码,如:
$connection_str = "mysql:host=$host;dbname=$db;charset=utf8";
-If everything was ok then the problem is the way the Arabic strings are coming from your android device.
- 如果一切正常,那么问题就是阿拉伯字符串来自Android设备的方式。
#1
0
Hope these tips will help :
希望这些提示有所帮助:
Try adding a static text into database in your addition file (do not read it from request) so you can divide the problem into two parts and you can see if the problem is from android client to PHP server or from PHP server to database like :
尝试在你的附加文件中添加静态文本到数据库(不要从请求中读取),这样你就可以将问题分成两部分,你可以看到问题是从android客户端到PHP服务器,还是从PHP服务器到数据库,如:
<?php
/*
please type the Arabic string(salam) with your own keyboard and do not
copy my version because I used Persian keyboard.
*/
$arabic_string = 'سلام';
file_put_contents("arabic_file", $arabic_string);
$query_params = array(
':news_date' => $_POST['news_date'],
':news_title' => $_POST['news_title'],
':news_body' => $arabic_string
);
?>
now check both the "arabic_file" content and also the database.
现在检查“arabic_file”内容以及数据库。
-If the file was not ok(contained gibberish) then the problem is from your php server(like maybe it does not support arabic language,...).
- 如果文件不正确(包含乱码),那么问题来自你的php服务器(也许它不支持阿拉伯语,...)。
-If the database was not ok(gibberish again) then maybe the problem is your database or the way PHP's PDO is inserting data into the database, and you should specify the encoding for your connection like :
- 如果数据库不好(再次乱码),那么问题可能是您的数据库或PHP的PDO将数据插入数据库的方式,您应该为您的连接指定编码,如:
$connection_str = "mysql:host=$host;dbname=$db;charset=utf8";
-If everything was ok then the problem is the way the Arabic strings are coming from your android device.
- 如果一切正常,那么问题就是阿拉伯字符串来自Android设备的方式。