Android uses the open-source framework AsyncHttpClient to upload files to the server.

Source: Internet
Author: User

Open source is really a good thing. We don't need to recreate the wheel. Many stubborn people always look down on people who use the framework and think that there is no technical content and they should write everything on their own. In fact, the first task in our work is to complete the work, and to complete the work better. These open-source frameworks have been verified and corrected by many people. It is better than writing it by yourself. Don't repeat the wheel. Besides, for users, it is to see whose products are doing well, rather than who has better developer technology.

This AsynchHttpClient has many advantages. In particular, it is very simple to write, so you do not need to access the network to open sub-threads, and modify the interface information to create another handler. As for how to use it, first download the jar package, or download the source code to your src file. Search android from github and you will be able to find it. This is very popular.

Code:

Public void postFile () throws Exception {String path = ""; File file = new File (path); if (file. exists () & file. length ()> 0) {AsyncHttpClient client = new AsyncHttpClient (); RequestParams params = new RequestParams (); params. put ("profile_picture", file); client. post ("http: // 192.168.1.100: 8080/web/UploadFile", params, new AsyncHttpResponseHandler () {@ Overridepublic void onSuccess (int statusCode, Header [] headers, byte [] responseBody) {Toast. makeText (MainActivity. this, "success", Toast. LENGTH_LONG ). show () ;}@ Overridepublic void onFailure (int statusCode, Header [] headers, byte [] responseBody, Throwable error) {Toast. makeText (MainActivity. this, "failed", Toast. LENGTH_LONG ). show () ;}}) ;}else {Toast. makeText (this, "the file does not exist", 1 ). show ();}}


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.