Android php连接无法正常工作

时间:2016-01-17 10:59:37

标签: android mysql

ANDROID CODE

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lpu.delight.lovelysweets.R;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;

public class AddNewItem extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_add_new_item);
        EditText proname = (EditText)findViewById(R.id.adduprice);
        EditText  unitprice = (EditText) findViewById(R.id.addpnameet);

        final String PName = proname.getText().toString();
        final String UPrice = unitprice.getText().toString();
        Button btninsert = (Button)findViewById(R.id.addbtninsert);

        btninsert.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://10.0.2.2/insert.php");

                try
                {
                    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("pname",PName));
                    nameValuePairs.add(new BasicNameValuePair("uprice", UPrice));
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httpPost);
                    Toast.makeText(getApplicationContext(),
                            "Inserted", Toast.LENGTH_LONG)
                            .show();

                }
                catch (Exception e)
                {
                e.printStackTrace();
                }
            }
        });

    }
}

INSERT.PHP CODE

<?php

$username = "root";
$password = "toor";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";


//select a database to work with
$selected = mysql_select_db("android_api",$dbhandle) 
  or die("Could not select examples");


//retrieve the data
$pnm = $_POST['pname'];
$pp = $_POST['uprice'];


mysql_query ("INSERT INTO tblproduct (pname,uprice) VALUES('$pnm', '$pp', NOW())");

mysql_close($dbhandle);

?>

没有工作没有发生请帮助

因为我正在运行它不打印toast消息。请帮助我在php文件中有什么错误,变量是pname和uprice,但仍然没有执行try块。

2 个答案:

答案 0 :(得分:0)

在您的查询中,您遗漏了INTO

中的一个参数
mysql_query ("INSERT INTO tblproduct (pname,uprice) VALUES('$pnm', '$pp')") or die(mysql_error());

; //值是3个参数,插入表中有两列

答案 1 :(得分:0)

您的SQL查询中存在问题,INTO中缺少一个参数...您尝试设置3个值并且只定义了2个表列:

INSERT INTO tblproduct (pname,uprice,MISSING_PARAM) VALUES('$pnm', '$pp', NOW())

另外,为什么不以最简单的方式查看okHTTP库来处理HTTP连接: http://square.github.io/okhttp/