我正在一个项目中工作,我们在客户端有一个Android应用程序,服务器端是用PHP管理的。客户端应该向服务器发送信息,它应该回复一个包含结果的json。一切都找到,直到我们使用一些特殊字符,我不知道如何解决这个问题。
我已经验证了Apache和PHP设置,并且两者都使用UTF-8作为默认字符集。
我面临的问题是:
这是我的代码
Android方
public class HelperClassJSONDownload {
public HelperClassJSONDownload(){
}
// Given a URL, establishes an HttpUrlConnection and retrieves
// the web page content as a InputStream, which it returns as
// a string.
public JSONObject downloadJSON (String myurl,List<NameValuePair> params) {
InputStream inputStream = null;
JSONObject jsonObject = null;
String json = "";
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);//We need it to set parameters
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
//Writer w=new OutputStreamWriter(
// conn.getOutputStream(), "UTF-8");
//w.write(getQuery(params).toString());
//w.flush();
//w.close();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
//END SETTING PARAMETERS REQUEST
// Starts the query
conn.connect();
int response = conn.getResponseCode();
switch (response){
case 200:
case 201:
inputStream = conn.getInputStream();
BufferedReader reader = new BufferedReader
(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
json = sb.toString();
jsonObject = new JSONObject(json);
return jsonObject;
default:
return null;
}
} catch(IOException e){
return null;
} catch(JSONException e1){
return null;
}finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
/**
* This method create the String we will pass with the parameters
* @param params
* @return the string used for for the parameters
* @throws UnsupportedEncodingException
*/
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params)
{
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}
}
这是PHP代码,函数generalCheckUserData()检查一些值并比较客户端提供的信息。如果客户端提交了一些特殊字符则无法管理它。即如果我们需要比较提交的á和'a'这个函数不起作用($ _ POST ['values'] ==á) 此外,如果返回的json包含一些特殊字符,则它不起作用。
我希望这些信息足够
public function __construct() {
$this->response = array("tag" => $this->tag, "success" => 0, "error" => 0);
if ($this->generalCheckUserData() && (isset($_POST['tag']) && $_POST['tag'] == 'register')){
$this->registerNewUser($_POST['user_email'], $_POST['user_password_new'], $_POST['user_password_repeat'],
$_POST['user_first_name'], $_POST['user_last_name_1'], $_POST['user_last_name_2'], $_POST['user_telephone']
,$_POST['user_city'], $_POST['user_province'], $_POST['user_postal_code'], $_POST['user_country']);
} else {
//we create an array where to storage the information returned to the client
$this->response['error'] = 1;
$this->response['error_type'] = 1;
$this->response["error_msg"] = $this->mes;
echo json_encode($this->response);
}
}
答案 0 :(得分:0)
我不知道这会有什么帮助,但根据我的一般知识,如何添加此连接属性Accept-Encoding并将其设置为utf8,并在连接区域的PHP代码中,将collation设置为UTF8
答案 1 :(得分:0)
最后我发现问题出在Zend Studio设置中。请查看此Encoding in Eclipse表单了解更多详情。