嗨,我正在使用真实设备将数据插入db中,当我从浏览器测试本地服务器与我的设备之间的连接时,它可以成功运行,但是在应用程序中,它给了我无效的ip地址toast消息,我的android代码连接到数据库:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.43.210:3306/cfa/insert.php");
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();
}
try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONObject json_data = new JSONObject(result);
code=(json_data.getInt("code"));
if(code==1)
{
Toast.makeText(getBaseContext(), "Inserted Successfully",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "Sorry, Try Again",
Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
}
public void cancel(View view) {
Intent i = new Intent(this , SignUpAs.class);
startActivity(i);
finish();
}
}
和php代码:
<?php
$host='localhost';
$uname='root';
$pwd='';
$db="cfa";
$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");
$fullName=$_REQUEST['fullName'];
$userName=$_REQUEST['userName'];
$IDNum=$_REQUEST['IDNum'];
$email=$_REQUEST['email'];
$address=$_REQUEST['address'];
$password=$_REQUEST['password='];
$phone=$_REQUEST['phone'];
$flag['code']=0;
if($r=mysql_query("insert into applicant values(NULL,'$fullName',
'$userName','$IDNum','$phone','$email','$address','$password') ",$con))
{
$flag['code']=1;
echo"hi";
}
print(json_encode($flag));
mysql_close($con);
?>
所以我不知道这是什么问题...