0% found this document useful (0 votes)
12 views12 pages

Android Network Programming Essentials

This document discusses various aspects of network connectivity and performing network operations in Android applications. It covers checking for network availability, required permissions, and libraries that can be used to simplify network requests like Retrofit. Retrofit allows making REST API requests in a type-safe way by automatically serializing JSON responses to POJO objects. The document provides code snippets for setting up Retrofit by defining an interface for the API and configuring a Retrofit builder with the base URL.

Uploaded by

Dfaiz Aditya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views12 pages

Android Network Programming Essentials

This document discusses various aspects of network connectivity and performing network operations in Android applications. It covers checking for network availability, required permissions, and libraries that can be used to simplify network requests like Retrofit. Retrofit allows making REST API requests in a type-safe way by automatically serializing JSON responses to POJO objects. The document provides code snippets for setting up Retrofit by defining an interface for the API and configuring a Retrofit builder with the base URL.

Uploaded by

Dfaiz Aditya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Network Connectivity & Internet

Dimas Aditya Faiz


1706040031
Accessing the network in Android
Android contains the standard Java network [Link] package which can
be used to access network resources. The base class for HTTP network
access in the [Link] package is the HttpURLConnection class.
Permission to access the network
To access the Internet your application requires the
[Link] permission. On modern Android API
versions, this permission is automatically granted to your application.

You can check it in your [Link] file


Check the network availability
The network on an Android device is not always available you must first
check that are you connected to that network or internet e.t.c. For this
android provides ConnectivityManager class.

We combine the ConnectivityManager with a broadcast receiver to


implement a listener on a network availability with a hadling. An example
of implementation is stated in the next page.
[Link] (code snippet)
Main_activity.kt
MobileApp
Menampilkan snackbar saat koneksi
internet tidak tersedia
Performing Network Operations
After checking that you are connected to the internet, you can perform any
network operation Performing network operations with standard Java API can be
cumbersome. You have to open and close a connections, enable caches and
ensure to perform the network operation in a background thread.

To simplify these operations several popular Open Source libraries are available.
The most popular ones are the following:
● OkHttp for efficient HTTP access
● Retrofit for REST based clients
● Glide for image processing
Retrofit
Retrofit is type-safe REST client for Android and Java which aims to
make it easier to consume RESTful web services

Retrofit automatically serialises the JSON response using a POJO(Plain


Old Java Object) which must be defined in advanced for the JSON
Structure. To serialise JSON we need a converter to convert it into Gson
first. We need to add the following dependencies in our [Link] file.
Making interface API
Retrofit (2)

To make a network request to the REST API with Retrofit, we need to


create an Instance with the [Link] class and then configure it
with BASE_URL
Retrofit builder example

You might also like