gpt4 book ai didi

android - 将数据从 Android 发送到 Webservice 以插入到我的数据库中

转载 作者:搜寻专家 更新时间:2023-10-30 22:14:29 26 4
gpt4 key购买 nike

我正在尝试通过“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 的正确方法。你能告诉我这里的问题是什么吗?

最佳答案

请查看 LogCat 输出。它应该说明在哪个类的哪一行出现异常。如果您需要帮助,请在此处发布 logcat 输出。我还建议您查看 Android Asynchronous Http Client .它极大地简化了 HTTP 通信。 HTTP POST 示例:

AsyncHttpClient client = new AsyncHttpClient();
RequestParams rp = new RequestParams();
rp.put("fname", et3.getText().toString());
rp.put("lname", et4.getText().toString());
rp.put("userid", et5.getText().toString());
rp.put("pass", et6.getText().toString());

client.post("http://mywebsitename.com/webservice/register.php", rp, new AsyncHttpResponseHandler() {
@Override
public final void onSuccess(String response) {
// handle your response here
}

@Override
public void onFailure(Throwable e, String response) {
// something went wrong
}
});

关于android - 将数据从 Android 发送到 Webservice 以插入到我的数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16447647/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com