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

时间:2014-01-27 02:55:21

标签: php android mysql web-services

我正在尝试将值传递给我的php webservice。我已经使用此代码传递“name”值:

 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

我需要传递“name”值,以便我的php webservice可以检索并执行如下查询:

 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'");

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

1 个答案:

答案 0 :(得分:0)

您正在Android中发送帖子请求并尝试使用$_GET在PHP中检索参数。

尝试通过$_POST访问名称变量:

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注入。