【问题标题】:communicate with web database with android使用android与web数据库通信
【发布时间】:2013-07-12 08:19:04
【问题描述】:

我在一个包含用户名和密码的 Android 应用程序中有一个注册表单。我希望当用户单击注册表单的提交按钮时,用户名和密码将被保存到网络数据库(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';
       }
   ?>

但是数据没有插入到my-sql数据库中。

我在这里错了什么? 我应该怎么做才能将注册表单的数据插入网络数据库??

【问题讨论】:

  • 我会尝试用 $_GET[] 替换 $_POST[] 变量,然后在浏览器中手动尝试调试它。从您的代码的外观来看,它可能因任何原因而失败。即 imon.php?paramUsername=someone&paramPassword=something - 然后看看输出是什么。如果它有效,那么你知道它不是 php。顺便说一句,PHP 对我来说看起来不错。

标签: php android http-post webservice-client


【解决方案1】:

你不能这样做

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

然后这样做

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

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

mysqli_query() 和 mysql_query() 不采用相同的参数顺序。 mysql_query() 的第一个参数是查询。其中 mysqli_query 第一个参数是连接资源。您之前还混合了连接。

【讨论】:

  • 我纠正了这个问题,好像 (!mysql_query($con,$sql)) 但仍然没有工作@DevZero
  • 您无法将其更正为mysql_query($con,$sql))。就做mysql_query($sql)
  • 它有效,但我仍然不明白我的错误。你能详细解释一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-09
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多