Php 用android与web数据库通信
我在android应用程序中有一个注册表单,其中包括用户名和密码。我希望当用户单击注册表单的提交按钮时,用户名和密码将保存到web数据库(mySql数据库)中 当用户按下提交按钮,然后Php 用android与web数据库通信,php,android,http-post,webservice-client,Php,Android,Http Post,Webservice Client,我在android应用程序中有一个注册表单,其中包括用户名和密码。我希望当用户单击注册表单的提交按钮时,用户名和密码将保存到web数据库(mySql数据库)中 当用户按下提交按钮,然后sendPostRequest(givenUsername,givenPassword)被执行 sendPostRequest()函数是这样的 private void sendPostRequest(String givenUsername, String givenPassword) { class
sendPostRequest(givenUsername,givenPassword)代码>被执行
sendPostRequest()函数是这样的
private void sendPostRequest(String givenUsername, String givenPassword) {
class SendPostReqAsyncTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
String paramUsername = params[0];
String paramPassword = params[1];
System.out.println("*** doInBackground ** paramUsername " + paramUsername + " paramPassword :" + paramPassword);
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2/imon.php");
BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair("paramUsername", paramUsername);
BasicNameValuePair passwordBasicNameValuePAir = new BasicNameValuePair("paramPassword", paramPassword);
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
nameValuePairList.add(usernameBasicNameValuePair);
nameValuePairList.add(passwordBasicNameValuePAir);
try {
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
httpPost.setEntity(urlEncodedFormEntity);
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
InputStream inputStream = httpResponse.getEntity().getContent();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String bufferedStrChunk = null;
while((bufferedStrChunk = bufferedReader.readLine()) != null){
stringBuilder.append(bufferedStrChunk);
}
return stringBuilder.toString();
} catch (ClientProtocolException cpe) {
System.out.println("First Exception caz of HttpResponese :" + cpe);
cpe.printStackTrace();
} catch (IOException ioe) {
System.out.println("Second Exception caz of HttpResponse :" + ioe);
ioe.printStackTrace();
}
} catch (UnsupportedEncodingException uee) {
System.out.println("An Exception given because of UrlEncodedFormEntity argument :" + uee);
uee.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result.equals("working")){
Toast.makeText(getApplicationContext(), "HTTP POST is working...", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Invalid POST req...", Toast.LENGTH_LONG).show();
}
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(givenUsername, givenPassword);
}
private void sendPostRequest(字符串givenUsername,字符串givenPassword){
类SendPostReqAsyncTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串paramUsername=params[0];
字符串paramPassword=params[1];
System.out.println(“***doInBackground**paramUsername”+paramUsername+“paramPassword:+paramPassword”);
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://10.0.2.2/imon.php");
BasicNameValuePair用户名BasicNameValuePair=新的BasicNameValuePair(“paramUsername”,paramUsername);
BasicNameValuePair passwordBasicNameValuePAir=新的BasicNameValuePair(“paramPassword”,paramPassword);
List nameValuePairList=新的ArrayList();
nameValuePairList.add(usernameBasicNameValuePair);
nameValuePairList.add(passwordBasicNameValuePAir);
试一试{
UrlEncodedFormEntity UrlEncodedFormEntity=新的UrlEncodedFormEntity(nameValuePairList);
setEntity(urlEncodedFormEntity);
试一试{
HttpResponse HttpResponse=httpClient.execute(httpPost);
InputStream InputStream=httpResponse.getEntity().getContent();
InputStreamReader InputStreamReader=新的InputStreamReader(inputStream);
BufferedReader BufferedReader=新的BufferedReader(inputStreamReader);
StringBuilder StringBuilder=新的StringBuilder();
字符串bufferedStrChunk=null;
while((bufferedStrChunk=bufferedReader.readLine())!=null){
追加(bufferedStrChunk);
}
返回stringBuilder.toString();
}捕获(客户端协议异常cpe){
System.out.println(“HTTPResponse的第一个例外caz:+cpe”);
printStackTrace();
}捕获(ioe异常ioe){
System.out.println(“HttpResponse的第二个异常caz:+ioe”);
ioe.printStackTrace();
}
}捕获(不支持的编码异常uee){
System.out.println(“由于UrlEncodedFormEntity参数:“+uee”而给出的异常);
uee.printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
如果(结果等于(“工作”)){
Toast.makeText(getApplicationContext(),“HTTP POST正在工作…”,Toast.LENGTH\u LONG.show();
}否则{
Toast.makeText(getApplicationContext(),“无效的POST请求…”,Toast.LENGTH_LONG.show();
}
}
}
SendPostReqAsyncTask SendPostReqAsyncTask=新建SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(givenUsername,givenPassword);
}
我的imon.php是这样的:
<?php
$varUsername = $_POST['paramUsername'];
$varPassword = $_POST['paramPassword'];
if($varUsername != "" && $varPassword != ""){
$con = mysql_connect('localhost', 'root', '');
if (!$con) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('post_db', $con);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
$sql="INSERT INTO post_table (username,password)
VALUES
('$varUsername ','$varPassword')";
if (!mysql_query($con,$sql))
{
die('Error: ' . mysql_error($con));
}
echo 'working';
}else
{
echo 'invalid';
}
?>
你不能这样做
mysql_connect('localhost', 'root', '');
然后再这样做
if (!mysqli_query($con,$sql))
mysql和mysqli是独立的扩展。您不能相互共享资源
mysqli_query()和mysql_query()的参数顺序不同。mysql_query()的第一个参数是查询。其中,在mysqli_查询中,第一个参数是连接资源。您之前也在混合连接。我会尝试用$\u GET[]替换$\u POST[]变量,并在浏览器中手动尝试调试它。从代码的外观来看,它可能由于任何原因而失败。i、 php?paramUsername=someone¶mPassword=something-然后查看输出是什么。如果它能工作,那么你就知道它不是php。顺便说一句,PHP在我看来还不错。我纠正了这个问题,好像(!mysql\u query($con,$sql))但是@devzero仍然不起作用。你不能将它纠正为mysql\u query($con,$sql))
。只要做一下mysql查询($sql)
就行了,但我还是不明白我的错在哪里。你能详细解释一下吗。