php - 如何将值从 android 传递到 php web 服务并检索它?

标签 php android mysql web-services

我正在尝试向我的 php 网络服务传递一个值。我已经使用此代码传递“名称”值:

 private class MyAsyncTask extends AsyncTask<String, Void, Void> {

    protected Void doInBackground (String... params)
    {
            Intent intent = getIntent();
            String name = intent.getStringExtra("KEY_NAME");
            //HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/secure_login/get_data_user.php");

            List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
            nameValuePair.add(new BasicNameValuePair("KEY_NAME", name));
            DefaultHttpClient hc = new DefaultHttpClient();
           // HttpResponse response = hc.execute(httppost);


            try {
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
            } catch (UnsupportedEncodingException e){
                // writing error to log
                e.printStackTrace();
            }

            try {
                HttpResponse response = hc.execute(httppost);
                HttpEntity entity = response.getEntity();
                InputStream inStream = entity.getContent();
                // writing response to log
                Log.d("Http Response:", response.toString());

               } catch (ClientProtocolException e) {
                   // writing exception to log
                   e.printStackTrace();
               } catch (IOException e) {
                   e.printStackTrace();
               }
            return null;



    }

还有这个,用于将 Stream 转换为 String。

          protected String convertStreamToString(InputStream inStream)
            {
               BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
               StringBuilder sb = new StringBuilder();

               String line = null;
               try 
               {
                   while ((line = reader.readLine()) != null) 
                   {
                       sb.append(line + "\n");
                   }
               } 
               catch (IOException e) 
               {
                   e.printStackTrace();
               } 
               finally 
               {
                   try 
                   {
                       inStream.close();
                   } 
                   catch (IOException e) 
                   {
                       e.printStackTrace();
                   }
               }
               return sb.toString();
            }
  }

但我只在 log cat 中得到了这个响应: org.apache.http.message.BasicHttpResponse@43e4c068

我需要传递“名称”值,以便我的 php 网络服务可以像这样检索和执行查询:

 if (isset($_GET['name'])) {
$name = $_GET['name'];

require_once 'DB_Functions.php';
$db = new DB_Functions();
$result = mysql_query("SELECT name, email from users where name = '$name'");

我该如何解决?提前谢谢你。

最佳答案

您正在 android 中发送一个 post 请求并尝试使用 $_GET 在 PHP 中检索参数。

尝试通过$_POST访问name变量:

if (isset($_POST['name']))
    $name = mysql_real_escape_string($_POST['name']);

require_once 'DB_Functions.php';
$db = new DB_Functions();
$result = mysql_query("SELECT name, email from users where name = '$name'");

重要:在使用 mysql_real_escape_string 将字符串放入 SQL 语句之前,不要忘记对字符串进行转义以避免 SQL 注入(inject)。

关于php - 如何将值从 android 传递到 php web 服务并检索它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21372276/

相关文章:

php - 按 id 列查找实体,\Phalcon\Mvc\Model::findFirst() 给出不正确的结果

java - 从自定义数组列表创建一个新列表

android - 在android中读取RAW DNG文件

php - 设置对 PHP 函数的调用,将 POST 变量作为参数,作为 HTML 表单操作

php - 使用 GCP Cloud SQL 只读副本

php - 如何解决 €25.99 vs 25.99€ preg_match 问题?

java - FTPClient 上传文件 = SocketException : Connection reset

mysql - 在第二个表中按日期排序结果

mysql - 限制数据库级别的关联记录数

mysql - Grails Shiro 插件创建只读表