Bài giảng Lập trình cho thiết bị di động - Chương 5: Networking APIsvà multimedia APIs - Trần Duy Thanh

DONG NAI UNIVERSITY OF TECHNOLOGY  
API  
2
1
Networking  
Multimedia  
APIs  
APIs  
1
DONG NAI UNIVERSITY OF TECHNOLOGY  
1. Networking APIs  
1.1 Understanding Mobile Networking  
1.2 Strict Mode with Networking  
1.3 Accessing the Internet (HTTP)  
2
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.1 Understanding Mobile Networking  
➢ Networking on the Android platform is standardized, using  
a combination of powerful yet familiar technologies and  
libraries such as java.net.  
➢ Network implementation is generally straightforward,  
but mobile application developers need to plan for less  
stable connectivity than one might expect in a home or  
office network setting—connectivity depends on the  
location of the users and their devices.  
➢ developer must take extra care when designing network-  
enabled applications  
3
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Strict Mode with Networking  
➢ Strict mode is a method that developers can use to detect  
operations performed on the main thread that should not  
be there.  
➢ API Level 11 expanded upon strict mode in ways that  
impact networking code. By default, if you perform  
network operations on the main thread, your application  
throws an exception, specifically  
android.os.NetworkOnMainThreadException.  
➢ 2 ways to avoid this is to use proper coding techniques  
and put all networking operations on a thread other than  
the main thread (should use AsyncTask class).  
➢ Or call the permitAll() method to skip strict mode entirely  
4
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Strict Mode with Networking  
➢ If you want to use networking in Main Thread, the coding:  
if (android.os.Build.VERSION.SDK_INT > 10) {  
StrictMode.ThreadPolicy policy = new  
StrictMode.ThreadPolicy.Builder()  
.permitAll().build();  
StrictMode.setThreadPolicy(policy);  
}
This is not recommended for production applications.➔  
WHY?  
The next slide you will learn 3 ways to create Thread for networking, to see  
details please click the link below:  
5
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Strict Mode with Networking  
➢ 1 - If not, you must write coding on a Thread other  
At first, this code seems to be a good solution to your problem, as it  
does not block the UI thread. Unfortunately, it violates the single  
thread model: the Android UI toolkit is not thread-safe and must  
always be manipulated on the UI thread. In this piece of code, the  
ImageView is manipulated on a worker thread, which can cause  
really weird problems. Tracking down and fixing such bugs can be  
difficult and time-consuming.  
6
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Strict Mode with Networking  
➢ 2 - If not, you must write coding on a Thread other  
Unfortunately, these classes and methods also tend to make your  
code more complicated and more difficult to read. It becomes even  
worse when your implement complex operations that require  
frequent UI updates.  
7
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Strict Mode with Networking  
➢ 3 - If not, you must write coding on a Thread other  
✓ Android 1.5 offers a new utility class, called AsyncTask, that  
simplifies the creation of long-running tasks that need to  
communicate with the user interface. The goal of AsyncTask  
is to take care of thread management for you.  
8
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.3 Accessing the Internet (HTTP)  
✓ The most common way to transfer data to and from the  
network is to use HTTP. You can use HTTP to encapsulate  
almost any type of data and to secure the data with Secure  
Sockets Layer (SSL)  
Objectives:  
❑Reading Data from the Web  
❑Using HttpURLConnection  
❑Displaying Images from a Network Resource  
❑Retrieving Android Network Status  
9
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.3 Accessing the Internet (HTTP)  
❑Reading Data from the Web  
<uses-permission  
android:name="android.permission.  
INTERNET"/>  
10  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.3 Accessing the Internet (HTTP)  
❑Reading Data from the Web  
11  
DONG NAI UNIVERSITY OF TECHNOLOGY  
❑Cont…  
1.3 Accessing the Internet (HTTP)  
12  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.3 Accessing the Internet (HTTP)  
❑Using HttpURLConnection  
This object to do a little reconnaissance on our URL before  
we transfer too much data. It retrieves some information  
about the resource referenced by the URL object, including  
HTTP status and header information.  
13  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.3 Accessing the Internet (HTTP)  
❑Displaying Images from a Network Resource  
<uses-permission  
android:name="android.permission.  
INTERNET"/>  
14  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.3 Accessing the Internet (HTTP)  
❑Displaying Images from a Network Resource  
15  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.3 Accessing the Internet (HTTP)  
❑Displaying Images from a Network Resource  
16  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.3 Accessing the Internet (HTTP)  
❑Retrieving Android Network Status  
✓ The Android SDK provides utilities for gathering  
information about the current state of the network.This is  
useful to determine whether a network connection is  
even available before trying to use a network resource.  
✓ The ConnectivityManager class provides a number of  
methods to do this  
17  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.3 Accessing the Internet (HTTP)  
❑Retrieving Android Network Status  
Only physical device testing can truly  
reveal these results.  
<uses-permission android:name="android.permission.INTERNET"/>  
<uses-permission  
android:name="android.permission.ACCESS_NETWORK_STATE"/>  
18  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.3 Accessing the Internet (HTTP)  
❑Retrieving Android Network Status  
19  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.3 Accessing the Internet (HTTP)  
❑Retrieving Android Network Status  
20  
Tải về để xem bản đầy đủ
pptx 61 trang baolam 55520 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Lập trình cho thiết bị di động - Chương 5: Networking APIsvà multimedia APIs - Trần Duy Thanh", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pptxbai_giang_lap_trinh_cho_thiet_bi_di_dong_chuong_5_networking.pptx