gpt4 book ai didi

php - 无法通过php使用android模拟器将数据插入mysql

转载 作者:行者123 更新时间:2023-11-29 23:56:06 25 4
gpt4 key购买 nike

实际上,我是 android 新手,尝试通过 php 页面使用 android 将记录插入到 mysql 表中。我正在使用 wamp 服务器,但是。我使用 IP 地址 10.0.2.2 而不是 127.0.0.1,我还使用 IP 地址 10.0.0.2:8676 作为我的端口号,但它们都不起作用,我总是发现这是错误 IP 地址无效。我无法理解我错在哪里。尽管此代码正在实时服务器主机上运行,​​并且值正在插入到实时服务器中,但不在本地主机中。我使用 wamp 服务器,我的操作系统是 Windows XP 3 。我正在使用 Eclipse 。 Android SDK 17 安卓版本 4.2。下面是我的代码:

提前致谢。

public class MainActivity extends Activity {

String name;
String id;
InputStream is=null;
String result=null;
String line=null;
int code;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.buddyproject.R.layout.activity_main);

final EditText e_id=(EditText) findViewById(R.id.editText1);
final EditText e_name=(EditText) findViewById(R.id.editText2);
Button insert=(Button) findViewById(R.id.button1);

insert.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

id = e_id.getText().toString();
name = e_name.getText().toString();

insert();
}
});
}

public void insert()
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("id",id));
nameValuePairs.add(new BasicNameValuePair("name",name));

try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/androidtest/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());
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

$host='localhost';
$uname='root';
$pwd='';
$db="test";

$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'];

$flag['code']=0;

$sql="INSERT INTO sample (id,name)VALUES('$id','$name')";



if (!mysql_query($sql,$con))
{
$flag['code']=1;
echo"hi";
}
print(json_encode($flag));
mysql_close($con);

最佳答案

你必须在这一行更改你的IP

 HttpPost httppost = new HttpPost("http://10.0.2.2/androidtest/insert.php");

打开 cmd-type ipconfig -hit Enter。获取IPv4地址通常以192.X开头

enter image description here

更改代码中的 IP。应该可以工作

关于php - 无法通过php使用android模拟器将数据插入mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25348869/

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