当前位置:Gxlcms > mysql > android虚拟机在xampp的集成环境下,向mysql插入id,name数据。

android虚拟机在xampp的集成环境下,向mysql插入id,name数据。

时间:2021-07-01 10:21:17 帮助过:29人阅读

虚拟机xampp数据androidmysql

客户端界面如下:图片说明

info1.php代码

$dbuser = "root"; //我的用户名$dbpass = "123"; //我的密码$dbname = "php_test"; //我的mysql库名$cn = mysql_connect("127.0.0.1", $dbuser, $dbpass) or die("connect error");mysql_query("set names UTF8");mysql_select_db($dbname)or die("db error");$id=$_POST['id'];$name=$_POST['name'];//接收客户端发来的username;$sql =  "INSERT INTO info1 (id, name) VALUES ('$id', '$name')";mysql_query($sql);                

?>

android客户端代码

public class MainActivity extends Activity {
EditText id;
EditText name;
Button ok;
InputStream is = null;

private static String url = "http://192.168.56.1/PHP/info1.php";@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    id = (EditText)findViewById(R.id.id);    name = (EditText)findViewById(R.id.name);    ok = (Button)findViewById(R.id.ok);    ok.setOnClickListener(new View.OnClickListener() {                  @Override        public void onClick(View v) {            // TODO 自动生成的方法存fajfa                               Log.v("break", "进入try过程");                try                {                    String text1 = id.getText().toString();                    String text2 = name.getText().toString();                    ArrayList para = new ArrayList();                    para.add(new BasicNameValuePair("id", text1));                    para.add(new BasicNameValuePair("name", text2));                    HttpClient httpClient = new DefaultHttpClient();                    HttpPost httpPost = new HttpPost(url);                    httpPost.setEntity(new UrlEncodedFormEntity(para));                    httpClient.execute(httpPost);                }catch(Exception e)                {                    e.printStackTrace();                }        }    });}

}
我尝试把httppost的参数改为很多方式。。
比如 private static String url = "http://localhost/PHP/info1.php";
private static String url = "http://127.0.0.1/PHP/info1.php";
private static String url = "http://10.0.2.15/PHP/info1.php";
都不能把数据给插入成功。。
我不知道是url 设置错了。还是其他哪里错了。如果您知道,请大家告诉小弟一下。

人气教程排行