2014-02-20 52 views
1

我正在使用此代碼POST和從MySQL數據庫中獲取數據。 但是,當獲取不是英文的數據時,它會顯示爲問號? 爲了啓用希伯來語,我做了哪些改變?在JSON代碼上使用希伯來語?

public class JSONParser { 

static InputStream is = null; 
static JSONObject jObj = null; 
static String json = ""; 

public JSONParser() { 

} 

public JSONObject makeHttpRequest(String url, String method,List<NameValuePair> params) { 
    try { 

     if(method == "POST"){ 

      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      httpPost.setEntity(new UrlEncodedFormEntity(params)); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 

     }else if(method == "GET"){ 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      String paramString = URLEncodedUtils.format(params, "utf-8"); 
      url += "?" + paramString; 
      HttpGet httpGet = new HttpGet(url); 

      HttpResponse httpResponse = httpClient.execute(httpGet); 
      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, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    return jObj; 

} 

}

+2

不要使用'method =='POST'',總是使用'method.equals(「POST」)'進行字符串比較。 – skiwi

+1

'...新的InputStreamReader(是,「iso-8859-1」)'你在這裏沒有使用unicode編碼,但拉丁-1不支持希伯來字符。 – Thomas

+0

你應該考慮使用更堅實的API比org.json的 - 傑克遜例如 – fge

回答

1

確保您的HTTP響應再以Unicode(UTF-8爲例)進行編碼,並且也是你的客戶端(應用程序消耗的服務)必須認識到,該編碼的閱讀你的迴應。

+0

那麼,JSON需要UTF-8或UTF- {16,32}。不支持_all_ Unicode編碼。 – fge

+0

...這意味着一個庫接受一個字符串作爲輸入,而不是一個純粹的字節流是一種破碎。該庫應該發現字節流是否包含UTF-8,UTF-16或UTF-32大小寫字母並處理。 – gnasher729