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

Similar to Android Connecting to internet Part 2

Threads handlers and async task, widgets - day8
Threads   handlers and async task, widgets - day8Threads   handlers and async task, widgets - day8
Threads handlers and async task, widgets - day8
Utkarsh Mankad
 

Similar to Android Connecting to internet Part 2 (20)

Android - Background operation
Android - Background operationAndroid - Background operation
Android - Background operation
 
MobileAppDev Handout#4
MobileAppDev Handout#4MobileAppDev Handout#4
MobileAppDev Handout#4
 
Threads handlers and async task, widgets - day8
Threads   handlers and async task, widgets - day8Threads   handlers and async task, widgets - day8
Threads handlers and async task, widgets - day8
 
Lecture #2 threading, networking &amp; permissions final version #2
Lecture #2  threading, networking &amp; permissions final version #2Lecture #2  threading, networking &amp; permissions final version #2
Lecture #2 threading, networking &amp; permissions final version #2
 
Background Thread
Background ThreadBackground Thread
Background Thread
 
Threading model in windows store apps
Threading model in windows store appsThreading model in windows store apps
Threading model in windows store apps
 
Think Async: Understanding the Complexity of Multithreading - Avi Kabizon & A...
Think Async: Understanding the Complexity of Multithreading - Avi Kabizon & A...Think Async: Understanding the Complexity of Multithreading - Avi Kabizon & A...
Think Async: Understanding the Complexity of Multithreading - Avi Kabizon & A...
 
Asynchronous Programming in Android
Asynchronous Programming in AndroidAsynchronous Programming in Android
Asynchronous Programming in Android
 
[Android] Multiple Background Threads
[Android] Multiple Background Threads[Android] Multiple Background Threads
[Android] Multiple Background Threads
 
Not Quite As Painful Threading
Not Quite As Painful ThreadingNot Quite As Painful Threading
Not Quite As Painful Threading
 
Android 101 Session @thejunction32
Android 101 Session @thejunction32Android 101 Session @thejunction32
Android 101 Session @thejunction32
 
Dori waldman android _course
Dori waldman android _courseDori waldman android _course
Dori waldman android _course
 
Tech talk
Tech talkTech talk
Tech talk
 
Android Jumpstart Jfokus
Android Jumpstart JfokusAndroid Jumpstart Jfokus
Android Jumpstart Jfokus
 
Android session-5-sajib
Android session-5-sajibAndroid session-5-sajib
Android session-5-sajib
 
Dori waldman android _course_2
Dori waldman android _course_2Dori waldman android _course_2
Dori waldman android _course_2
 
Tech Talk: App Functionality (Android)
Tech Talk: App Functionality (Android)Tech Talk: App Functionality (Android)
Tech Talk: App Functionality (Android)
 
[143]Inside fuse deview 2016
[143]Inside fuse   deview 2016[143]Inside fuse   deview 2016
[143]Inside fuse deview 2016
 
High Performance NodeJS
High Performance NodeJSHigh Performance NodeJS
High Performance NodeJS
 
Android: the Single Activity, Multiple Fragments pattern | One Activity to ru...
Android: the Single Activity, Multiple Fragments pattern | One Activity to ru...Android: the Single Activity, Multiple Fragments pattern | One Activity to ru...
Android: the Single Activity, Multiple Fragments pattern | One Activity to ru...
 

More from Paramvir Singh

Android one, why it is important for Android developers in India
Android one, why it is important for Android developers in IndiaAndroid one, why it is important for Android developers in India
Android one, why it is important for Android developers in India
Paramvir Singh
 

More from Paramvir Singh (9)

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

Recently uploaded

Genaihelloallstudyjamheregetstartedwithai
GenaihelloallstudyjamheregetstartedwithaiGenaihelloallstudyjamheregetstartedwithai
Genaihelloallstudyjamheregetstartedwithai
joceko6768
 
Dr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdf
Dr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdfDr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdf
Dr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdf
Dr. Nazrul Islam
 
皇冠体育- 皇冠体育官方网站- CROWN SPORTS| 立即访问【ac123.net】
皇冠体育- 皇冠体育官方网站- CROWN SPORTS| 立即访问【ac123.net】皇冠体育- 皇冠体育官方网站- CROWN SPORTS| 立即访问【ac123.net】
皇冠体育- 皇冠体育官方网站- CROWN SPORTS| 立即访问【ac123.net】
larisashrestha558
 
Transferable Skills - Your Roadmap - Part 1 and 2 - Dirk Spencer Senior Recru...
Transferable Skills - Your Roadmap - Part 1 and 2 - Dirk Spencer Senior Recru...Transferable Skills - Your Roadmap - Part 1 and 2 - Dirk Spencer Senior Recru...
Transferable Skills - Your Roadmap - Part 1 and 2 - Dirk Spencer Senior Recru...
Dirk Spencer Corporate Recruiter LION
 

Recently uploaded (20)

Heidi Livengood Resume Senior Technical Recruiter / HR Generalist
Heidi Livengood Resume Senior Technical Recruiter / HR GeneralistHeidi Livengood Resume Senior Technical Recruiter / HR Generalist
Heidi Livengood Resume Senior Technical Recruiter / HR Generalist
 
134. Reviewer Certificate in Computer Science
134. Reviewer Certificate in Computer Science134. Reviewer Certificate in Computer Science
134. Reviewer Certificate in Computer Science
 
D.El.Ed. College List -Session 2024-26.pdf
D.El.Ed. College List -Session 2024-26.pdfD.El.Ed. College List -Session 2024-26.pdf
D.El.Ed. College List -Session 2024-26.pdf
 
135. Reviewer Certificate in Journal of Engineering
135. Reviewer Certificate in Journal of Engineering135. Reviewer Certificate in Journal of Engineering
135. Reviewer Certificate in Journal of Engineering
 
Genaihelloallstudyjamheregetstartedwithai
GenaihelloallstudyjamheregetstartedwithaiGenaihelloallstudyjamheregetstartedwithai
Genaihelloallstudyjamheregetstartedwithai
 
Employee Background Verification Service in Bangladesh
Employee Background Verification Service in BangladeshEmployee Background Verification Service in Bangladesh
Employee Background Verification Service in Bangladesh
 
Dr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdf
Dr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdfDr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdf
Dr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdf
 
0524.priorspeakingengagementslist-01.pdf
0524.priorspeakingengagementslist-01.pdf0524.priorspeakingengagementslist-01.pdf
0524.priorspeakingengagementslist-01.pdf
 
太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】
太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】
太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】
 
129. Reviewer Certificate in BioNature [2024]
129. Reviewer Certificate in BioNature [2024]129. Reviewer Certificate in BioNature [2024]
129. Reviewer Certificate in BioNature [2024]
 
Day care leadership document it helps to a person who needs caring children
Day care leadership document it helps to a person who needs caring childrenDay care leadership document it helps to a person who needs caring children
Day care leadership document it helps to a person who needs caring children
 
Operating system. short answes and Interview questions .pdf
Operating system. short answes and Interview questions .pdfOperating system. short answes and Interview questions .pdf
Operating system. short answes and Interview questions .pdf
 
Luke Royak's Personal Brand Exploration!
Luke Royak's Personal Brand Exploration!Luke Royak's Personal Brand Exploration!
Luke Royak's Personal Brand Exploration!
 
0524.THOMASGIRARD_CURRICULUMVITAE-01.pdf
0524.THOMASGIRARD_CURRICULUMVITAE-01.pdf0524.THOMASGIRARD_CURRICULUMVITAE-01.pdf
0524.THOMASGIRARD_CURRICULUMVITAE-01.pdf
 
皇冠体育- 皇冠体育官方网站- CROWN SPORTS| 立即访问【ac123.net】
皇冠体育- 皇冠体育官方网站- CROWN SPORTS| 立即访问【ac123.net】皇冠体育- 皇冠体育官方网站- CROWN SPORTS| 立即访问【ac123.net】
皇冠体育- 皇冠体育官方网站- CROWN SPORTS| 立即访问【ac123.net】
 
欧洲杯投注网站-欧洲杯投注网站推荐-欧洲杯投注网站| 立即访问【ac123.net】
欧洲杯投注网站-欧洲杯投注网站推荐-欧洲杯投注网站| 立即访问【ac123.net】欧洲杯投注网站-欧洲杯投注网站推荐-欧洲杯投注网站| 立即访问【ac123.net】
欧洲杯投注网站-欧洲杯投注网站推荐-欧洲杯投注网站| 立即访问【ac123.net】
 
Widal Agglutination Test: A rapid serological diagnosis of typhoid fever
Widal Agglutination Test: A rapid serological diagnosis of typhoid feverWidal Agglutination Test: A rapid serological diagnosis of typhoid fever
Widal Agglutination Test: A rapid serological diagnosis of typhoid fever
 
欧洲杯买球平台-欧洲杯买球平台推荐-欧洲杯买球平台| 立即访问【ac123.net】
欧洲杯买球平台-欧洲杯买球平台推荐-欧洲杯买球平台| 立即访问【ac123.net】欧洲杯买球平台-欧洲杯买球平台推荐-欧洲杯买球平台| 立即访问【ac123.net】
欧洲杯买球平台-欧洲杯买球平台推荐-欧洲杯买球平台| 立即访问【ac123.net】
 
132. Acta Scientific Pharmaceutical Sciences
132. Acta Scientific Pharmaceutical Sciences132. Acta Scientific Pharmaceutical Sciences
132. Acta Scientific Pharmaceutical Sciences
 
Transferable Skills - Your Roadmap - Part 1 and 2 - Dirk Spencer Senior Recru...
Transferable Skills - Your Roadmap - Part 1 and 2 - Dirk Spencer Senior Recru...Transferable Skills - Your Roadmap - Part 1 and 2 - Dirk Spencer Senior Recru...
Transferable Skills - Your Roadmap - Part 1 and 2 - Dirk Spencer Senior Recru...
 

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