我不能" POST"从android服务器PHP

时间:2014-09-09 04:00:28

标签: php android http post

class CreateNewProduct extends AsyncTask<String, String, String>
    {


        @Override
        protected void onPreExecute() 
        {
            super.onPreExecute();
            pDialog = new ProgressDialog(AddNewProduct.this);
            pDialog.setMessage("Creating Product..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }


        protected String doInBackground(String... args) 
        {

            String nameproducts = inputName.getText().toString();
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("name", nameproducts));
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(http://localhost:8080/Ten_project/create_product.php);
            try {

                httppost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));

                HttpResponse response = httpclient.execute(httppost);
                finish();
                result.setText("success"); 

            }
         catch (ClientProtocolException e) 
            {
             e.printStackTrace();
            } 
         catch (IOException e)
            {
            e.printStackTrace();
            }

         return null;
        }

        protected void onPostExecute()
        {
            pDialog.dismiss();
        }
    }

/////////// PHP代码 /database.php

class database{
    var $_dbh = '';
    var $_sql = '';
    var $_cursor = NULL;        

    public function database() {
        $this->_dbh = new PDO('mysql:host=localhost; dbname=thu_nghiem','root','');
        $this->_dbh->query('set names "utf8"');
    }

    public function setQuery($sql) {
        $this->_sql = $sql;
    }

    //Function execute the query 
    public function execute($options=array()) {

            $this->_cursor = $this->_dbh->prepare($this->_sql);

        $bien =     $this->_cursor->execute($options);
        echo 'Lop database <br/>';
        var_dump($bien);
        echo '<br/>';
        var_dump($this->_cursor);

            return $this->_cursor;
    }

    //Function load datas on table
    public function loadAllRows($options=array()) {
        if(!$options) {
            if(!$result = $this->execute())
                return false;
        }
        else {
            if(!$result = $this->execute($options))
                return false;
        }
        return $result->fetchAll(PDO::FETCH_OBJ);
    }

    //Function load 1 data on the table
    public function loadRow($option=array()) {
        if(!$option) {
            if(!$result = $this->execute())
                return false;
        }
        else {
            if(!$result = $this->execute($option))
                return false;
        }
        return $result->fetch(PDO::FETCH_OBJ);
    }

    //Function count the record on the table
    public function loadRecord($option=array()) {
        if(!$option) {
            if(!$result = $this->execute())
                return false;
        }
        else {
            if(!$result = $this->execute($option))
                return false;
        }
        return $result->fetch(PDO::FETCH_COLUMN);
    }

    public function getLastId() {
        return $this->_dbh->lastInsertId();
    }

    public function disconnect() {
        $this->_dbh = NULL;
    }

}

create_product.php

if (isset($_POST['nameproducts'])) {

$name = $_POST['nameproducts'];

require_once('database.php');

$chuoi_sql = "INSERT INTO products(name) VALUES('$name')";
$pdo->setQuery($chuoi_sql) ;
$result = $pdo->execute(array('$name'));

}

我想要实现的目标:在我的开发PC上运行从Android应用程序到Apache Web服务器的简单POST请求,并显示发送表单的PHP脚本的POSTed数据。 所以,为什么我不能&#34; POST&#34;到服务器,我使用warpserver。服务器或我的Android客户端出错? 为什么HTTPCLIENT.execute()无法运行。

1 个答案:

答案 0 :(得分:2)

CreateNewProduct的第24行,

HttpPost httppost = new HttpPost(http://localhost:8080/Ten_project/create_product.php);

当您将网址作为字符串传递时,您需要将网址包装在双引号内。

其次,localhost在您的Android模拟器中无法使用。您需要使用10.0.2.2代替localhost。所以您应该使用

HttpPost httppost = new HttpPost("http://10.0.2.2:8080/Ten_project/create_product.php");

在您的logcat中,如果您设置了无效的uri,则会看到IllegalArgumentException

注意:如果10.0.2.2对您不起作用,您可以随时使用本地IP.Just不要使用localhost127.0.0.1