我正在构建一个 Android 应用程序,我需要通过 Json 将希伯来语(UTF-8?)字母传递给服务器,而这些字母显示为乱码。试图在网上寻找信息,但没有运气。怎么可能做到这一点?
问问题
357 次
1 回答
0
in the JSONParser.java class:
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
Log.d("params in json",String.valueOf(params));
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is 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 parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
I had the same problem, found the solution, hope it can still help. in the line:
httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
I didn't have the utf-8
defined.
I used post method, if you use get method then the line is:
String paramString = URLEncodedUtils.format(params, "utf-8");
于 2013-11-26T23:02:18.103 回答