-1

I am trying to fetch data from mysql database and populate it on a listview.

I am getting the ip address of my system like this

String link_url = "192.168.19.6";

Now in my wamp server I have a file called

/product_api/getDataProduct.php";

I now I want this file to called in eclipse I am doing this

link_url = Referensi.link+"/product_api/getDataProduct.php";

Please is this the right way of connecting android to mysql and how I do know if the file I am calling on eclipse is connected to mysql database.

this is the getDataOriduct.php file

<?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;
?>

this is the included file

<?php
    $conn = mysql_connect("localhost","root","");
    $db = mysql_select_db("android_tokobaju");
?>

this is where the error took place and this method is defined in oncreate method

JSONParser jParser = new JSONParser();
        String nlink_url = link_url +"/product_api/getDataProduct.php";
        JSONObject json = jParser.AmbilJson(nlink_url);

This is the stacktrace

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.

This is the AmbilJson function

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

        }

Please with the above is my database connected to mysql db. Thanks

Blaze
  • 2,269
  • 11
  • 40
  • 82

2 Answers2

2

You are in a very wrong direction my friend. There are lots of problem in the code which you shared. You have to know something before you start.

Why shouldn't I use mysql_* functions in PHP

After that JSON with PHP

After that your code will look like this

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);
?>

After that on android side you have to use

Simple parse JSON from URL on Android and display in listview

You can't connect android to MySql directly you have to use any server side language which will fetch data from the mysql and feed to the android. In your case PHP is server side scripting language which is fetching data from mysql and returning JSON as a response. By Android making an HTTP request to the server you can get that JSON from the server and then inflate the list with that data.

Community
  • 1
  • 1
Aman Rawat
  • 2,625
  • 1
  • 25
  • 40
-1

you should add username , password ,hostname and database. add that they try again.

<?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);
?>
Moudiz
  • 7,211
  • 22
  • 78
  • 156
  • So can I call it from eclipse this way link_url = Referensi.link+"/product_api/getDataProduct.php"; – Blaze May 09 '16 at 09:07
  • I am getting this error Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. – Blaze May 09 '16 at 09:27
  • @Moudiz i think mysql_connect use 3 parameter in it mysql_connect($hostname, $username, $password) and it is also depricated http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Aman Rawat May 09 '16 at 09:30
  • Is that the cause of my error? – Blaze May 09 '16 at 09:35