2013-02-12 47 views
1

我存儲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 
+0

可能是這個[鏈接](http://stackoverflow.com/questions/14555208/how-to-send-and-retrieve-data-from-web-service-using-php-and-android)將幫助你 – 2013-02-12 05:17:42

回答

1

嘗試刪除"UTF-8"形式

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8")); 

所以應該像httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

因爲你已經在使用UrlEncodedFormEntity所以沒有必要。

-1

如果設置json_encode的參數正確像

$output=json_encode($output,JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE); 

在編碼值和數據傳輸沒有問題,但如果不行再.SET

header('Content-Type: application/json; charset=utf8'); 

可以使用進行urlencode用於編碼UTF-8的價值和解碼它在Android

這樣

urlencode('aیgfسبd'); 

輸出

a%DB%8Cgf%D8%B3%D8%A8d 

與此輸出你沒有json值問題。

+0

穆罕默德我必須在PHP中使用上面的代碼$ str吧? – Ghouse 2013-02-12 05:19:59

+0

@AnswerMe不,它是樣品,你必須把$輸出。:) – 2013-02-12 05:21:46

+0

好吧,讓我檢查 – Ghouse 2013-02-12 05:22:32