我正在尝试通过“httppost”方法从android发送一些变量(或数据)以注册到Web服务。这是代码。
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mywebsitename.com/webservice/register.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("fname", et3.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("lname", et4.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("userid", et5.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("pass", et6.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
以下是 Register.php 文件代码。
<?php
$host = "localhost";
$user = "myusername";
$pass = "mypass";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$userid = $_POST['userid'];
$pass = $_POST['pass'];
$db_select=mysql_select_db("the_db");
if(!$db_select){
die(mysql_error());
echo "error";
}
$query= "INSERT INTO USERS(fname, lname, userid, pass)
VALUES ('{$fname}', '{$lname}', '{$userid}', '{$pass}'); " ;
if($medo=mysql_query($query)){
header("localhost/filename");
exit;
}else{
echo"<p> Error</p>";
die(mysql_error());
}
当我尝试运行它时,给我一个错误。它在我的设备上显示“YourApp 已停止工作”。可能是空指针异常或类似的东西。我不知道这是否是将简单变量(如名称和密码等)发送到 register.php 的正确方法。你能告诉我这里有什么问题吗?