gpt4 book ai didi

android - 我如何连接mysql数据库并使用android代码将数据插入其中

转载 作者:搜寻专家 更新时间:2023-10-30 21:59:04 24 4
gpt4 key购买 nike

我的托管服务器上有 mysql 数据库

简单的android应用程序上,我有反馈表,在提交上,我想将数据插入mysql数据库在服务器上。

尝试了 google 并发现了以下本地机器的解决方案

how do I connect to my hosting server and mysql database without any php code?

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/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());
}
}

最佳答案

这里

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

提到insert.php意味着你必须把这个文件放在服务器上

只需将http://10.0.2.2/insert.php 更改为您存储文件的服务器文件路径

insert.php 的代码

        // this variables is used for connecting to database and server
$host="yourhost";
$uname="username";
$pwd='pass';
$db="dbname";

// this is for connecting
$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");

// getting id and name from the client
if(isset($_REQUEST)){
$id=$_REQUEST['id'];
$name=$_REQUEST['name'];}


// variable used to tell the client whether data is stored in database or not

$flag['code']=0;

// for insertion
if($r=mysql_query("insert into emp_info values('$name','$id') ",$con))
{
//if insertion succeed set code to 1
$flag['code']=1;
echo"hi";
}
// send result to client that will be 1 or 0
print(json_encode($flag));

//close
mysql_close($con);
?>

如您的代码中所述,无论数据是否存储,这将从服务器获取值,代码=1 表示已存储,代码=0 表示未存储

 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();
}

关于android - 我如何连接mysql数据库并使用android代码将数据插入其中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25208654/

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