Use Internet Resources

Source: Internet
Author: User

You may ask when you can create a web-based application, is there any reason for deciding to create a local, Internet-based alternative version.

 

Creating a fat and thin client application without relying on the entire web-based solution has many benefits:

 

Bandwidth

With limited and expensive bandwidth constraints, static resource slices, layout, and sound are consumers of expensive data on devices. By creating a local application, you can limit bandwidth requirements for data updates only.

Shard Cache

Mobile Network Storage has not reached the general state. Using a browser-based solution, some network connections may cause application interruption. Local applications can cache data to provide as many functions as possible without a real-time connection.

 

❑ Local features

The Android device is not only a platform for running browsers, but also a positioning service, camera hardware, and gravity accelerometer. By creating local applications, you can combine online data with hardware features on devices to provide a richer user experience.

 

Modern mobile devices provide many ways to access the Internet. Android provides three network connection technologies. Each type clearly provides the application layer.

 

Listen GPRS, edge, 3G

Access the mobile network by a service provider that provides a mobile data plan.

 

Wi-Fi

Wi-Fi wireless reception and mobile hotspots are becoming increasingly common.

 

Connect to Internet Resources

 

This book does not cover the details of the work under a specific web service, but it is useful to understand the general principles of connecting networks and obtaining data streams from a remote data source.

 

Before you can access Internet resources, you need to add an Internet uses-Permission node to the application manifest, as shown in the following XML snippet:

 

<Uses-Permission Android: Name = "android. Permission. Internet"/>

 

The following code snippet shows the basic style for opening an Internet Data Stream:

 

String myfeed = getstring (R. String. my_feed );

Try {

URL url = new URL (myfeed );

Urlconnection connection = URL. openconnection ();

Httpurlconnection httpconnection = (httpurlconnection) connection;

Int responsecode = httpconnection. getresponsecode ();

If (responsecode = httpurlconnection. http_ OK ){

Inputstream in = httpconnection. getinputstream ();

[... Process the input stream as required...]

}

}

Catch (malformedurlexception e ){}

Catch (ioexception e ){}

 

Android contains some classes to help you process network communication. They are placed in the java.net. * And android.net. * packages.

 

Next to this chapter, we will provide a complete example of how to obtain and process an Internet seed to obtain a list of earthquakes that can be felt in the last 24 hours.

 

Chapter 2 contains more details about Internet-based communication using the Gtalk service. Chapter 10th describes more information about managing specific Internet connections, including monitoring connection status and configuring Wi-Fi access point connections.

 

Use Internet Resources

 

Android provides many ways to use Internet resources.

 

To the extreme, you can use a webview widget that contains a WebKit-based browser in the activity. On the other hand, you can use the client APIs, such as Google's gdata APIs, to directly process them. Between the two, you can use Java-based XML parsing tools (such as sax or javax) to process remote XML seeds to extract and access data.

 

Parsing XML and instructions for processing specific Web services are not within the scope of this book. That is to say, in the earthquake examples later in this chapter, we provide a complete workable code for parsing XML seeds using the javax class.

 

If you use Internet resources in your program, remember that your data connection depends on the communication technology they can obtain. Edge and GSM are notoriously low-bandwidth, while Wi-Fi connections may be unreliable in mobile phone settings.

 

Optimize user experience by limiting the amount of data transmitted, and ensure that your application is strong enough to cope with network disconnection and bandwidth restrictions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.