SlideShare a Scribd company logo
Android
Connecting To Internet
Today’s agenda
Why Connect to Internet?
How to Connect to Internet?
Getting Data from Server.
Why Connect to Internet?
Two modes of apps:
● Standalone apps - Reminder/notes app, Photo editing app, Contacts app,
Music Player app.
● Connected apps - Chatting app, Food ordering app, News app, Banking apps,
Maps app.
Standalone apps More Power
Standalone Apps
Sync with server.
Access from anywhere.
Share with friends.
Save in your account.
Upload on server as
backup. Access from
anywhere.
Search for online
music, search for song
lyrics. Save
preferences online.
Contacts app
Music Player App
Reminder/notes app
Photo Editing app
Ads
Analytics
Crash reports
Push Notifications
Connecting to Network
Wi-fi: Low cost or free, high speed, large volume of data available.
3g/4g/LTE: Very Expensive, Speed may be high or low, low data volume.
Local Area Network: Connectivity to web may not be available, high speed, free.
Connecting to Internet
Permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Connecting to Internet
Checking connection status:
ConnectivityManager mgr= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
// fetch data
} else {
// display error
}
● Tells about network state
● Also notify about changes in the
state
Getting Data from Server
1. Create URL.
1. Create Connection using “HttpUrlConnection”.
1. Start Connection.
1. Read Data from Connection.
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
Code
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
URL
“A Uniform Resource Locator that identifies the location of an Internet resource.”
http://dailynews.com:8080/directory/file?search
Protocol http
Host Daiynews.com
Port 8080
Path /directory/File
Query search
String myurl = “http://facebook.com”
URL url = new URL(myurl);
HttpUrlConnection
“It is used to send and receive data over the web.”
“Data may be of any length.”
“Can be used to GET and POST data.”
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
Get Data
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
int responseCode = conn.getResponseCode(); // check if responseCode is 200
InputStream is = conn.getInputStream();
String responseData = readResponse(is); // Convert the InputStream into a string
return responseData;
Next..
Why it is important to run network operations on separate thread?
How to show network response in UI?
Parsing different data formats: Json, Images.
Handling common issues.

More Related Content

What's hot

Java Servlets
Java ServletsJava Servlets
Java Servlets
BG Java EE Course
 
Vpn presentation
Vpn presentationVpn presentation
Vpn presentation
Kuldeep Padhiyar
 
Event handling
Event handlingEvent handling
Event handling
Anand Grewal
 
Share File easily between computers using sftp
Share File easily between computers using sftpShare File easily between computers using sftp
Share File easily between computers using sftp
Tushar B Kute
 
Maven Introduction
Maven IntroductionMaven Introduction
Maven Introduction
Sandeep Chawla
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
Dharani Kumar Madduri
 
Java adapter
Java adapterJava adapter
Java adapter
Arati Gadgil
 
Java media framework
Java media frameworkJava media framework
Java media framework
Payal Vishwakarma
 
Content provider in_android
Content provider in_androidContent provider in_android
Content provider in_android
PRITI TELMORE
 
Java RMI Presentation
Java RMI PresentationJava RMI Presentation
Java RMI Presentation
Masud Rahman
 
servlet in java
servlet in javaservlet in java
servlet in java
sowfi
 
Android Intent.pptx
Android Intent.pptxAndroid Intent.pptx
Android Intent.pptx
vishal choudhary
 
SDN-ppt-new
SDN-ppt-newSDN-ppt-new
SDN-ppt-new
Gifty Susan Mani
 
Event handling
Event handlingEvent handling
Event handling
swapnac12
 
Webservices
WebservicesWebservices
Webservices
Gerard Sylvester
 
PHP Project PPT
PHP Project PPTPHP Project PPT
PHP Project PPT
Pankil Agrawal
 
Introduction to APIs (Application Programming Interface)
Introduction to APIs (Application Programming Interface) Introduction to APIs (Application Programming Interface)
Introduction to APIs (Application Programming Interface)
Vibhawa Nirmal
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
Scott Leberknight
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
kamal kotecha
 
virtual hosting and configuration
virtual hosting and configurationvirtual hosting and configuration
virtual hosting and configuration
HAMZA AHMED
 

What's hot (20)

Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Vpn presentation
Vpn presentationVpn presentation
Vpn presentation
 
Event handling
Event handlingEvent handling
Event handling
 
Share File easily between computers using sftp
Share File easily between computers using sftpShare File easily between computers using sftp
Share File easily between computers using sftp
 
Maven Introduction
Maven IntroductionMaven Introduction
Maven Introduction
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
 
Java adapter
Java adapterJava adapter
Java adapter
 
Java media framework
Java media frameworkJava media framework
Java media framework
 
Content provider in_android
Content provider in_androidContent provider in_android
Content provider in_android
 
Java RMI Presentation
Java RMI PresentationJava RMI Presentation
Java RMI Presentation
 
servlet in java
servlet in javaservlet in java
servlet in java
 
Android Intent.pptx
Android Intent.pptxAndroid Intent.pptx
Android Intent.pptx
 
SDN-ppt-new
SDN-ppt-newSDN-ppt-new
SDN-ppt-new
 
Event handling
Event handlingEvent handling
Event handling
 
Webservices
WebservicesWebservices
Webservices
 
PHP Project PPT
PHP Project PPTPHP Project PPT
PHP Project PPT
 
Introduction to APIs (Application Programming Interface)
Introduction to APIs (Application Programming Interface) Introduction to APIs (Application Programming Interface)
Introduction to APIs (Application Programming Interface)
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
virtual hosting and configuration
virtual hosting and configurationvirtual hosting and configuration
virtual hosting and configuration
 

Similar to Android Connecting to Internet

Communication in android
Communication in androidCommunication in android
Communication in android
eleksdev
 
INFORMATION TECHNOLOGY FOR MANAGERS PRESENTATION BY AMAN DWIVEDI
INFORMATION TECHNOLOGY FOR MANAGERS PRESENTATION BY AMAN DWIVEDIINFORMATION TECHNOLOGY FOR MANAGERS PRESENTATION BY AMAN DWIVEDI
INFORMATION TECHNOLOGY FOR MANAGERS PRESENTATION BY AMAN DWIVEDI
Aman Dwivedi
 
Web Technologies Notes - TutorialsDuniya.pdf
Web Technologies Notes - TutorialsDuniya.pdfWeb Technologies Notes - TutorialsDuniya.pdf
Web Technologies Notes - TutorialsDuniya.pdf
Raghunathan52
 
Web Technologies Notes - TutorialsDuniya.pdf
Web Technologies Notes - TutorialsDuniya.pdfWeb Technologies Notes - TutorialsDuniya.pdf
Web Technologies Notes - TutorialsDuniya.pdf
Raghunathan52
 
Internet
InternetInternet
Internet
manochitra10
 
abdelwahab alsammak_Chapter 1-Internet concepts.ppt
abdelwahab alsammak_Chapter 1-Internet concepts.pptabdelwahab alsammak_Chapter 1-Internet concepts.ppt
abdelwahab alsammak_Chapter 1-Internet concepts.ppt
David319172
 
Sai internet ppt
Sai   internet ppt Sai   internet ppt
Sai internet ppt
Sai Swamy
 
Going Online
Going OnlineGoing Online
Going Online
allsaintstech
 
Going Online
Going OnlineGoing Online
Going Online
cocon5
 
The internet
The internetThe internet
The internet
Aniqa Zai
 
unit 1(chapter1).pdf
unit 1(chapter1).pdfunit 1(chapter1).pdf
unit 1(chapter1).pdf
ArunArun542872
 
Lecture 1- Introduction to Computers and the Internet.pptx
Lecture 1- Introduction to Computers and the Internet.pptxLecture 1- Introduction to Computers and the Internet.pptx
Lecture 1- Introduction to Computers and the Internet.pptx
RemyaTom2
 
unit oe 1.pdf
unit oe 1.pdfunit oe 1.pdf
unit oe 1.pdf
wondimagegndesta
 
Internet and services by Jaspinder Singh
Internet and services by Jaspinder SinghInternet and services by Jaspinder Singh
Internet and services by Jaspinder Singh
sekhonkittu
 
Http_Protocol.pptx
Http_Protocol.pptxHttp_Protocol.pptx
Http_Protocol.pptx
Abshar Fatima
 
Acsess and Use Internet To access and use
Acsess and Use Internet To access and  useAcsess and Use Internet To access and  use
Acsess and Use Internet To access and use
TsegabrehanZerihun
 
Intrenet and its uses
Intrenet and its usesIntrenet and its uses
Intrenet and its uses
Raj Parekh
 
Network Basics & Internet
Network Basics & InternetNetwork Basics & Internet
Network Basics & Internet
VNSGU
 
How Internet Work
How Internet WorkHow Internet Work
How Internet Work
trendy updates
 
HTML CSS web engineering slides topics
HTML CSS web engineering slides topicsHTML CSS web engineering slides topics
HTML CSS web engineering slides topics
Salman Khan
 

Similar to Android Connecting to Internet (20)

Communication in android
Communication in androidCommunication in android
Communication in android
 
INFORMATION TECHNOLOGY FOR MANAGERS PRESENTATION BY AMAN DWIVEDI
INFORMATION TECHNOLOGY FOR MANAGERS PRESENTATION BY AMAN DWIVEDIINFORMATION TECHNOLOGY FOR MANAGERS PRESENTATION BY AMAN DWIVEDI
INFORMATION TECHNOLOGY FOR MANAGERS PRESENTATION BY AMAN DWIVEDI
 
Web Technologies Notes - TutorialsDuniya.pdf
Web Technologies Notes - TutorialsDuniya.pdfWeb Technologies Notes - TutorialsDuniya.pdf
Web Technologies Notes - TutorialsDuniya.pdf
 
Web Technologies Notes - TutorialsDuniya.pdf
Web Technologies Notes - TutorialsDuniya.pdfWeb Technologies Notes - TutorialsDuniya.pdf
Web Technologies Notes - TutorialsDuniya.pdf
 
Internet
InternetInternet
Internet
 
abdelwahab alsammak_Chapter 1-Internet concepts.ppt
abdelwahab alsammak_Chapter 1-Internet concepts.pptabdelwahab alsammak_Chapter 1-Internet concepts.ppt
abdelwahab alsammak_Chapter 1-Internet concepts.ppt
 
Sai internet ppt
Sai   internet ppt Sai   internet ppt
Sai internet ppt
 
Going Online
Going OnlineGoing Online
Going Online
 
Going Online
Going OnlineGoing Online
Going Online
 
The internet
The internetThe internet
The internet
 
unit 1(chapter1).pdf
unit 1(chapter1).pdfunit 1(chapter1).pdf
unit 1(chapter1).pdf
 
Lecture 1- Introduction to Computers and the Internet.pptx
Lecture 1- Introduction to Computers and the Internet.pptxLecture 1- Introduction to Computers and the Internet.pptx
Lecture 1- Introduction to Computers and the Internet.pptx
 
unit oe 1.pdf
unit oe 1.pdfunit oe 1.pdf
unit oe 1.pdf
 
Internet and services by Jaspinder Singh
Internet and services by Jaspinder SinghInternet and services by Jaspinder Singh
Internet and services by Jaspinder Singh
 
Http_Protocol.pptx
Http_Protocol.pptxHttp_Protocol.pptx
Http_Protocol.pptx
 
Acsess and Use Internet To access and use
Acsess and Use Internet To access and  useAcsess and Use Internet To access and  use
Acsess and Use Internet To access and use
 
Intrenet and its uses
Intrenet and its usesIntrenet and its uses
Intrenet and its uses
 
Network Basics & Internet
Network Basics & InternetNetwork Basics & Internet
Network Basics & Internet
 
How Internet Work
How Internet WorkHow Internet Work
How Internet Work
 
HTML CSS web engineering slides topics
HTML CSS web engineering slides topicsHTML CSS web engineering slides topics
HTML CSS web engineering slides topics
 

More from Paramvir Singh

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
Paramvir Singh
 
AI in image recognition
AI in image recognition AI in image recognition
AI in image recognition
Paramvir Singh
 
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
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
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
Paramvir Singh
 
Android Session 6 - UI Part 1
Android Session 6 - UI Part 1Android Session 6 - UI Part 1
Android Session 6 - UI Part 1
Paramvir Singh
 
Android ui part 2
Android ui part 2Android ui part 2
Android ui part 2
Paramvir Singh
 
Android Connecting to internet Part 2
Android  Connecting to internet Part 2Android  Connecting to internet Part 2
Android Connecting to internet Part 2
Paramvir Singh
 
Android Starting App Development
Android  Starting App DevelopmentAndroid  Starting App Development
Android Starting App Development
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
 
Clean code, Better coding practices
Clean code, Better coding practicesClean code, Better coding practices
Clean code, Better coding practices
Paramvir Singh
 
Android enterprise application development
Android enterprise application developmentAndroid enterprise application development
Android enterprise application development
Paramvir Singh
 
Near field communication
Near field communicationNear field communication
Near field communication
Paramvir Singh
 

More from Paramvir Singh (13)

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 Session 6 - UI Part 1
Android Session 6 - UI Part 1Android Session 6 - UI Part 1
Android Session 6 - UI Part 1
 
Android ui part 2
Android ui part 2Android ui part 2
Android ui part 2
 
Android Connecting to internet Part 2
Android  Connecting to internet Part 2Android  Connecting to internet Part 2
Android Connecting to internet Part 2
 
Android Starting App Development
Android  Starting App DevelopmentAndroid  Starting App Development
Android Starting App Development
 
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
 
Android enterprise application development
Android enterprise application developmentAndroid enterprise application development
Android enterprise application development
 
Near field communication
Near field communicationNear field communication
Near field communication
 

Android Connecting to Internet

  • 2. Today’s agenda Why Connect to Internet? How to Connect to Internet? Getting Data from Server.
  • 3. Why Connect to Internet? Two modes of apps: ● Standalone apps - Reminder/notes app, Photo editing app, Contacts app, Music Player app. ● Connected apps - Chatting app, Food ordering app, News app, Banking apps, Maps app. Standalone apps More Power
  • 4. Standalone Apps Sync with server. Access from anywhere. Share with friends. Save in your account. Upload on server as backup. Access from anywhere. Search for online music, search for song lyrics. Save preferences online. Contacts app Music Player App Reminder/notes app Photo Editing app Ads Analytics Crash reports Push Notifications
  • 5. Connecting to Network Wi-fi: Low cost or free, high speed, large volume of data available. 3g/4g/LTE: Very Expensive, Speed may be high or low, low data volume. Local Area Network: Connectivity to web may not be available, high speed, free.
  • 6. Connecting to Internet Permissions: <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  • 7. Connecting to Internet Checking connection status: ConnectivityManager mgr= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { // fetch data } else { // display error } ● Tells about network state ● Also notify about changes in the state
  • 8. Getting Data from Server 1. Create URL. 1. Create Connection using “HttpUrlConnection”. 1. Start Connection. 1. Read Data from Connection. URL url = new URL(myurl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); InputStream is = conn.getInputStream();
  • 9. Code URL url = new URL(myurl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); InputStream is = conn.getInputStream();
  • 10. URL “A Uniform Resource Locator that identifies the location of an Internet resource.” http://dailynews.com:8080/directory/file?search Protocol http Host Daiynews.com Port 8080 Path /directory/File Query search String myurl = “http://facebook.com” URL url = new URL(myurl);
  • 11. HttpUrlConnection “It is used to send and receive data over the web.” “Data may be of any length.” “Can be used to GET and POST data.” HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  • 12. Get Data URL url = new URL(myurl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.connect(); int responseCode = conn.getResponseCode(); // check if responseCode is 200 InputStream is = conn.getInputStream(); String responseData = readResponse(is); // Convert the InputStream into a string return responseData;
  • 13. Next.. Why it is important to run network operations on separate thread? How to show network response in UI? Parsing different data formats: Json, Images. Handling common issues.