0% found this document useful (0 votes)
3 views

Networking HTTP JSON

Uploaded by

Zarish Saif
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Networking HTTP JSON

Uploaded by

Zarish Saif
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

NETWORKING, HTTP,

JSON SERVICES
NETWORKING
Networking plays a crucial role in modern Android development, enabling
applications to communicate with remote servers and consume data from web services.
1. Data Retrieval from Remote Sources
2. Integration with Web Services
3. Cloud Connectivity
4. Data Synchronization and Offline Access
5. Push Notifications
6. User Interaction and Collaboration
7. Analytics and Monitoring
UNDERSTANDING
NETWORKING BASICS
Networking concepts: Familiarize yourself with concepts like HTTP protocol,
RESTful APIs, and JSON (JavaScript Object Notation).
HTTP methods: Learn about common HTTP methods such as GET, POST, PUT,
and DELETE, and their usage in web service communication.
Asynchronous operations: Understand the importance of performing network
operations asynchronously to avoid blocking the main UI thread.
ESTABLISHING AN HTTP
CONNECTION
Permissions setup: Configure necessary permissions in the AndroidManifest.xml
file to allow internet access for your application.

HttpURLConnection: Explore the built-in HttpURLConnection class for making


HTTP requests in Android.

Third-party libraries: Consider using popular libraries like OkHttp or Retrofit for
simplified and efficient HTTP communication.
CONSUMING WEB SERVICES
USING HTTP
Sending GET requests: Implement logic to send HTTP GET requests to retrieve
data from remote servers.

Handling response: Process the HTTP response received from the server, including
parsing XML or JSON data.

Error handling: Implement mechanisms to handle errors gracefully, such as


network errors or server-side issues.
CONSUMING JSON SERVICES
IN ANDROID
JSON basics: Understand the structure and syntax of JSON (JavaScript Object
Notation) for representing data.

Parsing JSON: Learn how to parse JSON responses from web services into usable
data structures using libraries.

Data binding: Explore techniques for binding JSON data to UI elements, such as
RecyclerView or ListView, to display dynamic content.
ESTABLISHING AN
HTTP CONNECTION
ESTABLISHING AN HTTP
CONNECTION
Add Internet Permission to AndroidManifest.xml
Ensure your application has the necessary permissions to access the internet by
adding the following line to your AndroidManifest.xml file within the <manifest>
tag;
<uses-permission android:name="android.permission.INTERNET" />

Perform HTTP Request Asynchronously


To prevent blocking the main UI thread, perform HTTP requests asynchronously
using either AsncTask, Thread, or Coroutine. Here's an example using AsyncTask;
ESTABLISHING AN HTTP
CONNECTION
public class HttpAsyncTask extends AsyncTask<String, sb.append(line);
Void, String> { }
@Override result = sb.toString();
protected String doInBackground(String... params) { urlConnection.disconnect();
String urlString = params[0]; } catch (Exception e) {
String result = ""; Log.e("HttpAsyncTask", "Error: " +
try { e.getMessage());
URL url = new URL(urlString); }
HttpURLConnection urlConnection = return result;
(HttpURLConnection) url.openConnection(); }
InputStream in = new
BufferedInputStream(urlConnection.getInputStream()); @Override
BufferedReader reader = new protected void onPostExecute(String result) {
BufferedReader(new InputStreamReader(in));
// Process the HTTP response here
StringBuilder sb = new StringBuilder();
}
String line;
}
while ((line = reader.readLine()) != null) {
ESTABLISHING AN HTTP
CONNECTION
Execute the AsyncTask Handle the HTTP Response
Instantiate and execute the HttpAsyncTask In the onPostExecute() method of the
class with the URL you want to connect to; AsyncTask, you can process the HTTP
response;
String url = "https://api.example.com/data";
@Override
HttpAsyncTask task = new HttpAsyncTask();
protected void onPostExecute(String result) {
task.execute(url);
// Process the HTTP response here
Log.d("HttpAsyncTask", "Response: " +
result);
}
CONSUMING WEB
SERVICES USING HTTP
ESTABLISHING AN HTTP
CONNECTION
Add Internet Permission to AndroidManifest.xml
Ensure your application has the necessary permissions to access the internet by
adding the following line to your AndroidManifest.xml file within the <manifest>
tag.

Perform HTTP Request Asynchronously


To prevent blocking the main UI thread, perform HTTP requests asynchronously
using either AsyncTask, Thread, or Coroutine. Here's an example using AsyncTask;
ESTABLISHING AN HTTP
CONNECTION
public class HttpAsyncTask extends BufferedReader reader = new }
AsyncTask<String, Void, String> { BufferedReader(new
InputStreamReader(in)); return result;
@Override
StringBuilder sb = new }
protected String StringBuilder();
doInBackground(String... params) {
String line; @Override
String urlString = params[0];
while ((line = reader.readLine()) ! protected void onPostExecute(String
String result = ""; = null) { result) {
try { sb.append(line); // Process the HTTP response here
URL url = new URL(urlString); } Log.d("HttpAsyncTask", "Response:
HttpURLConnection result = sb.toString(); " + result);
urlConnection = (HttpURLConnection)
url.openConnection(); urlConnection.disconnect(); }

InputStream in = new } catch (Exception e) { }


BufferedInputStream(urlConnection.getIn
putStream()); Log.e("HttpAsyncTask", "Error: "
ESTABLISHING AN HTTP
CONNECTION
Execute the AsyncTask
Instantiate and execute the HttpAsyncTask class with the URL you want to connect to;
String url = "https://api.example.com/data";
HttpAsyncTask task = new HttpAsyncTask();
task.execute(url);

Handle the HTTP Response


In the onPostExecute() method of the AsyncTask, you can process the HTTP response;
@Override
protected void onPostExecute(String result) {
// Process the HTTP response here
Log.d("HttpAsyncTask", "Response: " + result);
CONSUMING JSON
SERVICES
JSON
JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for
humans to read and write and easy for machines to parse and generate. It is based on a subset
of the JavaScript programming language, but it is language-independent, meaning it can be
used with any programming language.

JSON consists of key-value pairs, where each key is a string and each value can be a string,
number, boolean, array, or another JSON object. The basic syntax of JSON is similar to that
of JavaScript object literals.

JSON is commonly used for transmitting data between a server and a client in web
applications, as well as for storing configuration data and exchanging information between
different systems. It is widely supported by programming languages and frameworks,
making it a popular choice for data serialization and communication.
JSON SERVICES IN ANDROID
JSON services in Android typically refer to web services that use JSON (JavaScript
Object Notation) as the data interchange format.
These services allow Android applications to communicate with remote servers over
the internet and exchange data in JSON format.
JSON SERVICES USAGE
Fetching Data from APIs: Many web APIs (Application Programming Interfaces) provide data in
JSON format. Android applications can make HTTP requests to these APIs, receive JSON responses,
and parse the data to display it in the app.

Sending Data to Servers: Android apps can also send data to servers using JSON. For example,
when submitting a form or sending user-generated content, the app can serialize the data into JSON
format and send it as part of an HTTP request to the server.

Interacting with Backend Systems : JSON services enable Android apps to interact with
backend systems such as databases, content management systems, or other web services. The app can
send requests to perform CRUD (Create, Read, Update, Delete) operations on data stored on the server.

Authentication and Authorization : JSON services are often used for handling authentication
and authorization in Android apps. The app can send credentials (e.g., username and password) to the
CONSUMING JSON SERVICES
To consume JSON services in Android, you'll typically make an HTTP request to a
server that returns JSON data, parse the JSON response, and then use the data in
your application.

Add Internet Permission to AndroidManifest.xml


Ensure your application has the necessary permissions to access the internet by
adding the following line to your AndroidManifest.xml file within the <manifest>
tag;
<uses-permission android:name="android.permission.INTERNET" />
Perform HTTP Request Asynchronously while ((line = reader.readLine()) != null) {
To prevent blocking the main UI thread, perform sb.append(line);
HTTP requests asynchronously using either }
AsyncTask, Thread, or Coroutine. Here's an example result = sb.toString();
using AsyncTask; urlConnection.disconnect();
} catch (Exception e) {
public class HttpAsyncTask extends AsyncTask<String,
Log.e("HttpAsyncTask", "Error: " +
Void, String> {
e.getMessage());
@Override
}
protected String doInBackground(String... params) {
return result;
String urlString = params[0];
}
String result = "";
@Override
try {
protected void onPostExecute(String result) {
URL url = new URL(urlString);
// Process the JSON response here
HttpURLConnection urlConnection =
Log.d("HttpAsyncTask", "Response: " + result);
(HttpURLConnection) url.openConnection();
try {
InputStream in = new
BufferedInputStream(urlConnection.getInputStream()); JSONObject jsonObject = new JSONObject(result);
BufferedReader reader = new BufferedReader(new // Parse JSON data and use it in your application
InputStreamReader(in)); } catch (JSONException e) {
StringBuilder sb = new StringBuilder(); Log.e("HttpAsyncTask", "Error parsing JSON: " +
String line; e.getMessage());
Execute the AsyncTask
Instantiate and execute the HttpAsyncTask class with the URL you want to connect to;
String url = "https://api.example.com/data";
HttpAsyncTask task = new HttpAsyncTask();
task.execute(url);

Handle the JSON Response


In the onPostExecute() method of the AsyncTask, parse the JSON response and use
the data in your application;
@Override
protected void onPostExecute(String result) {
// Process the JSON response here
Log.d("HttpAsyncTask", "Response: " + result);
try {
JSONObject jsonObject = new JSONObject(result);
// Parse JSON data and use it in your application
} catch (JSONException e) {
Log.e("HttpAsyncTask", "Error parsing JSON: " + e.getMessage());
}

You might also like