Java 在JSON代码中使用希伯来语?
我使用这段代码从MySQL数据库发布和获取数据。 但是,当获取非英语数据时,它会显示为问号? 我做了哪些更改来启用希伯来语的使用Java 在JSON代码中使用希伯来语?,java,mysql,json,character-encoding,hebrew,Java,Mysql,Json,Character Encoding,Hebrew,我使用这段代码从MySQL数据库发布和获取数据。 但是,当获取非英语数据时,它会显示为问号? 我做了哪些更改来启用希伯来语的使用 public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; public JSONParser() { } public JSONObject makeHttpRequest(String url,
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;
}
公共类JSONParser{
静态InputStream为空;
静态JSONObject jObj=null;
静态字符串json=“”;
公共JSONParser(){
}
公共JSONObject makeHttpRequest(字符串url、字符串方法、列表参数){
试一试{
如果(方法==“POST”){
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}else if(方法==“GET”){
DefaultHttpClient httpClient=新的DefaultHttpClient();
String paramString=URLEncodedUtils.format(params,“utf-8”);
url+=“?”+参数字符串;
HttpGet HttpGet=新的HttpGet(url);
HttpResponse HttpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
json=sb.toString();
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
试一试{
jObj=新的JSONObject(json);
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
返回jObj;
}
}确保您的HTTP响应以Unicode编码(例如UTF-8),并且您的客户端(使用该服务的应用程序)必须知道该编码才能读取您的响应。永远不要使用
方法==“POST”
,始终使用方法.equals(“POST”)
进行字符串比较。。。。新的InputStreamReader(IS,“ISO-859-1”)这里不使用Unicode编码,但LaTIN -1不支持希伯来字符。应该考虑使用比ORG更坚实的API。JSON的-杰克逊的StaseSkiWi,谢谢你所说的…托马斯,那么我应该用什么来支持希伯来?嗯,JSON需要UTF-8或UTF-{16,32}。并非所有的Unicode编码都受支持……这意味着接受字符串作为输入而不是纯字节流的库有点崩溃。库应该发现字节流是否包含UTF-8、UTF-16或UTF-32大端或小端,并处理其中的任何一个。