我试图通过我的Android应用程序将数据插入到mysql数据库中。 Httppost每次都不返回null。每当应用程序执行时,httppost都会为null。 它转到异常无效的IP地址。但是在浏览器中搜索时,网址工作正常。任何帮助,将不胜感激。 这是异步任务
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id",userid));
nameValuePairs.add(new BasicNameValuePair("pswd",pswd));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.example.com/work/examplefolder/");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
这是我的PHP代码
$host='127.0.0.1';
$uname='root';
$pwd='';
$db="example";
$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");
//$id=$_REQUEST['id'];
//$name=$_REQUEST['name'];
$id=$_REQUEST['id'];
$pswd=$_REQUEST['pswd'];
$flag['code']=0;
if($r=mysql_query("insert into user(username,password) values('$id','$pswd')",$con))
{
$flag['code']=1;
}
print(json_encode($flag));
mysql_close($con);