Cara menggunakan merge json mysql

Pada tutorial ini, anda akan belajar membuat sebuah aplikasi android sederhana yang menggunakan List View. Data yang akan ditampilkan adalah kumpulan aplikasi android yang populer di Play Store lengkap dengan icon, total download dan data rating dari masing-masing aplikasi. Data total download dan rating yang kita gunakan dalam tutorial ini bukan merupakan data asli, akan tetapi hanya berupa data dumi untuk tujuan demo.

Data dumi tersebut akan disimpan dalam database MySQL. Kemudian anda juga akan membuat script PHP yang akan mengambil data dumi tadi dari database MySQL. Aplikasi android yang anda buat kemudian memanggil script PHP tadi untuk mengambil data dumi dalam format JSON [Javascript Object Notation]. Sebelum saya menjelaskan tahap-tahap pembuatan aplikasi tersebut. Saya akan menjelaskan terlebih dahulu konsep-konsep dasar yang perlu anda pahami.

 

Mengenal JSON

JSON [Javascript Object Notation] merupakan format pertukaran data. Sebuah objek JSON merupakan kumpulan dari pasangan key dan value yang diawali dengan tanda “{” dan diakhiri dengan tanda “}”. Berikut ini adalah contoh sebuah objek JSON yang akan anda gunakan dalam tutorial ini.

var json = {
    'title' : 'Facebook',
    'rating' : 5,
    'total_dl' : 10990998,
    'icon' : 'facebook'
};

Sebuah array dari JSON merupakan serangkaian object JSON. Di bawah ini merupakan contoh array dari beberapa objek JSON di atas.

var json = [
    {'title' : 'Facebook',
    'rating' : 5,
    'total_dl' : 10990998,
    'icon' : 'facebook'},
    {'title' : 'Twitter',
    'rating' : 5,
    'total_dl' : 12343487,
    'icon' : 'twitter'},
    {'title' : 'Whatsapp',
    'rating' : 4,
    'total_dl' : 5635989,
    'icon' : 'whatsapp'}
    ];

 

Konsep Dasar

 

As I’ve stated before, we’ll create simple android application that show a list of top application. We use android list view to display the data. The list data come from MySQL server. So this application will have a http connection feature. Communication between server and android application is use JSON based data. In this tutorial we’ll use apached server in localhost that running AVD [Android Virtual Device].
The data that are fetched from server including applicatoin title, rating, total download and icon name. For simplicity of the tutorial, we’ll use an icons that are saved in /res/drawable folder for applications. This will reduce the complexity of downloading the icons from server.
Below is final screenshot of our tutorial

Populate Listview Using JSON

Server Side Task

1. Creating A Database in MySQL

Before creating a database in MySQL, make sure you’ve create a user to login into the MySQL server. In this tutorial, we use a default user root with blank password.
We’ll create a simple database scheme for the application. The database will be used to store application data. Execute the following sql to create such database.

CREATE DATABASE apps;

Now, we have a database apps. We need to create a table in database apps that will store the application title, total download, rating and icon file name. Execute the following sql to create the table.

CREATE TABLE `app_data`[
    `id` int unsigned NOT NULL AUTO_INCREMENT,
    `app_title` varchar[150] NOT NULL,
    `total_dl` int unsigned NOT NULL default 0,
    `rating` int unsigned NOT NULL default 0,
    `icon` varchar[120] NOT NULL,
    primary key[`id`]
];

Now, we must put sample application data into the table. Execute the following sql to create sample application data.

INSERT INTO app_data VALUES[null, "Facebook", 20099099, 5, "facebook"];
INSERT INTO app_data VALUES[null, "Twitter", 11342099, 5, "twitter"];
INSERT INTO app_data VALUES[null, "Google +", 10123023, 4, "google"];
INSERT INTO app_data VALUES[null, "Whatsapp", 10033876, 3, "whatsapp"];
INSERT INTO app_data VALUES[null, "Youtube", 10023444, 4, "youtube"];
INSERT INTO app_data VALUES[null, "Line", 9023434, 5, "line"];
INSERT INTO app_data VALUES[null, "Kakao Talk", 8247836, 3, "kakao"];
INSERT INTO app_data VALUES[null, "Linked In", 784736, 4, "linkedin"];
INSERT INTO app_data VALUES[null, "Angry Bird", 693847, 2, "angrybird"];
INSERT INTO app_data VALUES[null, "Skype", 528374, 3, "skype"];

 

2. Create PHP Script to Fetch the Data

The database now ready, we need create a PHP script that connect to MySQL server and get the application data. Then the application data is converted into JSON string that will send into client [android application]. This script use user root with blank password to login into MySQL server, you can change them to meet your server account.

Bài mới nhất

Chủ Đề