我有一个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