2014-02-20 22 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