MC 9 8A Shubham
MC 9 8A Shubham
PO5, PSO1,
PS02
Practical No: 9
AIM: ANDROID PROGRAM BASED ON REST API.
A) CREATE A BASIC APPLICATION THAT ALLOWS YOU TO
DOWNLOAD HTML FROM A GIVEN WEB PAGE USING
HTTPURLCONNECTION.
B) CREATE AN APPLICATION TO PARSE THE DATA USING JSON OBJECT
METHODS AND SET IT IN THE TEXT VIEW’S. (EMPLOYEE NAME AND SALARY
STORED IN JSON FORMAT).
THEORY:
1. What is REST?
● Stateless: Each request from a client to the server must contain all the information
needed to process the request. The server does not store the client’s state between
requests.
● Client-Server Architecture: There is a clear separation between the client (frontend)
and server (backend). The client requests resources, and the server responds with the
necessary data.
● Uniform Interface: REST defines a uniform interface for interacting with resources.
This is typically based on HTTP and uses standard methods like GET, POST, PUT,
DELETE, and PATCH.
● Resource-based: In REST, everything is considered a resource, and these resources
are identified using URIs (Uniform Resource Identifiers).
● Representation: Resources are usually represented in formats like JSON, XML, or
HTML. JSON is the most commonly used format because of its lightweight nature.
● GET: Used to retrieve a resource. It’s a read-only operation and should not have
side effects.
○ Example: GET /users retrieves a list of users.
● POST: Used to create a new resource on the server. It often involves sending data to the
server (e.g., via JSON).
○ Example: POST /users creates a new user.
● PUT: Used to update or replace an existing resource. The entire resource is sent to
update it.
○ Example: PUT /users/1 updates the details of user with ID 1.
● PATCH: Used to partially update a resource. Unlike PUT, only the fields that need to
be updated are sent.
○ Example: PATCH /users/1 updates part of user 1’s information.
● DELETE: Used to delete a resource.
○ Example: DELETE /users/1 deletes user 1.
4. Resource Representation
A resource in REST is typically represented using a common format such as JSON, XML, or
HTML. JSON (JavaScript Object Notation) is the most popular format due to its simplicity,
readability, and ease of use in web applications.
{
"id": 1,
"name": "John Doe",
"email": "[email protected]"
}
The server responds to client requests by providing these representations in a standardized format,
enabling the client to understand and process the data.
In REST API design, resources are identified by URIs (Uniform Resource Identifiers), and
these URIs are structured based on a logical hierarchy. RESTful URLs should be intuitive,
descriptive, and use nouns rather than verbs.
● Scalability: The stateless nature of REST makes it easy to scale web services.
● Simplicity: REST APIs leverage standard HTTP methods and are easy to understand
and implement.
● Flexibility: REST can return data in different formats (e.g., JSON, XML), making it
suitable for a wide variety of applications (web, mobile, etc.).
● Separation of Client and Server: REST APIs provide a clear separation between the
client and server, allowing independent development on both sides.
CODE:
androidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:id="@+id/main">
<TextView
android:layout_width="match_p
arent"
android:layout_height="wrap_co
ntent" android:text="HTML
Fetcher"
android:textAlignment="center"
android:textSize="40dp"
android:fontFamily="serif-mono
space" android:textStyle="bold"
android:layout_margin="30dp"/
>
<EditText
android:id="@+id/urlInput
"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter URL"
android:inputType="textUri"
android:textSize="20dp"
android:layout_marginBottom="20dp"/>
<Button
android:id="@+id/fetchButt
on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fetch HTML"
android:textSize="23dp"
android:layout_gravity="center_horizont
al"
android:layout_marginBottom="20dp"/>
<ScrollView
android:layout_width="match_p
arent"
android:layout_height="wrap_co
ntent"
android:fillViewport="true">
<TextView
android:id="@+id/htmlOutput"
android:layout_width="match_p
arent"
android:layout_height="wrap_co
ntent"
android:paddingTop="10dp"
android:textSize="14sp" />
</ScrollView>
</LinearLayout
>
MainActivity.java
package com.example.mcpractical9;
import
androidx.appcompat.app.AppCompatActivit
y; import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextView;
import java.io.BufferedReader;
import
java.io.InputStreamReader;
import
java.net.HttpURLConnection;
import java.net.URL;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
urlInput = findViewById(R.id.urlInput);
htmlOutput =
findViewById(R.id.htmlOutput);
Button fetchButton = findViewById(R.id.fetchButton);
fetchButton.setOnClickListener(new
View.OnClickListener() { @Override
public void onClick(View v) {
String urlString =
urlInput.getText().toString(); new
FetchHtmlTask().execute(urlString);
}
});
}
@Override
protected void onPostExecute(String
result) { htmlOutput.setText(result);
}
}
}
OUTPUT:
B) CREATE AN APPLICATION TO PARSE THE DATA USING JSON OBJECT
METHODS AND SET IT IN THE TEXT VIEW’S. (EMPLOYEE NAME AND SALARY
STORED IN JSON FORMAT).
CODE:
Activity_main.xml
<TextView
android:layout_width="match_p
arent"
android:layout_height="wrap_co
ntent" android:text="JSON
Parser"
android:textAlignment="center"
android:textSize="40dp"
android:fontFamily="serif-mono
space" android:textStyle="bold"
android:layout_margin="30dp"/
>
<TextView
android:id="@+id/jsonStringVie
w"
android:layout_width="wrap_co
ntent"
android:layout_height="wrap_co
ntent" android:text="JSON
String: "
android:paddingBottom="10dp"
android:textSize="20dp" />
<Button
android:id="@+id/parseBut
ton"
android:layout_width="match_parent"
android:layout_height="wrap_co
ntent" android:text="Parse
JSON"
android:textSize="23dp"/>
<TextView
android:id="@+id/employeeName"
android:layout_width="wrap_conte
nt"
android:layout_height="wrap_conte
nt" android:text="Employee Name:
" android:paddingTop="20dp"
android:textSize="23dp"
android:layout_marginBottom="20
dp"/>
<TextView
android:id="@+id/employeeSala
ry"
android:layout_width="wrap_co
ntent"
android:layout_height="wrap_co
ntent" android:text="Employee
Salary: "
android:textSize="23dp"/>
</LinearLayout>
MainActivity.java
package com.example.mcpractical9;
import
androidx.appcompat.app.AppCompatActivit
y; import android.os.Bundle;
import android.view.View;
import
android.widget.Button;
import
android.widget.TextView;
import
org.json.JSONException;
import
org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
private TextView employeeName, employeeSalary,
jsonStringView; @Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
employeeName = findViewById(R.id.employeeName);
employeeSalary =
findViewById(R.id.employeeSalary);
jsonStringView =
findViewById(R.id.jsonStringView); Button
parseButton =
findViewById(R.id.parseButton);
} catch (JSONException e) {
e.printStackTrace();
employeeName.setText("Error parsing JSON");
employeeSalary.setText("");
}
}
}
OUTPUT:
CONCLUSION:
REST APIs are a standard way to build scalable, flexible, and lightweight services. They allow
different systems and platforms to communicate via HTTP, using common methods such as GET,
POST, PUT, and DELETE. With the rise of web, mobile, and cloud applications, REST APIs have
become a vital tool for enabling seamless and efficient communication between clients and
servers.