我正试图从mysql数据库中获取数据,并将其填充到列表视图中。
我正在获取我的系统的ip地址,就像这个
String link_url = "192.168.19.6";
现在在我的wamp服务器中,我有一个名为的文件
/product_api/getDataProduct.php";
我现在我想在eclipse中调用这个文件我正在做这个
link_url = Referensi.link+"/product_api/getDataProduct.php";
请注意,这是将android连接到mysql的正确方式吗?我如何知道我在eclipse上调用的文件是否连接到了mysql数据库。
这是getDataOriduct.php文件
<?php
include("koneksi.php");
$q = mysql_query('select * from produk');
$v = '{"info" : [';
while($r=mysql_fetch_array($q))
{
$ob = array("<br>","<b>","</b>");
if(strlen($v)<12)
{
$v .= '{"id_produk" : "'.$r['id_produk'].'", "nama_produk" : "'.$r['nama_produk'].'", "harga" : "'.$r['harga'].'", "deskripsi" : "'.$r['deskripsi'].'", "stok" : "'.$r['stok'].'", "gambar" : "'.str_replace($ob,"",$r['gambar']).'"}';
}
else
{
$v .= ',{"id_produk" : "'.$r['id_produk'].'", "nama_produk" : "'.$r['nama_produk'].'", "harga" : "'.$r['harga'].'", "deskripsi" : "'.$r['deskripsi'].'", "stok" : "'.$r['stok'].'", "gambar" : "'.str_replace($ob,"",$r['gambar']).'"}';
}
}
$v .= ']}';
echo $v;
?>
这是包含的文件
<?php
$conn = mysql_connect("localhost","root","");
$db = mysql_select_db("android_tokobaju");
?>
这就是错误发生的地方,该方法在创建方法中定义
JSONParser jParser = new JSONParser();
String nlink_url = link_url +"/product_api/getDataProduct.php";
JSONObject json = jParser.AmbilJson(nlink_url);
这是堆垛机
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ecommysql.yonandroid/com.ecommysql.yonandroid.PhotoProduk}: java.lang.IllegalStateException: Target host must not be null, or set in parameters.
这是AmbilJson函数
public JSONObject AmbilJson(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
请用上面的是我的数据库连接到mysqldb。感谢
我的朋友,你走错了方向。您共享的代码中有很多问题。在开始之前你必须知道一些事情。
为什么我不应该在PHP 中使用mysql_*函数
之后JSON与PHP
之后,您的代码将如下所示
include("koneksi.php");
$q = mysql_query('select * from produk'); // change it too
$v['info'] = [];
while($r=mysql_fetch_array($q))
{
$ob = array("<br>","<b>","</b>");
$temp['id_produk'] = $r['id_produk'];
$temp['nama_produk'] = $r['nama_produk'];
$temp['harga'] = $r['harga'];
$temp['deskripsi'] = $r['deskripsi'];
$temp['stok'] = $r['stok'];
$temp['gambar'] = str_replace($ob,"",$r['gambar']);
$v['info'][] = $temp;
}
echo json_encode($v);
?>
之后,在安卓方面,你必须使用
在Android上从URL简单解析JSON并在listview 中显示
你不能直接将android连接到MySql,你必须使用任何服务器side语言,它将从mysql中获取数据并将其馈送到安卓在您的案例中,PHP是服务器端脚本语言从mysql中获取数据并返回JSON作为响应。通过Android向服务器发出HTTP请求,您可以从服务器,然后用该数据填充列表。
您应该添加用户名、密码、主机名和数据库。补充说他们会再试一次。
<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";
$dbase ="your_database";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password,$dbase)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//execute the SQL query and return records
$result = mysql_query("SELECT id, model,year FROM cars");
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo "ID:".$row{'id'}." Name:".$row{'model'}."Year: ". //display the results
$row{'year'}."<br>";
}
//close the connection
mysql_close($dbhandle);
?>