我尝试了不同的解决方案,这些解决方案已经在这里回答了类似的问题,但它不起作用。帮助我在下面的这些代码中找到错误。
安卓端
Button b2= (Button) findViewById(R.id.buttons3);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//String test="hello";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.2.1/prosample.php");
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("test", "hello"));
try {
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Execute and get the response.
HttpResponse response;
try {
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
//php
<?php
$conn = mysql_connect("localhost","root","");
if(!$conn) die("unable to connect to MYSQL:".mysql_error());
else echo "connected to the server" ;
mysql_select_db("proj",$conn);// or die("Unable to select database",mysql_error());
$hello = $_POST['test'];
echo $hello;
$query = "INSERT into pro(colmn) values ('$hello')";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
else echo "AWESOME!!!!";
?>
我发现,这里的“测试”中没有收到传递的参数。. ,因为我在android中输入并通过'test'传递的字符串“hello”没有被插入到db“proj”中的表pro中。我该如何解决这个问题?提前致谢。