我正在一个项目中工作,我们在客户端有一个android应用程序,服务器端正在用PHP管理。客户端应该发送信息到服务器,它应该用包含结果的json来回复。在我们使用一些特殊字符之前,一切都很容易找到,我不知道如何解决这个问题。
我已经验证了Apache和PHP的设置,两者都使用UTF-8作为默认字符集。
我面临的问题越来越多:
- 我的PHP代码不能自己管理$_POST中的值,当它们包含特殊字符时。在使用它之前,我应该在PHP中使用urldecode()进行解码吗?这些值通过URLEncoder.econde(myvalue,"UTF-8")发送到服务器。参见下面的代码: 我的php代码返回一个json对象。如果对象中没有特殊字符,则一切正常,但如果它包含一些重音或类似的字符,则无法管理该对象。
我的代码
安卓方面
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()检查一些值并比较客户端提供的信息。如果客户端提交了一些特殊字符是无法管理的。例如,如果我们需要比较一个
我希望这些信息足够了
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);
}
}
我不确定这在哪里有帮助,但从我的一般知识如何添加这个连接属性Accept-Encoding并将其设置为utf8,并在连接区域的PHP代码中,将排序设置为utf8
最后我发现问题是在Zend Studio设置。