我正在一個項目中,我們在客戶端有一個Android應用程序,服務器端正在使用PHP進行管理。客戶端應該向服務器發送信息,並且應該使用包含結果的json進行回覆。一切都找到了,直到我們使用一些特殊字符,我不知道如何解決這個問題。Android PHP POST口音參數失敗
我已經驗證了Apache和PHP設置,並且都使用UTF-8作爲默認字符集。
我所面臨的問題arer:
- 我的PHP代碼是不能夠通過自身來管理$ _POST的值時,它們包含特殊字符。我應該在使用它之前使用PHP中的urldecode()解碼它嗎?這些值通過URLEncoder.econde(myvalue,「UTF-8」)發送到服務器。請參閱下面的代碼。
- 我的php代碼正在返回一個json對象。如果對象中沒有特殊字符,則每個字符都可以工作,但如果它包含某些重音或類似字符,則無法管理該對象。
這裏是我的代碼
的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()檢查一些值,並比較客戶端提供的信息。如果客戶提交了一些特殊字符不能管理它。即如果我們需要比較一個提交的和一個'這個函數不起作用,如果($ _ 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);
}
}
嗨Gamal,我想嘗試一下,但請你能澄清一下「連接區域的PHP代碼,將排序規則設置爲UTF8」 – Javier