Presentation is loading. Please wait.

Presentation is loading. Please wait.

Networking Android Club 2015. Networking AsyncTask HttpUrlConnection JSON Parser.

Similar presentations


Presentation on theme: "Networking Android Club 2015. Networking AsyncTask HttpUrlConnection JSON Parser."— Presentation transcript:

1 Networking Android Club 2015

2 Networking AsyncTask HttpUrlConnection JSON Parser

3 Thread UI thread Background thread

4 AsyncTask Helps us to manage process of backgroud thread Started from UI thread

5 AsyncTask: 3 parameters 1. Params 2. Progess 3. Result

6 AsyncTask: example TimeUnit.SECONDS.sleep(10);

7 AsyncTask: practice Create public static ProgressDialog in Activity Create AsyncTask onPreExecute: set message to dialog onPreExecute: show progress dialog onPostExecute: dismiss dialog

8 AsyncTask: pass data HttpTask task = new HttpTask(); task.execute(3); TimeUnit.SECONDS.sleep(params[0]);

9 Pass data: practice Change previous code so that it should wait twice amount of time If you pass 3, it should wait 6 seconds If 10, then 20

10 Networking: easiest way 1.Add permission: INTERNET 2.Create AsyncTask 3. Create HttpUrlConnection 4. Convert InputStream to String 5. Update UI 6. Start from UI

11 Step 1: Add permission Open AndroidManifest.xml Add this permission:

12 Step 2: Create AsynkTask public class HttpTask extends AsyncTask { @Override protected String doInBackground(String... params) { return null; } @Override protected void onPostExecute(String s) { } }

13 Step 3: HttpUrlConnection URL url = new URL(params[0]); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.connect();

14 Step 4: InputString to String InputStream stream = connection.getInputStream(); InputStreamReader isReader = new InputStreamReader(stream ); BufferedReader buffer = new BufferedReader(isReader ); StringBuilder content = new StringBuilder(); String line; while ((line = buffer.readLine()) != null) { content.append(line); }

15 Step 5: Update UI MainActivity.tvData.setText(s);

16 Step 6: Start from UI HttpTask task = new HttpTask(); String url = "http://jsonip.com"; task.execute(url);

17 HttpUrlConnection: practice http://www.telize.com/geoip?callback=YOUR_IP 1.Create new AsyncTask 2. Get content using HttpUrlConnection 3. Convert content InputStream to String 4. Update UI 5. Pass that URL to AsyncTask

18 Parsing JSON: example JSONObject object = new JSONObject(s); String ip = object.getString("ip"); MainActivity.tvData.setText("My ip address is "+ip);

19 Parsing JSON: practice http://www.telize.com/geoip?callback=Y OUR_IP Show latitude Show longitude Show ISP Show country

20 Homework 1: Forecast.IO developer.forecast.io

21 Homework 2: Football- api.com http://football-api.com Demo - EPL Free English Premier League Access limit: 1000 requests per hour Access from 1 IP address

22 Homework 3: Your own API Create your own API For example: calculator for communal services


Download ppt "Networking Android Club 2015. Networking AsyncTask HttpUrlConnection JSON Parser."

Similar presentations


Ads by Google