Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts

Thursday, November 2, 2017

Create JSON Object And Array Using PHP MySQL

If you want to transfer your data to another client (mobile or server) you have to create your own JSON. JSON or JavaScript Object Notation is a file that used to transfer data between clients/server or between two different programming languages.

JSON have two types, they are :

  • JSON type Object
  • JSON type Array
What the different of these type?

JSON Type Object


JSON Type Object has characteristic always using mark {}, please take a look at the following sample json :



JSON type Array


We can call a json as an array is the JSON use the mark [] in its data. Why this json use the mark []? Because the data that will be send is more that one object. Please take a look at the following sample :


If you will transfer data more that one object, use the JSON type array. But, if you just transfer only one object, use JSON type Object as its format.

Create JSON Array using PHP and MySQL 


First, you have to preparing your data and has been saved in your MySQL database. In this project i will using data in my database and look like the follows :


Create your connection to connect the database and your project. Here is the code :

Connection.php


After you connection has works well. Now create your JSON type Array

JSONArray.php


Running the above file, and you will show your data as list in json.

Create JSON type Object Using PHP and MySQL


Here is the file that you can use to create JSON type Object.

The above json will catch data type object from android and will send its data to database. I have been implemented it and has been post the project in this blog. Please open this : Tutorial insert data from android using retrofit to Server (PHP + MySQL).

That's all. Hope this article helps you. Thank you.

Thursday, January 26, 2017

Displaying Data On Android (Retrofit 2.0 GET) From Server PHP + MySQL

Continuing the previous post about how to input data from android to server that made using PHP + MySQL, then today i going to create an article that still related with database (PHP and MySQL).

This article will talking about how to displaying data on android from server that made using PHP and MySQL. The server hostinged in localhost.

 
In android I using retrofit 2.0 as the tool that help me to do communicate data between android and server. Because this process just do displaying data, then the method that will used is method GET (Retrofit)

By following this tutorial until the end, you will learn 2 thing. First, you will learn how to create your own server using PHP and MySQL, and the second you will learn how to use retrofit (Method GET).

Also see : How to GET using retrofit 2.0 by paramter (using dummy server from apiary)

Creating Database


Database that uses still same as the previous article that talking about how to insert data from Android to Server (PHP+MySQL). There is no changes or replaced. You can follow the steps how to create the database from this article.

Code In PHP Project


In the php project, there are several php class that uses :
  • Connection.php
  • DisplayJsonFood.php

1. Connection.php

This file used to connecting database that we have created to out php project. Below is the full code of Connection.php :

2. DisplayJsonFood.php

This file used to displaying datas from database be a json format. Why json format? Becase Android cannot straight access the database, so between php and android communicate by using JSON. Below is the full code of this file :

the above code will displaying json look like the follows  :

Because the data food is possibility more that one, then on json above, data saved inside array tagline. The array tagline that i meant is []. Inside the [] there is tagline {}. Tagline {} called as object.

Example : If there are four datas in database, then the object inside tagline [] also became four,  so the form look like : [{},{},{},{}]. Please take a look at the following image :



On above image, beside the tagline [] and {}, also there are two attributes, there are status and message.

If the process successful, the the value of attribute status became "1", and the massage became "success". And in the process is failed, the value of attribute status became "0", and the message became "Failed while displaying data".

Code In Android Project


There are several java classes on project android required. These classes look like the follows :
  • FoodModel.java
  • ListFoodModel.java
  • LoggingInterceptor.java
  • ApiService.java
  • ApiClient.java
  • FoodAdapter.java
  • MainActivity.java

1. FoodModel.java

This file created as an object. Inside this file contains method getter and setter. Below is the full code of FoodModel.java :

2. ListFoodModel.java

Perhaps serveral of you have a questions what the different between FoodModel.java and ListFoodModel.java?

Here, as you can see at the json output, the object located inside array tagline, in order to make a similar form between model in android and the json format, then we create list that encapsule the FoodModel.java

In order to easy understand, please take a look at ListFoodModel.java, here :

3. LoggingInterceptor.java

This file used to see the output of the process displaying data. If there is an error, this class file know what the causes of the error. The output will appears on logcat. Below is the full code of this file :

4. ApiService.java

This file is an interface. Inside this file contains method GET that used to get data from json. The full code of this file is look like the follows :

5. ApiClient.java

This file contains the target URL that will used. And also this class contains the initialized of retrofit. The following is the full code :

6. FoodAdapter.java

This file used to set data that got from server to the each textview. The full code is like the follows :

7. MainActivity.java

MainActivity is the main class that contains all of the process of the above class. The full code of MainActivity is look like the follows :

That's the several steps how to display data on android from database (server made using PHP and MySQL). Hope this article useful. Thank you.

Download the project by clicking the following image


Retrofit Android Insert Data To Database (Server Using : PHP + MySQL)

In previous posts, I have created a tutorial that show process input data to server using dummy server (apiary), means there is no data that inserted do server. Apiary was used just for testing the process of the application.



I my previous tutorial that I created using apiary as server just focused to explain about method GET and POST on Retrofit, how to send data using method GET or POST. There is no PHP file.

But, today I going to write article about how to input data to database from android application. The database using MySQL (Localhost/phpMyAdmin) and the server made from PHP.

To transfer data to server, on Android I using external library call retrofit 2.0. Retrofit will helps us to send data to database.

In PHP file, I uses PDO as the way to connecting between PHP and database (MySQL)

Creating Database MySQL


The first thing that we have to do is creating database. You can create the database MySQL from anywhere, from example from HeidiSQL, MySQL Server or PhphMyAdmin. In this case I uses PhpMyAdmin to creating database.

Recommeded : Android Create, Read, Update, Delete data in Firebase Firestore

1. Open localhost/phpmyadmin (make sure your xampp has been on)

2. Then you will redirected to the page that looks like the following Figure 1. To create database, choose New (menu in Yellow)

Figure 1

3. After that, write your database name and choose the type of the database. Take a look at the following Figure 2

Figure 2

Create database name as : loginphpandroid
Choose the type : utf8_general_ci

4. After your database done, now create the Table. To create the Table click menu new below your database name. Take a look at Figure 3 below

Figure 3
After you click the menu new (menu in yellow), the you will redirecting to the page where you can write your Table's name.

5. To create name of your Table, please take a look at the Figure 4

Figure 4
Guidelines of  Figure 4 :
  • Give the name of the Table as : Food
  • Choose number of row in table Food
  • Then choose Go

6.  After the table has done, now create the attributes/row in Table Food. Take a look at the following Figure 4.

Figure 5

Guideline of Figure 5 :
  • Give name for the first attribute as idfood, its type :  INT
  • Set idfood as Primary Key
  • Because the attribute idfood is primary key, then checklist A_I (Auto Increment).
  • Give name for the second attribute as foodname, its type VARCHAR
  • Give name for the third attribute as foodqty, its type INT
  • If done, then choose Save

Oke the database is ready to use. Now time to configure code of the PHP and Android project.

PHP Code


Recommended : Create rating and review look like Google Play Store Android

On PHP project that I created contains several PHP classes. Among that PHP files, I placed on different package. For clearly about the folder structure of the project, just take a look at the following figure 6

Figure 6


1. Connection.php

This file used to connecting php project that created to the database. If connected, then we allowed to input data either from Mobile or Form HTML. Below is the full code of Connection.php  :

2. InsertFood.php

This file used to insert data that get from users to the database. In this data gotten from Mobile Android. Below is the full code of InsertFood.php :

Attention! There are several codes that I have to explain:

$response = array()

The code above created for save the response in json form.

//cek is the row was inserted or not 
if($sqlInsert){     
    //success inserted     
    $response["success"] = 1;     
    $response["message"] = "Food successful inserted!";
    echo json_encode($response);
}else{     
    //failed inserted     
    $response["success"] = 0;     
    $response["message"] = "Failed while insert data";
    echo json_encode($response);}

The code above will do checking. Is the input process has success or not. If success the response will like the follows :
  • success = 1
  • message = Food successful inserted!
When program do echo json_encode($rensponse), then it will displaying output json like the follows :

{"success":1,"message":"Food successful inserted!"}

If the process if falied, the the response that will like the follows :
  • success = 0
  • message = "Failed while insert data"
So when program do echo json_encode($response), the output json will display like the follows :

{"success":0,"message":"Failed while insert data"}

That json response will used in mobile, and will displaying on the Toast message.

Android Code


Recommended : Simply Login Using Firebase UI (No need API anymore)

In this project I using Retrofit 2.0 to help this project send or receive data from android to server.

1. Library

As always the first thing you do before continue project is compile the libraries that needed in the project. In this project compile the following libraries :

//add the following libraries 
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2' 
compile 'com.squareup.retrofit2:converter-gson:2.0.2'

2. Android Manifest

On AndroidManifest add user permission for internet, look like the follows :

<uses-permission android:name="android.permission.INTERNET"/>

3. Layout

There are several layout that used in this project :
  • activity_main.xml
  • popup_insert_food.xml
Create layout activity_main.xml, then paste the following code :

Next create layout popup_insert_food.xml. This layout contains two Edit Text for foodName dan foodQty. In its process this layout will appears on the popup. Paste the following code :


4. Java classes

There are several java classes that need in order to do insert data to the database, there are :
  • ApiService.java
  • ApiClient.java
  • LoggingInterceptors.java
  • InsertFoodResponseModel.java
  • MainActivity.java
This interface contains method that used to send data to server. Paste the following code :

This class contains the URL that used in this project. And also in this class contains initialized of the retrofit. Paste the following code :

Take a look at the URL. 192.168.43.147 is IP from my computer. And AndroidPhpLogin my project folder that located in localhost (htdocs). Change the IP/URL and the project folder name with your own.

This class used to save the response of the json from server (Take a look that the json that i have explained above in php code). Paste the following code :

Create class LoggingInterceptors.java. This class used to see the output of the input process to the server.

Why use this class? Because in retrofit we will face a little bit difficulty to see the result, especially the URL and the JSON. You can see the result on logcat. The full code on LoggingInterceptor.java is like the follows  :

Last is MainActivity.java. All the process and above classes will unite in this class. The full code of MainActivity.java is like the follows :

Demikian beberapa langkah yang dapat saya sampaikan tentang cara meng input data dari Android ke Database menggunakan Server yang dibuat dari Native PHP.
That's all about the steps how to input data from android to server made from PHP and MySQL.

If there are unclear about the steps above, feel-free to asked me. Thank you.

Download the project by clicking the following image