SlideShare a Scribd company logo
Android
Connecting To Internet (2)
Today’s agenda
Why it is important to run network operations on separate thread?
Network operations in background.
AsyncTask.
Parsing different data formats: Json.
--
Threads
“A sequence of execution.”
There can be more than one thread of execution to achieve multiple tasks
simultaneously.
This is called “multi-threading”.
Multithreading helps in optimal usage of resources and time.
Java has inherent support for multithreading.
Android - The UI Thread
There is 1 main thread in Android application.
This main thread is also called “The UI Thread”.
The UI thread is responsible for the user’s interaction with UI.
It handles all the button clicks, animations, displaying data and images on UI etc.
The UI thread should not be kept busy for other long running operations.
UI thread is only for UI operations
Should not do any long running operation on
UI thread.
Avoid these operations on UI thread:
● Get data from internet.
● Read file from permanent storage.
● Do heavy calculations.
● Parse large data.
Responsiveness
You want to make your app responsive.
Responsiveness = smooth user experience.
Delay in response to user action = bad user experience.
Delay can lead to ANR (Application Not Responding) or even crash.
Network operations
Do not ever do Internet operation on main thread.
It will also lead to “NetworkOnMainThread” exception.
Use a worker thread for all network operations.
But, update to UI can only happen on UI thread.
When operation is complete. Give the result back to UI thread.
● Handlers
● AsyncTasks
AsyncTask
private class MyLongRunningTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
// Do network operation
}
protected void onProgressUpdate(Integer... progress) {
// update progress on UI
}
protected void onPostExecute(Long result) {
// Finally display the end result on UI
}
}
AsyncTask
doInBackground(): For doing long running background task.
onProgressUpdate(): For showing the progress of the background task on UI. E.g.
percentage of downloaded file.
onPostExecute(): Finally showing the result of the task completed.
Data Formats
Simple text
Json
XML
Images
Audio/Video file
InputStream
Json
Lightweight data format.
Easy to read and edit for humans.
Either a collection of key-value pairs or a list of values.
Used to transfer data from server to client in
Web-services.
Parsing Json Data
{
"firstName":"Paramvir",
"lastName":"Singh",
"address":{
"streetAddress":"Street no. 123",
"city":"Gurgaon",
"state":"HR",
"postalCode":"122002"
},
"phoneNumbers":[
{
"type":"home",
"number":"123456"
},
{
"type":"office",
"number":"22-333-44"
}],
"children":[
],
}
Next..
Handlers
Loading images from web
Limitations and common problems in connectivity
Services
***
About Me
Paramvir Singh, Android Developer and Trainer
paramvir.singh88@gmail.com
https://in.linkedin.com/in/paramvir-singh-android-developer-b45b5321

More Related Content

PPTX
Android ui part 2
PPTX
Android Session 6 - UI Part 1
PPTX
Android Starting App Development
PPTX
Android enterprise application development
PPTX
Leveraging Modernizr and Media Queries
PPTX
Jquery mobile book review
PPTX
PPTX
Windows aazuremobileservices
Android ui part 2
Android Session 6 - UI Part 1
Android Starting App Development
Android enterprise application development
Leveraging Modernizr and Media Queries
Jquery mobile book review
Windows aazuremobileservices

Similar to Android Connecting to internet Part 2 (20)

PDF
Android - Background operation
PDF
MobileAppDev Handout#4
PPTX
Thread_Handlers_and_Executors_Presentation.pptx
PPTX
Threads handlers and async task, widgets - day8
PPTX
Lecture #2 threading, networking &amp; permissions final version #2
PPTX
Background Thread
PPTX
Threading model in windows store apps
PDF
Think Async: Understanding the Complexity of Multithreading - Avi Kabizon & A...
PDF
Asynchronous Programming in Android
PDF
[Android] Multiple Background Threads
PDF
Not Quite As Painful Threading
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
PPTX
Android 101 Session @thejunction32
PPTX
Dori waldman android _course
PPT
Tech talk
PDF
Android Jumpstart Jfokus
PPTX
Android session-5-sajib
PPTX
Dori waldman android _course_2
PDF
Tech Talk: App Functionality (Android)
PDF
[143]Inside fuse deview 2016
Android - Background operation
MobileAppDev Handout#4
Thread_Handlers_and_Executors_Presentation.pptx
Threads handlers and async task, widgets - day8
Lecture #2 threading, networking &amp; permissions final version #2
Background Thread
Threading model in windows store apps
Think Async: Understanding the Complexity of Multithreading - Avi Kabizon & A...
Asynchronous Programming in Android
[Android] Multiple Background Threads
Not Quite As Painful Threading
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Android 101 Session @thejunction32
Dori waldman android _course
Tech talk
Android Jumpstart Jfokus
Android session-5-sajib
Dori waldman android _course_2
Tech Talk: App Functionality (Android)
[143]Inside fuse deview 2016
Ad

More from Paramvir Singh (9)

PDF
Ai and using ml in mobile apps
PPTX
AI in image recognition
PDF
Android gps, location services, camera and sensors - Paramvir Singh
PDF
Dependency injection and dagger2 in android paramvir singh
PDF
Android: Network optimization by Paramvir Singh
PPTX
Android Connecting to Internet
PPTX
Android one, why it is important for Android developers in India
PPTX
Clean code, Better coding practices
PPTX
Near field communication
Ai and using ml in mobile apps
AI in image recognition
Android gps, location services, camera and sensors - Paramvir Singh
Dependency injection and dagger2 in android paramvir singh
Android: Network optimization by Paramvir Singh
Android Connecting to Internet
Android one, why it is important for Android developers in India
Clean code, Better coding practices
Near field communication
Ad

Recently uploaded (20)

PPTX
Prokaryotes v Eukaryotes PowerPoint.pptx
PPTX
Job-opportunities lecture about it skills
PPTX
OnePlus 13R – ⚡ All-Rounder King Performance: Snapdragon 8 Gen 3 – same as iQ...
PPTX
退学买新西兰毕业证(WelTec毕业证书)惠灵顿理工学院毕业证国外证书制作
PPT
BCH3201 (Enzymes and biocatalysis)-JEB (1).ppt
PPTX
FINAL PPT.pptx cfyufuyfuyuy8ioyoiuvy ituyc utdfm v
PDF
APNCET2025RESULT Result Result 2025 2025
PPTX
AREAS OF SPECIALIZATION AND CAREER OPPORTUNITIES FOR COMMUNICATORS AND JOURNA...
PPTX
Surgical thesis protocol formation ppt.pptx
PPTX
PE3-WEEK-3sdsadsadasdadadwadwdsdddddd.pptx
PPTX
Overview Planner of Soft Skills in a single ppt
PDF
MCQ Practice CBT OL Official Language 1.pptx.pdf
PPTX
cse couse aefrfrqewrbqwrgbqgvq2w3vqbvq23rbgw3rnw345
PDF
L-0018048598visual cloud book for PCa-pdf.pdf
PPTX
internship presentation of bsnl in colllege
PDF
313302 DBMS UNIT 1 PPT for diploma Computer Eng Unit 2
PDF
Blue-Modern-Elegant-Presentation (1).pdf
PDF
Prostaglandin E2.pdf orthoodontics op kharbanda
PPTX
Sports and Dance -lesson 3 powerpoint presentation
DOCX
mcsp232projectguidelinesjan2023 (1).docx
Prokaryotes v Eukaryotes PowerPoint.pptx
Job-opportunities lecture about it skills
OnePlus 13R – ⚡ All-Rounder King Performance: Snapdragon 8 Gen 3 – same as iQ...
退学买新西兰毕业证(WelTec毕业证书)惠灵顿理工学院毕业证国外证书制作
BCH3201 (Enzymes and biocatalysis)-JEB (1).ppt
FINAL PPT.pptx cfyufuyfuyuy8ioyoiuvy ituyc utdfm v
APNCET2025RESULT Result Result 2025 2025
AREAS OF SPECIALIZATION AND CAREER OPPORTUNITIES FOR COMMUNICATORS AND JOURNA...
Surgical thesis protocol formation ppt.pptx
PE3-WEEK-3sdsadsadasdadadwadwdsdddddd.pptx
Overview Planner of Soft Skills in a single ppt
MCQ Practice CBT OL Official Language 1.pptx.pdf
cse couse aefrfrqewrbqwrgbqgvq2w3vqbvq23rbgw3rnw345
L-0018048598visual cloud book for PCa-pdf.pdf
internship presentation of bsnl in colllege
313302 DBMS UNIT 1 PPT for diploma Computer Eng Unit 2
Blue-Modern-Elegant-Presentation (1).pdf
Prostaglandin E2.pdf orthoodontics op kharbanda
Sports and Dance -lesson 3 powerpoint presentation
mcsp232projectguidelinesjan2023 (1).docx

Android Connecting to internet Part 2

  • 2. Today’s agenda Why it is important to run network operations on separate thread? Network operations in background. AsyncTask. Parsing different data formats: Json. --
  • 3. Threads “A sequence of execution.” There can be more than one thread of execution to achieve multiple tasks simultaneously. This is called “multi-threading”. Multithreading helps in optimal usage of resources and time. Java has inherent support for multithreading.
  • 4. Android - The UI Thread There is 1 main thread in Android application. This main thread is also called “The UI Thread”. The UI thread is responsible for the user’s interaction with UI. It handles all the button clicks, animations, displaying data and images on UI etc. The UI thread should not be kept busy for other long running operations.
  • 5. UI thread is only for UI operations Should not do any long running operation on UI thread. Avoid these operations on UI thread: ● Get data from internet. ● Read file from permanent storage. ● Do heavy calculations. ● Parse large data.
  • 6. Responsiveness You want to make your app responsive. Responsiveness = smooth user experience. Delay in response to user action = bad user experience. Delay can lead to ANR (Application Not Responding) or even crash.
  • 7. Network operations Do not ever do Internet operation on main thread. It will also lead to “NetworkOnMainThread” exception. Use a worker thread for all network operations. But, update to UI can only happen on UI thread. When operation is complete. Give the result back to UI thread. ● Handlers ● AsyncTasks
  • 8. AsyncTask private class MyLongRunningTask extends AsyncTask<URL, Integer, Long> { protected Long doInBackground(URL... urls) { // Do network operation } protected void onProgressUpdate(Integer... progress) { // update progress on UI } protected void onPostExecute(Long result) { // Finally display the end result on UI } }
  • 9. AsyncTask doInBackground(): For doing long running background task. onProgressUpdate(): For showing the progress of the background task on UI. E.g. percentage of downloaded file. onPostExecute(): Finally showing the result of the task completed.
  • 11. Json Lightweight data format. Easy to read and edit for humans. Either a collection of key-value pairs or a list of values. Used to transfer data from server to client in Web-services.
  • 12. Parsing Json Data { "firstName":"Paramvir", "lastName":"Singh", "address":{ "streetAddress":"Street no. 123", "city":"Gurgaon", "state":"HR", "postalCode":"122002" }, "phoneNumbers":[ { "type":"home", "number":"123456" }, { "type":"office", "number":"22-333-44" }], "children":[ ], }
  • 13. Next.. Handlers Loading images from web Limitations and common problems in connectivity Services ***
  • 14. About Me Paramvir Singh, Android Developer and Trainer paramvir.singh88@gmail.com https://in.linkedin.com/in/paramvir-singh-android-developer-b45b5321