代碼的Android設備上是:我需要做的就是發佈3個值! Android版PHP服務器腳本
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
代碼的PHP服務器是:
$name = $_POST['name'];
$email= $_POST['email'];
$password = $_POST['password'];
$user = $db->storeUser($name, $email, $password);
if($user){
echo json_encode($response). "User Stored Successfully";
}
else{
echo json_encode($response). "Error Occured in Registration";
}
其中storeUser是:
public function storeUser($name, $email, $password) {
$result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email','$encrypted_password', '$salt', NOW())");
}
其工作過程,發回消息給android設備,但是細節不是插入到MySQL數據庫中的 ?
我想要的只是從Android設備發佈詳細信息,並將它們插入我的服務器上的MySQL數據庫。
任何人都知道這是爲什麼造成的,我做錯了什麼?
**警告**您的代碼易受sql注入攻擊。 –
教程如何防止? – cwiggo