JSON字符串这是Android with php: Saving utf-8 string to MySQL安卓:HttpPost在UTF-8编码PHP
我使用GSON库在Android中的类的实例转换成JSON字符串的后续问题。然后将此JSON字符串与HttpPost一起上传到PHP脚本中,以将其保存到我的MySQL DB中。
Initialy我认为问题是用php或MySQL,但现在看来是我的HttpPost代码序列没有正确地将JSON编码为UTF-8。如果我调试我的代码并使用JSON字符串在Chrome中执行POST,那么它的工作原理是完美的。
我的MySQL DB现在格式化为UTF-8(utf8_general_ci)。
这里是我的HttpPost代码:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("questionnaire", json));
httppost.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
的问题可能是httppost.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
,但我不知道怎么回事,通知我在UTF-8格式发送JSON我的PHP脚本。
作为一个例子,“不能到达那里”被Android正确编码为“can/u0027t get there”,当JSON strig由Gson创建时。将JSON上传到PHP后,它会保存为“canu0027t get there”,而不在MySQL DB中包含“\”。
那么如何在Android中正确标记/标记/标识此JSON字符串为UTF-8格式,以便正确保存?
什么是请求和你发送的内容。请在此发布,否则您的问题含糊不清。 –