0% found this document useful (0 votes)
37 views24 pages

6. Web Application Integration Techniques

This syllabus covers web application integration techniques in Android, focusing on AsyncTask for background operations, communication with web APIs, and JSON data handling. It includes methods for AsyncTask, how to implement web APIs using libraries like Volley, and the use of notifications and telephony services. Additionally, it discusses integrating Google Maps into Android applications and the necessary steps for setting up API keys.

Uploaded by

ved
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)
37 views24 pages

6. Web Application Integration Techniques

This syllabus covers web application integration techniques in Android, focusing on AsyncTask for background operations, communication with web APIs, and JSON data handling. It includes methods for AsyncTask, how to implement web APIs using libraries like Volley, and the use of notifications and telephony services. Additionally, it discusses integrating Google Maps into Android applications and the necessary steps for setting up API keys.

Uploaded by

ved
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
You are on page 1/ 24

Syllabus – Unit 6

Web Application Integration Techniques


Introduction to Async Task,
Communication with Web API
Introduction to JSON data, JSON Parsing,
Implementation of Third-Party Library to
Fetch Network Data, Notification, Telephony
API, Google API.
Introduction of Async Task
• Android AsyncTask going to do background operation on
background thread and update on main thread.
• In android we can’t directly touch background thread to main
thread in android development.
• Asynctask help us to make communication between background
thread to main thread.
Methods of Async Task
• onPreExecute() − Before doing background operation we
should show something on screen like progressbar or any
animation to user. We can directly communicate background
operation using on doInBackground() but for the best practice,
we should call all asyncTask methods .
• doInBackground(Params) − In this method we have to do
background operation on background thread. Operations in this
method should not touch on any mainthread activities or
fragments.
• onProgressUpdate(Progress…) − While doing background
operation, if you want to update some information on UI, we can
use this method.
• onPostExecute(Result) − In this method we can update ui of
background operation result.
Generic types in Async Task
• Type Of Var Arg Params − It contains information about what
type of params used for execution.
• Progress Value − It contains information about progress units.
While doing background operation we can update information
on ui using onProgressUpdate().
• Result Value −It contains information about result type.
Android Async Task Example
• To start an AsyncTask the following snippet must be in the MainActivity
class:
MyTask myTask = new MyTask();
myTask.execute();

In the above snippet we’ve used a sample classname that extends AsyncTask
and execute method is used to start the background thread.

Important note:

● The AsyncTask instance must be created and invoked in the UI thread.


● The methods overridden in the AsyncTask class should never be called.
They’re called automatically
● AsyncTask can be called only once. Executing it again will throw an
exception
How to use a web API from your Android app

● A Web API is an online “application programming


interface” that allows developers to interact with
external services.
● These are the commands that the developer of the
service has determined will be used to access certain
features of their program.
● It is referred to as an interface because a good API
should have commands that make it intuitive to
interact with.
How to use a web API works?

● Most APIs work using either XML or JSON.


● These languages allow us to send and retrieve large
amounts of useful information in the form of objects.
● XML is easy to understand and generally places keys inside
triangle brackets, followed by their values.
● It looks a bit like HTML:
How to use a web API works?
● JSON, on the other hand, stands for “Javascript Object
Notation.”
● It is a short-hand for sending data online. Like XML or a CSV
file, it can be used to send “value/attribute pairs.” Here the
syntax

● These are “data objects” in that they are conceptual entities


(people in this case) that can be described by key/value
pairs. We use these in our Android apps by turning them
into objects just as we normally would, with the use of
classes.
Step to Implementation by using the volley api

Add the dependencies in gradle (module.app)


implementation 'com.android.volley:volley:1.2.1

Implementation 'com.github.bumptech.glide:glide:4.13.2’

Add permission for access of internet


<uses-permission android:name="android.permission.INTERNET"/>
What is JSON?

▪ JSON stands for JavaScript Object Notation.


▪ JSON is lightweight data-interchange format.
▪ JSON is easy to read and write than XML.
▪ JSON is language independent.
▪ JSON supports array, object, string, number and values.
▪ JSON stands for JavaScript Object Notation.It is an independent data
exchange format and is the best alternative for XML.
▪ Android provides four different classes to manipulate JSON data.
▪ These classes are JSONArray, JSONObject, JSONStringer and
JSONTokenizer.
What is JSON?
▪ JSON’s main advantage is that it is a language-independent, and the JSON object
will contain data like a key/value pair.
▪ In general, JSON nodes will start with a square bracket ([) or with a curly
bracket ({).
▪ The square and curly bracket’s primary difference is that the square bracket ([)
represents the beginning of a JSONArray node.
▪ Whereas, the curly bracket ({) represents a JSONObject.
▪ So one needs to call the appropriate method to get the data.
▪ Sometimes JSON data start with [.
▪ We then need to use the getJSONArray() method to get the data.
▪ Similarly, if it starts with {, then we need to use the getJSONobject() method.
Syntax of JSON
{
"users": [
{
"name": "Parul University",
"designation": "Student",
"location": "Gujarat"
},
{
"name": "PET",
"designation": "Student PET",
"location": "Gujarat Vadodara"
}
]
}
JSON Parsing
● To parse the JSON data in android, we need to create an instance of JSONObject
and JSONArray objects with a string that contains JSON data in it.
● Following is the code snippet of parsing the JSON data in android using
JSONObject and JSONArray objects to get the required information from JSON
objects.
JSONObject jObj = new JSONObject(jsonStr);
JSONArray jsonArry = jObj.getJSONArray("users");
for(int i=0;i<jsonArry.length();i++){
HashMap<String,String> user = new HashMap<>();
JSONObject obj = jsonArry.getJSONObject(i);
user.put("name",obj.getString("name"));
user.put("designation",obj.getString("designation"));
user.put("location",obj.getString("location"));
userList.add(user);
}
Notification
Android Notification provides short, timely information about the action
happened in the application, even it is not running. The notification displays the
icon, title and some amount of the content text.

Set Android Notification Properties

The properties of Android notification are set using


NotificationCompat.Builder object. Some of the notification properties are
mentioned below:

● setSmallIcon(): It sets the icon of notification.


● setContentTitle(): It is used to set the title of notification.
● setContentText(): It is used to set the text message.
● setAutoCancel(): It sets the cancelable property of notification.
● setPriority(): It sets the priority of notification.
Notification
Create Notification Builder
NotificationCompat.Builder.build() 🡪 By calling this
we can create Notification Builder.

Setting Notification Properties


Notification builder at least should have the
properties like setSmallIcon(), setContentTitle(),
setContentText().
Notification
Attach Actions
⮚ PendingIntent containing an Intent that starts an Activity
⮚ By calling setContentIntent()

Issue the notification


⮚ By calling NotificationManager.notify() we can send our
notification
Telephony API
● The android.telephony.TelephonyManager class provides information about
the telephony services such as subscriber id, sim serial number, phone network
type etc. Moreover, you can determine the phone state etc.
● https://developer.android.com/reference/android/telephony/TelephonyMana
ger
Google API
• By using Google Maps Android API we can integrate google maps in android
applications to show the location details on map based on our requirements.
• To use google maps in our android applications we need to install Google Play
Services SDK in Android Studio because google made Google Maps API as a part of
Google Play Services SDK.
• To install Google Play Services, open Android Studio Go to Tools menu Android
click SDK Manager, then new window will open in that select SDK Tools tab Select
Google Play Services click OK like as shown below.
Google API
• We need to select for GoogleMapsActivity in the Module selection dialog.
Google API
• Once the project created, Android Studio will open google_maps_api.xml and MapsActivity.java
files in the editor.
• The google_maps_api.xml file will contains instructions to generate a Google Maps API key to
access Google Maps servers.
• Copy the link provided in the google_maps_api.xml file like as shown below.
Google API
• Sign with gmail account to create a key
Google API
• Confirm with you account and country need to selected with the country.
• Enable the check box to get access
Google API
• Get into the Google console and generate the Key for your project.
• Paste the into the string create a separate string name and save the key as value as shown below
Thank You

You might also like