Android development uses httpurlconnection for network programming [Download with source code]

This article gives an example of Android development using httpurlconnection for network programming. Share with you for your reference, as follows:

――HttpURLConnection

Urlconnection can easily exchange information with specified sites. There is a subclass under urlconnection: httpurlconnection. Httpurlconnection is improved on the basis of urlconnection, and some convenient methods for operating HTTP resources are added.

Setrequestmethod (string): set the method to send the request. Getresponsecode(): get the response code of the server. Getresponsemessage(): get the response message of the server

a) Code of get request:

b) Post request code

c) Close connection

Steps to realize multi-threaded download:

a) Create a URL object B) get the size of the resource pointed to by the specified URL object: getcontentlength () C) create an empty file with the same size as the network resource on the local disk d) calculate the application of each thread to download the specified part of the network resource E) create it in turn, and start multiple threads to download the specified part of the network resource

Note the required permissions:

For more instructions on Android permission control, please refer to the complete description of Android manifest functions and permissions

Here, I simply use httpurlconnection for text parsing and image parsing

The programming steps are as follows:

1. Write the layout file first:

2. Implementation of text parsing in mainactivity:

Here we use the get method ~ or the post method~

3. Implementation of image parsing in mainactivity:

This image parsing implements the image cache. When you want to load the image again, you can get the image in the cached file, which can reduce the use of memory~

I put this picture in the directory \ apache-tomcat-7.0.37 \ webapps \ upload on the server. You can download this picture from the server and save it in a file~

4. Finally, load the text and pictures

I won't post the renderings. Just know how to write the code~

The complete mainactivity code is as follows:

Attachment: click here to download the complete example code.

For more Android related content, readers who are interested can view the topics on this site: summary of Android communication mode, introduction and advanced tutorial of Android development, summary of Android debugging skills and solutions to common problems, summary of Android multimedia operation skills (audio, video, recording, etc.), summary of usage of Android basic components Summary of Android view skills, summary of Android layout skills and summary of Android control usage

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>