我有一个变音符号的问题。
当我尝试在网络上手动添加用户时,所有内容都完美地保存在数据库中(还包括ě,š,č,ř,ž,ý,á,í,é等字符),但是当我保存String时
String diacritic = "ěščřřžžýáíé";
并将其传递给NameValuePair参数中的getJSONFromUrl,它将这样保存到数据库中:
?????
我真的不知道该怎么做
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
HttpParams arg = new BasicHttpParams();
HttpProtocolParams.setVersion(arg, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(arg, "utf-8");
// defaultHttpClient
HttpClient httpClient = new DefaultHttpClient(arg);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String jsonText = EntityUtils.toString(httpEntity, HTTP.UTF_8);
try {
jObj = new JSONObject(jsonText);
} catch (JSONException e) {
Log.w("JSON Parser", "Error parsing data " + e.toString());
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jObj;
}
像这样保存:
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", "fb_register"));
params.add(new BasicNameValuePair("diacritic", "ěščřžýáíé"));
JSONObject json = jsonParser.getJSONFromUrl(URL, params);
也试过了
Charset.forName("UTF-8").encode(diacritic);
params.add(new BasicNameValuePair("diacritic", diacritic));
解决了 !
// defaultHttpClient
HttpClient httpClient = new DefaultHttpClient(arg);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8")); //add encoding
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句