我有一個Android java方法,它調用遠程php來將數據插入到mysql表中。用java和php插入mysql:沒有錯誤,但沒有插入
這是Java代碼客戶端:
public static void insert(String email1, String email2) {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email1", email1));
nameValuePairs.add(new BasicNameValuePair("email2", email2));
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("myserver:8080/insert.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//How to catch and print php echo() here??
}
,這是insert.php服務器端(重新編輯):
<?php
$mysqli_connection = new mysqli("localhost:3306", "root", "password", "mydb");
if ($mysqli_connection->connect_errno) {
echo ("Connection Failure");
exit();
}
$stmt = $mysqli->stmt_init();
if ($stmt->prepare("INSERT INTO `mytable` (`email1`, `email2`) VALUES (?,?)")) {
echo 'Success';
$stmt->bind_param("ss", $_GET['email1'], $_GET['email2']);
$stmt->execute();
$stmt->close();
} else {
echo 'Error Inserting Content';
}
$mysqli->close();
?>
當我打電話給我java insert()方法,沒有異常返回,但沒有插入完成和mytable爲空。
- 1)出了什麼問題?
- 2)如何查看發生了哪個錯誤?我怎樣才能看到php端執行的「日誌」?
非常感謝。 Geltry
我讀HttpResponse/HttpEntity沒有成功。你能告訴我怎麼樣?例如:httpEntity.getMessage()或類似的...謝謝 – Geltrude
你是什麼意思,沒有成功?另請參閱修改後的答案。 –
你的建議是對的(我在php.ini中沒有mysqli庫) – Geltrude