将数据从Android发送到Webservice以插入我的数据库
我试图通过“httppost”方法将一些变量(或数据)从android发送到web服务进行注册。这是密码将数据从Android发送到Webservice以插入我的数据库,android,database,web-services,http-post,Android,Database,Web Services,Http Post,我试图通过“httppost”方法将一些变量(或数据)从android发送到web服务进行注册。这是密码 HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://mywebsitename.com/webservice/register.php"); try { List<NameValuePair> nameValuePairs = new ArrayLis
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
}
HttpClient-HttpClient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(“http://mywebsitename.com/webservice/register.php");
试一试{
List nameValuePairs=新的ArrayList(4);
添加(新的BasicNameValuePair(“fname”,et3.getText().toString());
添加(新的BasicNameValuePair(“lname”,et4.getText().toString());
添加(新的BasicNameValuePair(“userid”,et5.getText().toString());
添加(新的BasicNameValuePair(“pass”,et6.getText().toString());
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
下面是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());
}
请查看LogCat输出。它应该说明在哪个类的哪一行发生了异常。如果需要帮助,请在此处发布logcat输出。我还建议你看一下。它大大简化了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
}
});
在logcat中,它应该为您提供详细的错误消息和堆栈跟踪。看那个,或者把它贴在这里。仅供参考-Android的规则之一是不能在主线程上使用网络IO。如果您没有在异步任务或线程中调用此函数,这可能是您的问题。请尝试此操作,在此处选择“后期服务”并传递参数,以便了解问题否我没有在异步任务或线程中调用它。我该怎么做?我如何在这里粘贴日志?很抱歉,我对这里的内容有点陌生。请阅读有关AsyncTask的文档,非常清楚,并提供了一个不错的示例。或者按照我的回答中的建议进行操作,因为AndroidSyncHttpClient执行应用程序主UI线程之外的所有请求,但是任何回调逻辑都将在创建回调时的同一线程上执行。对于您的代码,现在没有错误。但是我认为我的php代码有一些问题。好吧,那就不同了。您可以使用$\u POST或$\u REQUEST在PHP端获取POST数据吗?很抱歉回复晚了。实际上,我正忙于我的国家正在进行的选举。不管怎样,你能告诉我如何检查我的php代码是否正常工作吗?它是否接收值?如果是,是否将值发送到数据库?我如何检查呢?你能从网页中调用PHP代码并让它工作吗?或者尝试一个浏览器插件,比如Poster(,也可用于Firefox)。你的回答很有帮助,其余的我自己也解决了:)。访问代码时出现了一些打字错误。谢谢