客户端界面如下:
info1.php代码
<?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";
@Override
protected 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<NameValuePair> para = new ArrayList<NameValuePair>();
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 设置错了。还是其他哪里错了。如果您知道,请大家告诉小弟一下。