use web apis in android applications

27
www.company.com Use WEB APIs in android Applications The tutor: Ms. Phan Nguyệt Minh Members: Trần Minh Luận 12520248 Trần Thành Luân 12520246 Hồ Thị Thanh Thảo 12520406

Upload: coxanh21294

Post on 15-Aug-2015

34 views

Category:

Software


4 download

TRANSCRIPT

Page 1: Use WEB APIs in android Applications

Use WEB APIs in android Applications

The tutor:Ms. Phan Nguyệt Minh

Members:Trần Minh Luận 12520248Trần Thành Luân 12520246 Hồ Thị Thanh Thảo 12520406

Page 2: Use WEB APIs in android Applications

APIWEB APIs

Create a application

2

Web APIs Usage

Page 3: Use WEB APIs in android Applications

3

What’s API?

Page 4: Use WEB APIs in android Applications

4

What’s Web APIs?

Page 5: Use WEB APIs in android Applications

5

What’s Web APIs?Money getMoney (int number)

http://api.openweathermap.org/data/2.5/weather?q= <city>, <country>

Page 6: Use WEB APIs in android Applications

6

Web APIs Benefic

Page 7: Use WEB APIs in android Applications

7

Bing Finance

Page 8: Use WEB APIs in android Applications

8

Bing Weather

Page 9: Use WEB APIs in android Applications

9

GOOGLE MAP

Page 10: Use WEB APIs in android Applications

10

LING ESDICTIONARY

Page 11: Use WEB APIs in android Applications

11

WEB APIs Usage

Page 12: Use WEB APIs in android Applications

Wiki APIs

12

• http://en.wikipedia.org/w/api.php?action=languagesearch&search=VietNam

• http://en.wikipedia.org/w/api.php?action=login&format=json

Page 13: Use WEB APIs in android Applications

13

Google map APIs

• https://maps.googleapis.com/maps/api/place/textsearch/xml?query=restaurants+in+Sydney&key=AddYourOwnKeyHere

• https://maps.googleapis.com/maps/api/staticmap?center=-15.800513,-47.91378&zoom=11&size=200x200

Page 14: Use WEB APIs in android Applications

http://api.openweathermap.org/data/2.5/weather?q=Ho+Chi+Minh,vn

14

Weather APIs

Page 15: Use WEB APIs in android Applications

• http://devel.farebookings.com/api/curconversor/EUR/USD/1/

15

Currency APIs

Page 16: Use WEB APIs in android Applications

JSON (JavaScript Object Notation)• Faster• Simply• Enough to process a API output

16

WHY’S JSON?

Page 17: Use WEB APIs in android Applications

17

JSON OBJECT{

"title": "Despicable Me 2", "image": "http://api.androidhive.info/json/movies/11.jpg", "rating": 7.6, "releaseYear": 2013, "genre": ["Animation", "Comedy", "Family"] },

name

value

array

Type data of value: string, number, Boolean, null.

Page 18: Use WEB APIs in android Applications

18

Create a application

Page 19: Use WEB APIs in android Applications

The structure of Application

19

Data

String NhietDo;String ThanhPho;String Dogio;

MainActivity

//process i/o event and get cityName, country from user// show information that got from internet

GetData

String cityName;String country;

Void getData();

MyTask

String cityName;String country;

@overrideObject doInBackground (Object... params);

Page 20: Use WEB APIs in android Applications

String api = "http://api.openweathermap.org/data/2.5/weather?q=";

StringBuilder link = new StringBuilder (api + citiName + "," + country);

URL url;

HttpURLConnection httpCon;

InputStreamReader read;

StringBuilder result = new StringBuilder();

20

Declare something

Page 21: Use WEB APIs in android Applications

Get json file

21

link url httpCon stream

char buffer[] = new char[1024];while ((Pos= read.read(buffer)) != -1) { result.append(buffer, 0, Pos)}JSONObject json= new JSONObject (result.toString());

url = new URL (link.toString());

httpCon = (HttpURLConnection) url.openConnection();

stream = new InputStreamReader (httpCon.getInputStream());

Page 22: Use WEB APIs in android Applications

22

Get Data

json

main

wind

data.NhietDo

data.ThanhPho

data.DoGio

Data data = new Data();JSONObject main = json.getJSONObject("main");JSONObject wind = json.getJSONObject("wind");data.ThanhPho = main.getString("name");data.NhietDo = String.valueOf (main.getDouble ("temp") -273);data.DoGio = String.valueOf(wind.getDouble("speed"));

Page 23: Use WEB APIs in android Applications

Show data

23

Page 24: Use WEB APIs in android Applications

• To carry to get data, you must make a new thread when you click ...

Tips

24

myTask m = new myTask(); m.city = edDiaChi.getText().toString(); m.country = edCountry.getText().toString();m.execute(); try {m.get();txt_gio.setText( "Tốc độ gió :"+ m.data.DoGio);

txt_nhietDo.setText("Nhiệt độ :" +m.data.NhietDo);txt_tp.setText("Thành phố :"+m.data.ThanhPho);

} catch (InterruptedException e) {// TODO Auto-generated catch block e.printStackTrace();}

Page 25: Use WEB APIs in android Applications

25

Run application

Page 26: Use WEB APIs in android Applications

• API• WEB API• JSON OBJECT

26

Summary

Page 27: Use WEB APIs in android Applications

27

Thank for your attentions

^_^