Android Networking
-
Upload
matteo-bonifazi -
Category
Internet
-
view
77 -
download
0
Transcript of Android Networking
![Page 1: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/1.jpg)
NetworkingHow to manage data from WEB
+RobertoOrgiu@_tiwiz
+MatteoBonifazi@mbonifazi
![Page 2: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/2.jpg)
Building for BillionsTo succeed in the current market, app must provide a better experience for users who may be connecting to
slower networks
![Page 3: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/3.jpg)
For media rich applications, BITMAPS are everywhere.
![Page 4: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/4.jpg)
Check a Device's Network Connection
![Page 5: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/5.jpg)
Network connectionsDevice 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).
![Page 6: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/6.jpg)
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
![Page 7: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/7.jpg)
Connecting to the Network
![Page 8: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/8.jpg)
Secure Network CommunicationEnsure 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.
![Page 9: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/9.jpg)
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.
![Page 10: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/10.jpg)
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();….
![Page 11: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/11.jpg)
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;}
![Page 12: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/12.jpg)
Data exchange
![Page 13: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/13.jpg)
Network Architecture and Data exchange
![Page 14: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/14.jpg)
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) {...}
![Page 15: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/15.jpg)
Going forward
Youtube Videohttps://www.youtube.com/watch?v=l5mE3Tpjejshttps://www.youtube.com/watch?v=Ecz5WDZoJok
Reference Linkhttps://developer.android.com/training/basics/network-ops/managing.htmlhttps://developer.android.com/training/basics/network-ops/connecting.htmlhttp://www.slideshare.net/anoochit/slide06-18151934
![Page 16: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/16.jpg)
Is there something easier?
![Page 17: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/17.jpg)
Help from the outside
Retrofithttps://square.github.io/retrofit/
Gsonhttps://github.com/google/gson
![Page 18: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/18.jpg)
Retrofit (1/3)
public interface GitHubService { @GET("users/{user}/repos") Call<List<Repo>> listRepos(@Path("user") String user);}
![Page 19: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/19.jpg)
Retrofit (2/3)
Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .build();
GitHubService service = retrofit.create(GitHubService.class);
![Page 20: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/20.jpg)
Retrofit (3/3)
Call<List<Repo>> repos = service.listRepos("octocat");
![Page 21: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/21.jpg)
Gson
Gson gson = new Gson();String json = gson.toJson(myRepo);Repo myRepo = gson.fromJson(jsonString, Repo.class);
![Page 22: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/22.jpg)
Retrofit + Gson
Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .addConverterFactory(GsonConverterFactory.create()) .build();
GitHubService service = retrofit.create(GitHubService.class);
![Page 23: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/23.jpg)
Retrofit + Converters
● Gson● Jackson● Moshi● Protobuf● Wire● Simple XML● Scalars
![Page 24: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/24.jpg)
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'
![Page 25: Android Networking](https://reader033.fdocuments.net/reader033/viewer/2022051502/58abe7bb1a28ab504e8b49a5/html5/thumbnails/25.jpg)
+MatteoBonifazi@mbonifazi
Thank You!
+RobertoOrgiu@_tiwiz