Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

Lp trnh Android

Bi
60

Tng tc dch v
Web dng JSON
nguyn thy

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

Ni dung bi hc

JSON l g?
Cu trc lp, i tng, thnh phn
ca Json Object, Json Array
Cch c d liu dng Json

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

Khi nim nh Dng JSON:


JSON (JavaScript Object Notation) c nh ngha d theo ngn ng JavaScript, tiu
chun ECMA-262 nm 1999, cu trc l mt nh dng vn bn n gin vi cc trng
d liu c lng vo nhau. JSON c s dng trao i d liu gia cc thnh phn
ca mt h thng tng thch vi hu ht cc ngn ng C, C++, C#, Java, JavaScript,
Perl, Python

JSON c xy dng da trn hai cu trc chnh:


Tp hp cp gi tr name/value, trong nhiu ngn ng khc nhau cp gi tr ny c th
l object, record, struct, dictionary, hash table, keyed list
Tp hp danh sch cc gi tr, c th l array, vector, list hay sequence.

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

V tu thuc vo d liu cn trao i, JSON c th c nhiu dng khc nhau, tuy nhin c
th tng hp nhng hai dng chnh sau:
Mt i tng Object cha cc cp gi tr string/value khng cn th t, c bao
trong cp {}, cc gi tr bn trong c nh dng string:value v chia cch nhau bi
du ,. Value y c th l chui, s, true- false, nullC th xem m t cng v d
sau:

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

V d:

Lp trnh Android

Mt i tng mng c bao gm nhu phn t con c th t. Cc phn t con c bao


trong cp [] v chia cch nhau bi du ,. Mi phn t con c th l mt gi tr n l
nh: s, chui, true-false, null hoc mt object khc, thm ch c th l mt mng.

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

Vic c v ghi nh dng JSON c tch hp sn trong Android SDK (n nm trong th


vin org.json)
JSONObject: i tng qun l JSON dng mt Object.
JSONArray: i tng qun l JSON dng tp hn cc Object hoc
Array.
JSONStringer: i tng chuyn d liu JSON thnh dng chui.
JSONTokener: chuyn i i tng JSON (chun RFC-4627) m ho chui mt thnh
i tng tng ng.

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

Demo ly danh sch contact mu:


http://www.w3schools.com/website/customers_mysql.
php

I:\Topica\codingthamkhao\SourceCode_CU\BaitapModule4s2356\
HocJsonBai1

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

Nn to 1
JSonFactory

public class JSonFactory {


public static String readContentJSonFormat(Reader reader)
{
String data="";
try
{
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = reader.read()) != -1) {
sb.append((char) cp);
}
data=sb.toString();
}
catch (Exception ex)
{
Log.e("Loi_JSON",ex.toString());
}
return data;
}
}

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

Nn to 1
JSonFactory

public static JSONArray readJSonArray(String url)


{
JSONArray array=null;
try
{
//m lung c d liu t url
InputStream is = new URL(url).openStream();
//Khai bo i tng reader c th c c ni dung
//cha Unicode
BufferedReader rd = new BufferedReader
(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonFormat=readContentJSonFormat(rd);
//ta c c ni dung c format l Json vo
//bin jsonFormat--> ta cn a n v JsonArray
//v n c bao bc bi cp ngoc []
array=new JSONArray(jsonFormat);
}
catch (Exception ex)
{
Log.e("LOI_JSON",ex.toString());
}
return array;
}
}

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

c d liu t
JSOnArray
protected
ArrayList<Customer>

doInBackground(String... params) {
ArrayList<Customer> dsCustomer=new ArrayList<>();
try
{

JSONArray jsonArray=
JSonFactory.readJSonArray(params[0]);
if(jsonArray!=null)//nu c d liu
{
//duyt tt c JsonObject bn trong
//JsonArray nh sau:
for(int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObject=
jsonArray.getJSONObject(i);
Customer customer=new Customer();
if(jsonObject==null)continue;//nhy qua k tip
if(jsonObject.has("Name"))//c thuc tnh Name
{
customer.setName(jsonObject.getString("Name"));
}
if(jsonObject.has("City"))
{
customer.setCity(jsonObject.getString("City"));
}
if(jsonObject.has("Country"))
{
customer.setCountry(jsonObject.getString("Country"));
}
dsCustomer.add(customer);
Ths. Trn Duy Thanh duythanhcse@gmail.com
0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

END
Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

You might also like