Android's most popular network framework (original)

Source: Internet
Author: User

The most important module of the Android program is the network part, how to download data from the network, how to upload the processed data to the network, is often the key link of the Android program.

Android Native provides two types of network access based on HttpClient and HttpURLConnection. The use of these two ways to write network code, need to consider a lot of their own, access to data may be, but if you want to upload local data to the network, according to different web-side interface, the need to organize different data content upload, to the mobile phone side caused a lot of work. There are several fast open source frameworks for Web development that provide us with a great deal of convenience, and they should be the top three of the open source framework for Android networking. 1.https://github.com/loopj/android-async-http                    & nbsp   LOOPJ This open source framework has always been my favorite network framework, and has been using this framework for quite a few of the programs I've written, so much so that I'm tired of the framework.             Too detailed use of the document, too simple to use, it is like a high-level language, customized all the things, you only need to piece together to be able to deal with, so you want to customize more details of the operation, only the hands-on, Instead, it caused some unnecessary trouble.             LOOPJ support for HTTPS in the previous period is not good, and if you want to support HTTPS, it will take a bit of effort.             1.4 Several versions of the upgrade are not good enough for the first part of the code, resulting in my boredom with this framework.                                                      get a picture: 
asynchttpclient client = new asynchttpclient ();
client.get ("Http://example.com/file.png",  new fileasynchttpresponsehandler ( /* Context */ this)  {
@Override
public void&n Bsp;onsuccess (int statuscode, header[] headers, file response)  {
blockquote>//Do something with the file ' response '
}
  construction request parameters:  
 requestparams params = new Requestparams ();p arams.put ("Key", "value");p arams.put ("more"
 , "data");  
 LOOPJ provides a variety of parsing responsehandler about return parameters, which is one reason why I have become less fond of LOOPJ.
2.https://github.com/kevinsawicki/http-request
For LOOPJ, HttpRequest is a simple brute, a request can be covered in a code, do not give any bit of redundant ingredients.
String response = Httprequest.get ("http://google.com"). Body ();
Print the data to the console, or to another stream:
Httprequest.get ("http://google.com"). Receive (System.out);
to ignore HTTPS security:
HttpRequest request = Httprequest.get ("https://google.com");
//accept All certificates
request.trustallcerts ();
//accept All hostnames
Request.trustallhosts ();
For small projects, this kind of network framework is simply a happy choice. 3.https://github.com/square/okhttp is more powerful than LOOPJ and Httprequest,okhttp, but it's a little more complicated to use, but it's my favorite            The feeling of joy.            This is the open source framework for the famous company Square, which is a great shot. The code in my project is posted below:
                    Okhttpclient client = new Okhttpclient ();                    Client.networkinterceptors (). Add (New Stethointerceptor ());                    Requestbody BODY = new Formencodingbuilder (). Add ("Data", "data"). Add ("                            token", GetToken (data))                            . Add ("System", " Sample "). Build ();                    Request Request = new Request.builder ()                            . URL ("Http://sample.com/api/v1")                            . Post (body). Build ();                    Response Response = client.newcall (Request). Execute ();                    Final string reslut = Response.body (). String ();
there is a line of code in the preceding code:
        Client.networkinterceptors (). Add (New Stethointerceptor ());
        This is a tool for viewing network access using Facebook's open source Stetho.
Okhttp can use Chrome's development tools to view the phone's network access, just like web developers in the browser to view the mobile phone access to the network real-time information, review the submission and return results, greatly facilitate the development of the tool will be dedicated to write a blog to explain.
 
 
        
       

Android's most popular network framework (original)

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.