应用程序 mysql 连接_使用Android应用程序连接mysql数据库

连接数据库和Android应用时出现问题。我正在尝试实施this教程。一切似乎都很好,但我没有取得任何成功而不是错误。

有一个按钮侦听器,它可以在点击时向PHP文件发送帖子并获得结果。这是它的代码: -

ok.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

ArrayList postParameters = new ArrayList();

postParameters.add(new BasicNameValuePair("username", un.getText().toString()));

postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));

//String valid = "1";

String response = null;

try {

response = CustomHttpClient.executeHttpPost("http://10.0.2.2/check.php", postParameters);

String res=response.toString();

Log.d("res:", res);

// res = res.trim();

res= res.replaceAll("\\s+","");

//error.setText(res);

if(res.equals("1"))

error.setText("Correct Username or Password");

else

error.setText("Sorry!! Incorrect Username or Password");

} catch (Exception e) {

un.setText(e.toString());

}

}

});这是http post方法: -

public static String executeHttpPost(String url, ArrayList postParameters) throws Exception {

BufferedReader in = null;

try {

HttpClient client = getHttpClient();

HttpPost request = new HttpPost(url);

UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);

request.setEntity(formEntity);

HttpResponse response = client.execute(request);

in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

StringBuffer sb = new StringBuffer("");

String line = "";

String NL = System.getProperty("line.separator");

while ((line = in.readLine()) != null) {

sb.append(line + NL);

}

in.close();

String result = sb.toString();

Log.d("postMethodReturn", result);

return result;

} finally {

if (in != null) {

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}PHP代码如下: -

$un=$_POST['username'];

$pw=$_POST['password'];

//connect to the db

$user = "xyz";

$pswd = "xyz";

$db = "mydb";

$host = "localhost";

$conn = mysql_connect($host, $user, $pswd);

mysql_select_db($db);

//run the query to search for the username and password the match

$query = "SELECT * FROM mytable WHERE user = '$un' AND pass = '$pw'";

$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());

//this is where the actual verification happens

if(mysql_num_rows($result) --> 0)

echo 1; // for correct login response

else

echo 0; // for incorrect login response

?>程序中是否有错误?我尝试在活动代码中记录res(http响应)的中间值并导致execute post方法,但没有记录任何内容。尝试将“localhost”更改为“127.0.0.1”,并将其更改为公开可用的虚拟主机,但所有数据库环境均未成功。所有这些都在模拟器和公共主机上,也使用真实设备进行尝试。从浏览器检查时,服务器似乎正在运行。数据库与值存在。所有运行的服务(apache,mysql)。

主要问题是没有错误!任何建议发生了什么问题?

找不到有同样问题的人。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值