我正在为Android构建软键盘,它有我想要上传到我的网站服务器上的数据库中的用户行的设置,然后当用户更改设备并登录时,设置是相同的。我之前使用过SQL,但我还没有将android应用程序连接到我的网站数据库的经验。我在网上找到了一些东西,但是它为我在数据库中插入的字符串示例提供了null结果。这是我想在其上运行测试的数据库中的表:
名称|细节
拉扎尔|姓名jelic |名字
岩石|巴尔博亚
我的托管网站服务器上有一个名为keyboard.php
的PHP脚本,它的外观如下:
<?php
mysql_connect("hostname", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$link= $_GET['link'];
$query = "SELECT * FROM keyboard WHERE link=" . "'" . $link. "'";
$result =mysql_query($query);
if($result){
while($row= mysql_fetch_array( $result )) {
echo $row['name']
. " " . $row['link']
. " " . $row['time'];
}
}
?>
这是我的SyncDatabse.java
:
public class SyncDatabase {
public static String getFromDB(String linkToGetData) throws IOException {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
HttpClient httpclient=new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.ecloga.org/keyboard.php?link=" + linkToGetData);
HttpResponse response = httpclient.execute(httpget);
String theResponse = EntityUtils.toString(response.getEntity());
return theResponse;
}
}
请帮我解决这个问题。提前致谢。问任何你想知道的事情。
答案 0 :(得分:1)
测试这个php代码,因为你的结果是空的:
if (isset ($_GET ["name"])){
$name = mysql_real_escape_string($_GET ["name"]);
$query = htmlentities ($query);
mysql_query("SET NAMES 'utf8'");
$query = mysql_query("SELECT * FROM my_table WHERE name = '$name');
$result = mysql_query($query);
$rows = array();
if (!empty($result)){
while($r = mysql_fetch_assoc($result)){
// $rows[] = $r; has the same effect, without the superfluous data attribute
$r["Column1"] = $r["Column1"];
$r["Column2"] = $r["Column2"]; //You can add many columns like the table have
$rows[] = $r;
//$rows[] = array('Deal' => $r);
}
echo json_encode($rows);
}
}