Use WEB APIs in android Applications

Post on 15-Aug-2015

34 views 4 download

Transcript of 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

APIWEB APIs

Create a application

2

Web APIs Usage

3

What’s API?

4

What’s Web APIs?

5

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

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

6

Web APIs Benefic

7

Bing Finance

8

Bing Weather

9

GOOGLE MAP

10

LING ESDICTIONARY

11

WEB APIs Usage

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

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

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

14

Weather APIs

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

15

Currency APIs

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

16

WHY’S JSON?

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.

18

Create a application

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);

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

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());

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"));

Show data

23

• 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();}

25

Run application

• API• WEB API• JSON OBJECT

26

Summary

27

Thank for your attentions

^_^