Vous êtes sur la page 1sur 24

sundawijaya@gmail.

com

KLIEN SERVER
8 Get JSON from server

sundawijaya@gmail.com

Setup D evelopm ent Environm ent

Software :

Web Server
Web Programming Language
Database Server

sundawijaya@gmail.com

D atabase

RDBMS

MySQL (http://www.mysql.com/)
Oracle (http://www.oracle.com/)
Ms. Access (
http://office.microsoft.com/access)
Ms. SQL Server (
http://www.microsoft.com/sqlserver)
PostgreSQL (http://www.postgresql.org/)
etc
sundawijaya@gmail.com

W eb Program m ing
Language

Front End Client Side

HTML
Javascript

BackEnd Server Side

Java
PHP
ASP.NET
Ruby (on Rails)
etc
sundawijaya@gmail.com

W eb Server

Apache HTTP Server (


https://httpd.apache.org/)
Apache Tomcat (
http://tomcat.apache.org/)
Internet Information Services (IIS) (
http://iis.net/)
Nginx (http://nginx.org/)
etc
sundawijaya@gmail.com

Apache + M ySQ L + PH P
Apache + MySQL + PHP Package

https://www.apachefriends.org/

http://www.wampserver.com/
sundawijaya@gmail.com

Sistem Schem a

User or Client on
Mobile Device

JSON

sundawijaya@gmail.com

Test your Server

Test your server by opening the address


http://localhost/in your browser
Check phpmyadmin (front end of MySQL)
by openinghttp://localhost/phpmyadmin

sundawijaya@gmail.com

PH P Server Project

PHP: Hypertext Preprocessor


Location on C:\xampp\htdocs\server
PHP Example (example.php) :
<?php
echo Hello Android";
?>

Test on browser : http://localhost/


server/example.php
sundawijaya@gmail.com

exam ple_query.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("android_db");
$q=mysql_query("SELECT * FROM user");
while($row=mysql_fetch_assoc($q)) {
$json_output[]=$row;
}
echo json_encode($json_output);
?>
sundawijaya@gmail.com

Result
[
{
"username":"user",
"password":"pass
}
]

[{"username":"user","password":"pass"}]
sundawijaya@gmail.com

JSON (Java Script Object Notation) is


a lightweight data-interchange format. It
is easy for humans to read and write. It
is easy for machines to parse and
generate.

sundawijaya@gmail.com

sundawijaya@gmail.com

sundawijaya@gmail.com

JSO N Structure

An object is an unordered set of


name/value pairs

The pairs are enclosed within braces, { }


There is a colon between the name and the
value
Pairs are separated by commas
Example: { PK: TEK, m asa kuliah: 3}

An array is an ordered collection of


values

The values are enclosed within brackets, [ ]


sundawijaya@gmail.com
Values are separated by commas

JSO N Exam ple


{
"data":
[
{
"username":"user",
"password":"user"
}
]
}

echo '{"data":'.json_encode($json_output).'}';
sundawijaya@gmail.com

H ow to get JSO N from Server

HTTP request methods

GET
POST

Library Volley

sundawijaya@gmail.com

Read JSO N Form at

Android has JSONArray, JSONObject class for


parsing this format

sundawijaya@gmail.com

Exam ple # 1
String json
[{"username":"user","password":"pass"}]
Android :
JSONArray jArray = new JSONArray(json);
for (int i =
JSONObject
username =
password =
}

0; i < jArray.length(); i++) {


c = jArray.getJSONObject(i);
c.getString("username");
c.getString("password");
sundawijaya@gmail.com

Exam ple # 2
String json
{"data":[{"username":"user","password":"pass"}}
Android :

JSONObject jObject = new JSONObject(json);


JSONArray user = jObject.getJSONArray("data");
for (int i =
JSONObject
username =
password =
}

0; i < user.length(); i++) {


c = user.getJSONObject(i);
c.getString("username");
c.getString("password");
sundawijaya@gmail.com

{
"phoneNumber": [
{
"type": "phone",
"num": "(0251)12345"
},
{
"type": "fax",
"num": "(0251)54321"

Exercise

}
],
"address": {
"address": "jalan kumbang 14",
"city": "Bogor"
},
"name": "Diploma IPB"
}

sundawijaya@gmail.com

Android M anifest
Uses-Permission
android.permission.INTERNET

sundawijaya@gmail.com

Thank you
Andika Sundawijaya
sundawijaya@gmail.com
Teknik Komputer & Manajemen
Informatika
Program Diploma IPB

Vous aimerez peut-être aussi