Вы находитесь на странице: 1из 89

API

(Application Programming Interface)


STW300CEM Android Applications Development
Prerequisite
Install XAMPP or JSON Server and MongoDB and Postman

For Windows and Mac please download the setup file and install.

For Linux (Download the setup file and follow the instructions to install XAMPP):

1. chmod 755 [package name]


2. ls -l [package name]
3. sudo ./[package name]
What is an API ?
API is the acronym for Application Programming Interface, which is a software
intermediary that allows two applications to talk to each other.
Each time you use an app like Facebook, send an instant message, or check the
weather on your phone, you’re using an API.
Data Interchange format : JSON vs XML
JSON XML
JavaScript Object Notation Extensible Markup Language
Text based format Markup Language
Light-weighted Heavier
Does not support comments and namespaces Supports comments and namespaces
Popular API Example
•Google Maps APIs
•YouTube APIs
•Flickr APIs
•Twitter APIs
•Amazon Product Advertising APIs
Types of REST API client
1. Retrofit
2. Volley
3. OkHttp
4. Android Asynchronous HTTP client
e.t.c
Retrofit
Retrofit is a REST Client for Java and Android. It makes it relatively easy to retrieve and
upload JSON (or other structured data) via a REST based webservice.
In Retrofit you configure which converter is used for the data serialization. Typically
for JSON you use GSon, but you can add custom converters to process XML or other
protocols.
Retrofit uses the OkHttp library for HTTP requests.
Retrofit
Retrofit mainly need three things in android :

1. Retrofit Instance :

• You can create an instance of Retrofit by Retrofit.Builder class. You have to specify
the base url and converter factory at the time of the retrofit instance creation.
Retrofit
2. Model Class
• Retrofit need a POJO class for Sending and Receiving request.
• Retrofit use the POJO class for parse the server Response by using Converter like
Gson , Jackson , Moshi etc.
Retrofit
3. An interface for possible API Call

• The interface contain methods that represents possible API call.


• Each method need a base URL end point annotation that represents the http methods like GET,POST
etc.
• The return type of each of these methods is an instance of Call class.
Add Dependencies in build.gradle

implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
Manifest.xml
Allow your app to access internet

Below API 28, we dont have to


use cleartexttraffic
Making a Request
For sending a request we have to obtain an instance of the interface by make a call to create();
By using the interface instance you can make the needed API call through the interface methods.

Retrofit make the network request and wait for the response on a background thread and deliver the result to
onResponse() or onFailure() methods on main UI thread.
For Testing use this link
http://dummy.restapiexample.com/
Dummy Data in JSON format
Dashboard Activity
1. Display All Employee
(@GET)
Show All Employee XML
Model class
Create an Interface
Main Activity
Output
2. Search Employee by ID (@GET)
Search Employee XML
Interface
@Path : Named replacement
in the URL path.

Search by Id result
empId value
SearchEmployeActivity
We are expecting a single Employee object ,
that is why we are not using List<Employee>
3. Register Employee (@Post)
Create another class named
EmployeeCUD
For Create , Update and Delete
For retrieving date use
Employee class.
use EmployeeCUD class.

Why two classes for Employee Entity ??


EmployeeAPI class
Registration XML
Just to remind . Use EmployeeCUD for insert ,
update and delete.

Registration Activity
Just to remind . Use EmployeeCUD for insert ,
update and delete.
4. Update and Delete (@PUT and @Delete)
XML
Load Employee

Just to remind, use Employee class for retrieving data.


Update Employee
Create EmployeeCUD object

In slide no 32 you have already created this method


Delete Employee
Use alert dialog to promt a message saying
Are you sure you want to delete ?
Custom API
Download API project from the given link below

https://github.com/kiranrana8973/HeroesAPI_WebAPi.git

Use :
1. MongoDB
2. Node.js
Add a Hero for postman
Add another hero : Postman
Post Using Retrofit2
build.gradle : Add dependency for
Retrofit and GSON
Manifest File
Create three packages

For Interface

For Model class

For url class


XML
For Inserting we can use 3 Methods ( For now )

1. Using Class
2. Using @Field
3. Using @FieldMap
1. Using Class
model class
For now properties name should be same as the
JSON values

We will insert image later


Interface
URL class
Activity
Output
Output
2. Using @Field
@FormUrlEncoded and @Field
•Denotes that the request body will use form URL encoding.
•Fields should be declared as parameters and annotated with @Field.
•Requests made with this annotation will have application/x-www-form-urlencoded
MIME type.
•Field names and values will be UTF-8 encoded before being URI-encoded in
accordance to RFC-3986.

NOTE :
Please be aware of the fact, that you can’t use this annotation for GET requests
Interface
Activity
3. Using @FieldMap
Interface
Use HashMap

Passign HashMap object


Browsing image and previewing
click
here
to
browse
Loading image from URL
XML
Activity

1
Another way
2
Browsing image and previewing
click
here
to
browse
Activity
browse only image

After getting image path display image in imageview

This function will return the image path


After writing this code image won't load.
Go to next slide for the solution.
Permission
There are two ways of doing it. Either you take run time permission from the user for
accessing storage which we did in Topic 7 or go to setting and manually enable it.
Uploading image to server using Postman
Uploading and Retrieving Image in
android

Add this function


to create an instance of
Retrofit
Interface
Add ImageResonse class in model
Why ImageResponse class ??

Field Name

After inserting we need imageName i.e filename.


Field Name
Synchronized method not asynchronous.
WHY ??
Uing HashMap. If you don't like this method then
you can use @Field method or @Body method.

Just to remind, we already create


Retrofit instance in Url class. refer slide no 78.
Output
Displaying Heroes using RecyclerView

If you dont know what is RecyclerView please refer


Topic 5.
RcyclerView XML
No need to add image, this
is for testing purpose
Adapter class
Activity XML build.gradle

Вам также может понравиться