Get post JSON data and download pictures using httpurlconnection in Android

In Android 6.0, all packages and classes of the Apache HTTP client are marked as deprecated. It is no longer recommended that all HTTP related data requests and submission operations be implemented through the httpurlconnection class. The reality is that many Android developers have always used the Apache HTTP client for data interaction between the Android client and the background http interface, Xiaobian just made an android app with httpurlconnection and accidentally stepped into several pits. To sum up, the most commonly used is to post submit JSON data and get request JSON data through httpurlconnection. In addition, download pictures. Download pictures are divided into displaying progress and not displaying progress. When submitting data involving Chinese, you must first transcode the Chinese into UTF-8 and then submit it after post, otherwise you will always encounter HTTP 400 errors.

1、 Get request JSON data example

2、 Post submit JSON data

3、 Download pictures to show download progress

Summary: when submitting JSON data using httpurlconnection, the encoding method is UTF-8. All Chinese characters must be transcoded to UTF-8 in advance, and then decoded to UTF-8 in the API corresponding to the background server, otherwise an error HTTP 400 will be reported.

The above is the whole content of this article. I hope it will be helpful for everyone to learn Android Software 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
分享
二维码
< <上一篇
下一篇>>