Sie sind auf Seite 1von 12

Universitatea Tehnică a Moldovei

Laboratory Work nr.3


Topic: Simple HTTP Client (Web Service Emulation).

Done by student: Untilov Andrei, gr. FAF-151


Verified by: Ciudin Sergiu

Chisinau 2017
1 Tasks

1. To develop an application which will load the data from a Web resource
ex:https://news.yam.md/ro/rss.

2. To develop the possibility to add more rss feeds;

3. To develop the possibility to navigate to the link of the loaded articles;

4. To save locally the data, until the user won’t remove it.
Contents

1 Tasks

2 Introduction 4

3 Theory 4

4 Tasks implementation 5
4.1 Main activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4.2 Connector class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4.3 Feeds download . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4.4 Feeds parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4.5 Feeds storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

5 Conclusion 6

6 Bibliography 7

7 Annex A : Application’s screens 8

8 Annex B : Source Code 10

3
2 Introduction

All HTTP requests have a request line consisting a method name, a request URI and an HTTP
protocol version.The Request-URI is a Uniform Resource Identifier that identifies the resource upon
which to apply the request. HTTP request URIs consist of a protocol scheme, host name, optional port,
resource path, optional query, and optional fragment.[1]

3 Theory

Android, being based on Java Language, uses as a basic HTTP classes provided by the java.net
package ex. java.net.HttpURLConnection.
Uses of this class follow a pattern:
1. Obtain a new HttpURLConnection by calling URL.openConnection() and casting the result to
HttpURLConnection.
2. Prepare the request. The primary property of a request is its URI. Request headers may also include
metadata such as credentials, preferred content types, and session cookies.
3. Optionally upload a request body. Instances must be configured with setDoOutput(true) if they
include a request body. Transmit data by writing to the stream returned by getOutputStream().
4. Read the response. Response headers typically include metadata such as the response body’s content
type and length, modified dates and session cookies. The response body may be read from the stream
returned by getInputStream(). If the response has no body, that method returns an empty stream.[2]
5. Disconnect. Once the response body has been read, the HttpURLConnection should be closed by
calling disconnect(). Disconnecting releases the resources held by a connection so they may be closed
or reused.

4
4 Tasks implementation

4.1 Main activity

It initialize of all the implemented features. When the application is run, the articles stored before in
article.xml file, is loaded into the ListView.(Figure 1)
In this activity are presented the next features: Save data, Load saved data, Clear saved articles, Show
rss feeds list.
For loading the articles from the .xml saved previously file has been implemented in Serialization class,
where has been implemented the methods readXML/writeXML. The implementations are shown in the
Annex A.

4.2 Connector class

The connection to RSS stream is implemented by Connector class, through HttpURLConnection. After
the setup of connection,the request to the web resource is computed by method ”GET”. The source
code is shown in Annex B.

4.3 Feeds download

Using the static method ”connect” of the class Connector , by the url address is configured the connec-
tion. Before the dowload starts, is checked if the connection is successful , by request the response code
of Http url connection (200 is OK) .If connection is successful , then the data is loaded into InputStream
object. The source code is shown in Annex B.

4.4 Feeds parsing

The parsing of feeds is perfomed using XmlPullParser class.Using this class can be easily parsed the
stream of feeds , tag by tag. Moreover, the XmlPullParser can distinguish START TAG , TEXT
and END TAG. This feature gives the possibility to separate the stream of data into ”tagName” and
”tagValue” , and select just the tags that we need. The source code is shown in Annex B.

5
4.5 Feeds storage

Downloaded feeds are sotred into .xml files, which means that the data can not be deleted after appli-
cation’s crash.The serialization is performed by Serializer class ,which needs the model defined by the
class Article. The source code is shown in Annex B.

5 Conclusion

During this laboratory work has been developed an Android application- ’Web Service Emulation’
and had been gained certain skills HTTP communication, and the client-server concept.
Also during the development of this project, had been studied various .XML mapping libraries and has
been picked the most efficient one, for this situation.
The implementation of the Web-Sms sending service has began, but i look forward to complete it, and
get it done, as soon as I find how to work-out the current problems.
Looking forward to learn more android features, and to develop useful and efficient applications with
great UI and rich functionalities.

6
References

[1] Apache Software Foundation https://hc.apache.org/httpcomponents-client-ga/tutorial


/html/fundamentals.htmld5e49

[2] Dzone, The Dzone’s Android Tutorial, https://dzone.com/articles/google-android-tutorial

7
6 Annex A : Application’s screens

Figure 1. Main Activity Figure 4. Data storage

Figure 3. Load stored data Figure 4. Data erase

8
Figure 5. Load stored data after erase.

9
7 Annex B : Source Code

Figure 6. Model class

Figure 7. Serialization methods

10
Figure 8. Connector class

Figure 9. Download data

11
Figure 10. RSS Parsing

12

Das könnte Ihnen auch gefallen