Android uses urlconnection to submit requests

The openconnection () method of the URL will return a urlconnection object that represents the communication connection between the application and the URL. The program can send a request to the URL through the urlconnection instance to read the resources referenced by the URL.

Generally, the following steps are required to create a connection with the URL, send a request and read the resources referenced by the URL: Step 1: create the urlconnection object by calling the openconnection () method of the URL object; Step 2: set urlconnection parameters and common request attributes; Step 3: if you only send a get request, you can use the connect method to establish an actual connection with the remote resource; If you need to send a post request, you need to obtain the output stream corresponding to the urlconnection instance to send the request parameters; Step 4: when the remote resource becomes available, the program can access the header field of the remote resource or read the data of the remote resource through the inflow stream.

The following program demo demonstrates how to send get requests and post requests to a web site and get responses from the web site. This program uses a tool class for get and post requests. The code of this class is as follows:

The logic code of getpostutil.java is as follows:

As can be seen from the above program demo, if you need to send a get request, just call the connect () method of urlconnection to establish the actual connection. If you need to send a post request, you need to obtain the OutputStream of urlconnection, and then output the request parameters to the network. After providing the above tool class for sending get requests and post requests, you can send requests through this tool class in the activity class. The interface of the program contains two buttons. One button is used to send get request and the other button is used to send post request. The program also provides an EditText to display the server's response.

layout/activity_ The main.xml interface layout code is as follows:

The logic code of mainactivity.java is as follows:

The above program demo is used to send get requests and post requests. From the above code, it can be found that with the help of urlconnection class, the application can easily exchange information with the specified site, including sending get requests, post requests, and obtaining the response of the website.

Note: since the program needs to access the Internet, it also needs to authorize the permission to access the Internet in the manifest file androidmanifest.xml:

The screenshot of demo program running effect interface is as follows:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>