我存儲unicode在MySQL數據庫,並試圖檢索它在我的Android應用程序中使用PHP。每當我嘗試檢索只unicode即將到來不是utf-8 characters.I在http中指定編碼爲utf-8,我在網上搜索了很多解決方案,但仍未找到答案。我也使用過這個,但仍然沒有運氣。UNICODE UTF-8問題,同時檢索從服務器在android
new String(json_data.getString("message").getBytes(""),"UTF-8")
下面我張貼我的代碼.ANY建議是welcome.my要求是獲得印地文,烏爾都語或服務器的任何語言,我需要什麼display.so我做錯了。 並從數據庫中我得到這個\ 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
可能是這個[鏈接](http://stackoverflow.com/questions/14555208/how-to-send-and-retrieve-data-from-web-service-using-php-and-android)將幫助你 – 2013-02-12 05:17:42