使用android与web数据库进行通信


communicate with web database with android

我在安卓应用程序中有一个注册表单,其中包含用户名和密码。我希望当用户单击注册表单的提交按钮时,用户名和密码将保存到web数据库(mySql数据库)中。

当用户按下提交按钮时,则执行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);     
}

我的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';
       }
   ?>

但是数据并没有插入到我的sql数据库中。

我怎么了??我应该怎么做才能将注册表格的数据插入到web数据库中??

你不能做这个

mysql_connect('localhost', 'root', '');

然后做这个

if (!mysqli_query($con,$sql))

mysql和mysqli是独立的扩展。您不能将一个资源与另一个资源共享。

mysqli_query()和mysql_query()不采用相同的参数顺序。mysqlquery()的第一个参数是查询。其中,在mysqli_query中,第一个参数是连接资源。你早些时候也把关系搞混了。