SlideShare a Scribd company logo
Networking
How to manage data from WEB
+RobertoOrgiu
@_tiwiz
+MatteoBonifazi
@mbonifazi
Building for Billions
To succeed in the current market, app must provide a
better experience for users who may be connecting to
slower networks
For media rich applications, BITMAPS are
everywhere.
Check a Device's Network Connection
Network connections
Device has various types of network connections
Before performing network operations, it's good practice to
check the state of network connectivity:
● ConnectivityManager: Answers queries about the state of
network connectivity. It also notifies applications when
network connectivity changes.
● NetworkInfo: Describes the status of a network interface
of a given type (currently either Mobile or Wi-Fi).
Check network connections
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
boolean isWifiConn = networkInfo.isConnected();
networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isMobileConn = networkInfo.isConnected();
Log.d(DEBUG_TAG, "Wifi connected: " + isWifiConn);
Log.d(DEBUG_TAG, "Mobile connected: " + isMobileConn);
To perform network operations your app must
declare the following permission in the
AndroidManifest.xml
Connecting to the Network
Secure Network Communication
Ensure data and information stays safe in the app
● Minimize the amount of sensitive or personal user data that you
transmit over the network.
● Send all network traffic from your app over SSL.
● Consider creating a network security configuration, which allows
your app to trust custom CAs or restrict the set of system CAs
that it trusts for secure communication.
Choose an HTTP Client
● HttpsURLConnection client, which supports TLS, streaming
uploads and downloads, configurable timeouts, IPv6, and
connection pooling.
● Apache client is not supported anymore.Avoid it!
Network Operations on a Separate Thread
Network operations can involve unpredictable delay.To avoid
creating an unresponsive UI, don't perform network operations
on the UI thread.
HttpUrlConnection example (1 / 2)
private String downloadUrl(URL url) throws IOException {
try {
connection = (HttpsURLConnection) url.openConnection();
connection.setReadTimeout(3000);
connection.setConnectTimeout(3000);
connection.setRequestMethod("GET");
// Open communications link (network traffic occurs here).
connection.connect();
….
HttpUrlConnection example (2 / 2)
….
int responseCode = connection.getResponseCode();
if (responseCode != HttpsURLConnection.HTTP_OK) {
throw new IOException("HTTP error code: " + responseCode);
}
InputStream stream = connection.getInputStream();
if (stream != null) {
String result = readStream(stream, ...);
}
} finally { // Close Stream and disconnect HTTPS connection.
if (stream != null) { stream.close(); }
if (connection != null) { connection.disconnect(); }
}
return result;
}
Data exchange
Network Architecture and Data exchange
Read Json answer
String jsonStr = result;
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("contacts");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("id");
String name = c.getString("name");
…..
// Phone node is JSON Object
JSONObject phone = c.getJSONObject("phone");
String mobile = phone.getString("mobile");}
} catch (final JSONException e) {...}
Going forward
Youtube Video
https://www.youtube.com/watch?v=l5mE3Tpjejs
https://www.youtube.com/watch?v=Ecz5WDZoJok
Reference Link
https://developer.android.com/training/basics/network-ops/managing.html
https://developer.android.com/training/basics/network-ops/connecting.html
http://www.slideshare.net/anoochit/slide06-18151934
Is there something easier?
Help from the outside
Retrofit
https://square.github.io/retrofit/
Gson
https://github.com/google/gson
Retrofit (1/3)
public interface GitHubService {
@GET("users/{user}/repos")
Call<List<Repo>> listRepos(@Path("user") String user);
}
Retrofit (2/3)
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.build();
GitHubService service = retrofit.create(GitHubService.class);
Retrofit (3/3)
Call<List<Repo>> repos = service.listRepos("octocat");
Gson
Gson gson = new Gson();
String json = gson.toJson(myRepo);
Repo myRepo = gson.fromJson(jsonString, Repo.class);
Retrofit + Gson
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
GitHubService service = retrofit.create(GitHubService.class);
Retrofit + Converters
● Gson
● Jackson
● Moshi
● Protobuf
● Wire
● Simple XML
● Scalars
Retrofit + Gson + Gradle
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
+MatteoBonifazi
@mbonifazi
Thank You!
+RobertoOrgiu
@_tiwiz

More Related Content

PDF
Android Networking
PPT
SQLITE Android
PDF
Firebase Auth Tutorial
PPT
Intro to Web Application Security
PPTX
Command injection
PDF
Asp.net state management
PPT
PHP variables
PPTX
Web Application
Android Networking
SQLITE Android
Firebase Auth Tutorial
Intro to Web Application Security
Command injection
Asp.net state management
PHP variables
Web Application

What's hot (20)

PPTX
Input Validation
PPTX
ASP.NET Lecture 1
PPTX
android sqlite
PPTX
Http Introduction
PDF
Broken access controls
PDF
Android intents
PDF
Auto scaling
PPTX
Ch 7 data binding
PPTX
JSON: The Basics
PPSX
ADO.NET
PDF
Android resource
PPTX
Linux network file system (nfs)
PDF
Android activities & views
ODP
Apache ppt
PPT
Web Servers (ppt)
PPT
Android lifecycle
PDF
DNS hijacking using cloud providers – No verification needed
PPT
PPT
Introduction to Web Programming - first course
Input Validation
ASP.NET Lecture 1
android sqlite
Http Introduction
Broken access controls
Android intents
Auto scaling
Ch 7 data binding
JSON: The Basics
ADO.NET
Android resource
Linux network file system (nfs)
Android activities & views
Apache ppt
Web Servers (ppt)
Android lifecycle
DNS hijacking using cloud providers – No verification needed
Introduction to Web Programming - first course
Ad

Similar to Android Networking (20)

PPTX
Communication in android
PDF
Data in Transit, Risks in DNS and HTTPS (Khmer Coders)
PDF
Lecture 10 Networking on Mobile Devices
PDF
3 Mobile App Dev Problems - Monospace
PPTX
Android Connecting to Internet
PDF
Mobile apps & Server Apis, the weak link? par Emanuele Pecorari
PDF
Big Trouble in Little Networks, new and improved
PPTX
Performance #4 network
PDF
Web fundamentals
PDF
Android Performance #4: Network
PPTX
Is your mobile app up to speed softwaresymposium
PDF
Android App Development 06 : Network &amp; Web Services
PPT
API Architecture Summit 2014- APIs: A Mobile Developer's Perspective
PDF
11.Open Data Protocol(ODATA)
PPT
Creating Android Apps with PhoneGap
PDF
Architectural considerations when building an API
PPTX
Android Trainning Session 2
PDF
Preparing your web services for Android and your Android app for web services...
PDF
Network
PDF
API Management and Internet of Things
Communication in android
Data in Transit, Risks in DNS and HTTPS (Khmer Coders)
Lecture 10 Networking on Mobile Devices
3 Mobile App Dev Problems - Monospace
Android Connecting to Internet
Mobile apps & Server Apis, the weak link? par Emanuele Pecorari
Big Trouble in Little Networks, new and improved
Performance #4 network
Web fundamentals
Android Performance #4: Network
Is your mobile app up to speed softwaresymposium
Android App Development 06 : Network &amp; Web Services
API Architecture Summit 2014- APIs: A Mobile Developer's Perspective
11.Open Data Protocol(ODATA)
Creating Android Apps with PhoneGap
Architectural considerations when building an API
Android Trainning Session 2
Preparing your web services for Android and your Android app for web services...
Network
API Management and Internet of Things
Ad

More from Matteo Bonifazi (17)

PDF
Invading the home screen
PDF
Engage user with actions
PDF
Kotlin killed Java stars
PDF
Android JET Navigation
PDF
Firebase-ized your mobile app
PDF
Backendless apps
PDF
Android - Saving data
PDF
Android - Displaying images
PDF
Android - Background operation
PDF
Android things intro
PDF
The Firebase tier for your mobile app - DevFest CH
PDF
Engage and retain users in the android world - Droidcon Italy 2016
PDF
UaMobitech - App Links and App Indexing API
PDF
The unconventional devices for the Android video streaming
PDF
Google IO - Five months later
PDF
Video Streaming: from the native Android player to uncoventional devices
PDF
Enlarge your screen
Invading the home screen
Engage user with actions
Kotlin killed Java stars
Android JET Navigation
Firebase-ized your mobile app
Backendless apps
Android - Saving data
Android - Displaying images
Android - Background operation
Android things intro
The Firebase tier for your mobile app - DevFest CH
Engage and retain users in the android world - Droidcon Italy 2016
UaMobitech - App Links and App Indexing API
The unconventional devices for the Android video streaming
Google IO - Five months later
Video Streaming: from the native Android player to uncoventional devices
Enlarge your screen

Recently uploaded (20)

PDF
WebRTC in SignalWire - troubleshooting media negotiation
PPTX
Digital Literacy And Online Safety on internet
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PPTX
Introduction to Information and Communication Technology
PDF
Sims 4 Historia para lo sims 4 para jugar
PPTX
presentation_pfe-universite-molay-seltan.pptx
PDF
Testing WebRTC applications at scale.pdf
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PDF
The Internet -By the Numbers, Sri Lanka Edition
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PPTX
artificial intelligence overview of it and more
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PPTX
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
WebRTC in SignalWire - troubleshooting media negotiation
Digital Literacy And Online Safety on internet
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
Introduction to Information and Communication Technology
Sims 4 Historia para lo sims 4 para jugar
presentation_pfe-universite-molay-seltan.pptx
Testing WebRTC applications at scale.pdf
Unit-1 introduction to cyber security discuss about how to secure a system
Design_with_Watersergyerge45hrbgre4top (1).ppt
The Internet -By the Numbers, Sri Lanka Edition
Slides PDF The World Game (s) Eco Economic Epochs.pdf
artificial intelligence overview of it and more
Job_Card_System_Styled_lorem_ipsum_.pptx
Tenda Login Guide: Access Your Router in 5 Easy Steps
SASE Traffic Flow - ZTNA Connector-1.pdf
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
INTERNET------BASICS-------UPDATED PPT PRESENTATION
Slides PPTX World Game (s) Eco Economic Epochs.pptx
Introuction about ICD -10 and ICD-11 PPT.pptx
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx

Android Networking

  • 1. Networking How to manage data from WEB +RobertoOrgiu @_tiwiz +MatteoBonifazi @mbonifazi
  • 2. Building for Billions To succeed in the current market, app must provide a better experience for users who may be connecting to slower networks
  • 3. For media rich applications, BITMAPS are everywhere.
  • 4. Check a Device's Network Connection
  • 5. Network connections Device has various types of network connections Before performing network operations, it's good practice to check the state of network connectivity: ● ConnectivityManager: Answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. ● NetworkInfo: Describes the status of a network interface of a given type (currently either Mobile or Wi-Fi).
  • 6. Check network connections <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); boolean isWifiConn = networkInfo.isConnected(); networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); boolean isMobileConn = networkInfo.isConnected(); Log.d(DEBUG_TAG, "Wifi connected: " + isWifiConn); Log.d(DEBUG_TAG, "Mobile connected: " + isMobileConn); To perform network operations your app must declare the following permission in the AndroidManifest.xml
  • 8. Secure Network Communication Ensure data and information stays safe in the app ● Minimize the amount of sensitive or personal user data that you transmit over the network. ● Send all network traffic from your app over SSL. ● Consider creating a network security configuration, which allows your app to trust custom CAs or restrict the set of system CAs that it trusts for secure communication.
  • 9. Choose an HTTP Client ● HttpsURLConnection client, which supports TLS, streaming uploads and downloads, configurable timeouts, IPv6, and connection pooling. ● Apache client is not supported anymore.Avoid it! Network Operations on a Separate Thread Network operations can involve unpredictable delay.To avoid creating an unresponsive UI, don't perform network operations on the UI thread.
  • 10. HttpUrlConnection example (1 / 2) private String downloadUrl(URL url) throws IOException { try { connection = (HttpsURLConnection) url.openConnection(); connection.setReadTimeout(3000); connection.setConnectTimeout(3000); connection.setRequestMethod("GET"); // Open communications link (network traffic occurs here). connection.connect(); ….
  • 11. HttpUrlConnection example (2 / 2) …. int responseCode = connection.getResponseCode(); if (responseCode != HttpsURLConnection.HTTP_OK) { throw new IOException("HTTP error code: " + responseCode); } InputStream stream = connection.getInputStream(); if (stream != null) { String result = readStream(stream, ...); } } finally { // Close Stream and disconnect HTTPS connection. if (stream != null) { stream.close(); } if (connection != null) { connection.disconnect(); } } return result; }
  • 13. Network Architecture and Data exchange
  • 14. Read Json answer String jsonStr = result; try { JSONObject jsonObj = new JSONObject(jsonStr); // Getting JSON Array node JSONArray contacts = jsonObj.getJSONArray("contacts"); // looping through All Contacts for (int i = 0; i < contacts.length(); i++) { JSONObject c = contacts.getJSONObject(i); String id = c.getString("id"); String name = c.getString("name"); ….. // Phone node is JSON Object JSONObject phone = c.getJSONObject("phone"); String mobile = phone.getString("mobile");} } catch (final JSONException e) {...}
  • 15. Going forward Youtube Video https://www.youtube.com/watch?v=l5mE3Tpjejs https://www.youtube.com/watch?v=Ecz5WDZoJok Reference Link https://developer.android.com/training/basics/network-ops/managing.html https://developer.android.com/training/basics/network-ops/connecting.html http://www.slideshare.net/anoochit/slide06-18151934
  • 17. Help from the outside Retrofit https://square.github.io/retrofit/ Gson https://github.com/google/gson
  • 18. Retrofit (1/3) public interface GitHubService { @GET("users/{user}/repos") Call<List<Repo>> listRepos(@Path("user") String user); }
  • 19. Retrofit (2/3) Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .build(); GitHubService service = retrofit.create(GitHubService.class);
  • 20. Retrofit (3/3) Call<List<Repo>> repos = service.listRepos("octocat");
  • 21. Gson Gson gson = new Gson(); String json = gson.toJson(myRepo); Repo myRepo = gson.fromJson(jsonString, Repo.class);
  • 22. Retrofit + Gson Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .addConverterFactory(GsonConverterFactory.create()) .build(); GitHubService service = retrofit.create(GitHubService.class);
  • 23. Retrofit + Converters ● Gson ● Jackson ● Moshi ● Protobuf ● Wire ● Simple XML ● Scalars
  • 24. Retrofit + Gson + Gradle compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.google.code.gson:gson:2.8.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0'