我将 unicode 存储在 mysql 数据库中,并尝试在我的 android 应用程序中使用 php 检索它。每当我尝试仅检索 unicode 而不是 utf-8 字符时。我在 http 中将编码指定为 utf-8 并且我在网上搜索了很多解决方案,但仍然没有找到答案。我也用过这个,但还是没有运气。
new String(json_data.getString("message").getBytes(""),"UTF-8")
下面我发布我的代码。欢迎提出任何建议。我的要求是从服务器获取印地语、乌尔都语或任何语言,我需要显示。所以我做错了什么。从数据库我得到这个 \u0cb9\u0ccc \u0c85\u0cb0\u0cc6 \u0caf\u0cc1
private class BackgroundTask extends AsyncTask <String, Void, String> {
@Override
protected String doInBackground(String... url) {
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
String prefName = "MyPref";
SharedPreferences prefs=getSharedPreferences(prefName,MODE_PRIVATE);
nameValuePairs.add(new BasicNameValuePair("username", mname.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("password", mpassword.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("regid", regid));
nameValuePairs.add(new BasicNameValuePair("status_id", "1"));
nameValuePairs.add(new BasicNameValuePair("clienttype_id", "clienttype_id"));
try {
HttpClient httpclient = new DefaultHttpClient();
Resources resources=getResources();
String urlstr=String.format(resources.getString(R.string.androidexternal));
HttpPost httppost = new HttpPost(urlstr);// for test db
// Add your data
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
inputStream = response.getEntity().getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{//iso-8859-1,UTF-8
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"),8000);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
inputStream.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
if(result.contains("null")){
}
else
{
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
if(i>=3){
System.out.println( new String(json_data.getString("message").getBytes(),"UTF-8"));
}
}
startActivity(intent);
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return result.toString();
}
和服务器端代码是
// select query for login layout if(isset($_REQUEST['username']))
{
$get_message_sql = mysql_query("SELECT `message`,`language` FROM doctor_custom_message WHERE `client_id`='".$result['id']."' AND status='1' ");
$number=mysql_num_rows( $get_message_sql);
while($e=mysql_fetch_assoc($get_message_sql))
if($number==0)
{
$output[]="null";
}else{
$output[]=$e;
}
//header('content-type: text/plain; charset=utf-8');
print(json_encode($output));
}// main brace closed