0% found this document useful (0 votes)
49 views17 pages

CH 5

This chapter discusses various methods for connecting to networks and the web from an Android mobile application. It covers using HTTP to make requests to web servers, consuming XML and JSON web services, and connecting to a socket server to maintain a persistent connection. It emphasizes the need to perform networking operations asynchronously to avoid blocking the main UI thread. It provides code examples for making HTTP requests, using AsyncTask to offload work to a background thread, and connecting to a socket server.

Uploaded by

Yohannes Dereje
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)
49 views17 pages

CH 5

This chapter discusses various methods for connecting to networks and the web from an Android mobile application. It covers using HTTP to make requests to web servers, consuming XML and JSON web services, and connecting to a socket server to maintain a persistent connection. It emphasizes the need to perform networking operations asynchronously to avoid blocking the main UI thread. It provides code examples for making HTTP requests, using AsyncTask to offload work to a background thread, and connecting to a socket server.

Uploaded by

Yohannes Dereje
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/ 17

Jimma University

JIT Mobile Application


Faculty of computing development
&Informatics

Chapter Five
Communications Via Network
and the Web

1
Chapter Objectives
At the end the chapter students should able to know:

 ➤ How to connect to the web using HTTP

 ➤ How to consume XML web services

 ➤ How to consume JSON web services

 ➤ How to connect to a Socket server

2
Networking in Android
 Android provides robust networking capabilities that
allow developers to communicate with remote servers
using various protocols like HTTP, HTTPS, and
WebSocket.
 Networking operations are typically performed
asynchronously to avoid blocking the main UI thread and
ensure a responsive user interface.
 This blog post will focus on some of them, which are
widely used for data exchange in Android apps.
3
Require permission
 In order to perform network operations in your
application, your manifest must include the following
permissions

4
1. HTTP
 First your application needs the INTERNET permission;
we first add the permission in the AndroidManifest.xml
file.

 Use of the HttpURLConnection object to open an HTTP


connection with a remote URL. You can set all the various
properties of the connection, such as the request method,
and so on.

5
1. HTTP

 Establish connection with the server

6
1. HTTP
 After trying to establish a connection with the server, the
HTTP response code is returned.
 If the connection is established (via the response code
HTTP_OK), then you proceed to get an InputStream
object from the connection:

7
Problem
 Android doesn’t allow you to make call networks on UI
thread.
 To be specific, if you set the android:minSdkVersion
attribute in your AndroidManifest.xml file to a value of 9
or less; your synchronous code will still work in a UI thread
(though not recommended) an Android 3.0 or later
device,.

 However, if the android:minSdkVersion attribute


value is set to 10 or above, your synchronous code will
not work in a UI thread.

8
Android AsyncTask
 AsyncTask is an abstract class in Android that offers us the freedom to
execute demanding tasks in the background while keeping the UI thread light
and the application responsive.
 An asynchronous task is defined by 3 generic types:
1. Params, the type of the parameters sent to the task upon execution.

2. Progress, the type of the progress units published during the


background computation.

3. Result, the type of the result of the background computation.

 Note: all types are always used by an asynchronous task. To mark a type as
unused, simply use the type Void:

9
Android AsyncTask

10
Demo

11
3. consume JSON web services
 JSON (JavaScript Object Notation) is a lightweight data-
interchange format that is easy for humans to read and
write.

 It is also easy for machines to parse and generate.

12
Example

13
Demo

14
4. Using Socket Server
 Sockets programming is a technique through which you
use to establish a connection between a client and a
server .

 If you want your application to maintain a persistent


connection to the server and be notified by the server
whenever changes occur, you need to use a programming
technique known as sockets

15
Demo

16
17

You might also like