Android Program Development - Network Programming Application Create an Android application and practice the usage of URLConnection. 2. Add ImageView Button two controls to the layout file 3. Add corresponding controls to MainActivity

Network programming applications

1.1 Experimental purpose

Master the application of Android network programming

1.2 Experimental steps

1. Create an Android application and practiceURLConnection usage.

2. Add location informationImageView ButtonPersonal attachment

3. Add the initialization code of the corresponding control in MainActivity

4. Start the tomcat server and copy the images to the webapp/root directory

5. Create another Android application and practice the usage of HttpClient

1.3 实验代码

1. Create a project

2. Add two controls, ImageView Button, to the layout file activity_main.

3. Add the initialization code of the corresponding control in MainActivity

4. Add networking permissions to the AndroidMainfest manifest file

5. Experimental results

2. Start another project to practice the application of HttpClient

1. Add the following code to the activity_main layout file

2. Add the following initialization code to MainActivity

3. Add networking permissions to the AndroidMainfest manifest file

4.Experimental results:

1.4 Experimental Summary

URLConnection interface

(1)URLConnection belongs to the standard interface of JavaAPI and is included in the package java.net. And Android

The platform supports APIs in the java.net package.

The URLConnection object can be obtained through the openConnection() method in the URL.

Represents the communication between the application and the URL. Send a request to the URL through the URLConnection instance and read the URL resource.

The usual steps for using URLConnection are as follows:

  • Create URL object;
  • Create an object by calling the openConnection() method of the URL object;
  • Set the parameters of URLConnection;
  • Use getInputStream() of URLConnection to obtain the input stream; (5) Process the input stream accordingly.

HttpClient interface

The Apache open source organization provides an HttpClient project, which is a simple HTTP client.

Used to send HTTP requests and receive HTTP responses.

The steps for HttpClient to send requests and receive responses are as follows:

  • Create HttpClient object;
  • Create an HttpGet object or HttpPost object;
  • The setEntity() method of the HttpGet object or HttpPost object adds request parameters;
  • Use the execute() method of the HttpClient object to send a request, which returns HttpResponse;
  • Use the getEntity method of HttpResponse to obtain the server response.

In the process of using Android Studio in this experiment,HttpClient and other external packages need to be imported and downloaded in maven to establish dependencies, but there will be Running errors may be due to environmental issues and the package import was not successful, so this lab was completed with eclipse and will continue to be studied in the future.

Guess you like

Origin blog.csdn.net/WZY22502701/article/details/134230590