我在从android应用程序中向数据库提交数据时遇到问题,我在网站上引用了一个.php文件,我们将使用该文件:
public static final String SERVER_ADDRESS = "http://app.basicshosting.co.uk/";
然后我将从其他活动中获得的变量绑定到通过POST发送的变量:
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("name", user.name));
dataToSend.add(new BasicNameValuePair("username", user.username));
dataToSend.add(new BasicNameValuePair("password", user.password));
dataToSend.add(new BasicNameValuePair("age", user.age + ""));
最后,我将变量发送到我编写的.php脚本:
HttpPost post = new HttpPost(SERVER_ADDRESS
+ "Register.php");
try {
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
} catch (Exception e) {
e.printStackTrace();
}
PHP源代码位于:http://pastebin.com/JRCCk20X
感谢
您应该尝试使用线程或AsyncTask。示例-
public void updatedatabasethread(){
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
yourmethod();
}
}.start();
}
在一个方法()下创建httppost,并用这个线程调用该方法()。
调用Thread而不是直接使用方法。