Summary of Thinkandroid learning of third-party framework (II.)

Source: Internet
Author: User

Tag: row param CLI His map client returns IMA

In this paper, some common modules of thinkandroid are recorded, and this article continues to introduce the networked modules in Thinkandroid.

As usual, first on GitHub. Address: Https://github.com/white-cat/ThinkAndroid

First, the HTTP module use method:

    • Asynchronous Get method
    1. Instantiating a asynchttpclient object
    2. Call the Async Get method incoming (String URL, new Asynchttpresponsehandler method)
    3. Implement OnStart (), onsuccess (), OnFailure (), OnFinish () methods
Asynchttpclient Client=NewAsynchttpclient (); Client. Get ("http://www.thinkandroid.cn/",NewAsynchttpresponsehandler () {@OverridePublicvoid Onsuccess (String content) {TODO auto-generated Method StubSuper. onsuccess (content);talogger.D (loginactivity.this, content); }  @Override public void OnStart () {// TODO auto-generated Method Stub super.onstart ();}  @Override public void onfailure ( Throwable error) {//TODO auto-generated Method stub super @Override public void onfinish () {// TODO auto-generated Method Stub super.onfinish ();});      
    • Synchronous Get method
    1. Instantiating a tasynchttpclient object
    2. Call the Async Get method incoming (String URL, new Asynchttpresponsehandler method)
    3. Implement OnStart (), onsuccess (), OnFailure (), OnFinish () methods
Tasynchttpclient Client=NewTasynchttpclient (); Client. Get ("http://www.thinkandroid.cn/",NewAsynchttpresponsehandler () {@OverridePublicvoid Onsuccess (String content) {TODO auto-generated Method StubSuper. onsuccess (content);talogger.D (loginactivity.this, content); }  @Override public void OnStart () {// TODO auto-generated Method Stub super.onstart ();}  @Override public void onfailure ( Throwable error) {//TODO auto-generated Method stub super @Override public void onfinish () {// TODO auto-generated Method Stub super.onfinish ();}); 
/span>
    • Uploading a file using an HTTP module or submitting data to the server (post method, async) is similar to the previous Get method
    1. Instantiating a asynchttpclient object
    2. Instantiate a requestparams object and put add key value pair parameter
    3. Call the Async Post method incoming (String URL, new Asynchttpresponsehandler method)
    4. Implement OnStart (), onsuccess (), OnFailure (), OnFinish () methods
Requestparams params=NewRequestparams (); Params. put ("Username","White_Cat"); Params. put ("Password","123456"); Params. put ("Email","[Email protected]"); Params. put ("Profile_picture",NewFile ("/mnt/sdcard/testpic.jpg"));Upload file params. put ("Profile_picture2", InputStream);Upload data stream params. put ("Profile_picture3",NewBytearrayinputstream (bytes));Commit byte stream client. Post ("http://www.thinkandroid.cn/",NewAsynchttpresponsehandler () {@OverridePublicvoid Onsuccess (String content) {TODO auto-generated Method StubSuper. onsuccess (content);talogger.D (loginactivity.this, content); }  @Override public void OnStart () {// TODO auto-generated Method Stub super.onstart ();}  @Override public void onfailure ( Throwable error) {//TODO auto-generated Method stub super @Override public void onfinish () {// TODO auto-generated Method Stub super.onfinish ();}); } 
    • Similarly use HTTP modules to upload files or submit data to the server (post method, sync)
    • Download files using http: Support breakpoint continuation, stop downloading tasks at any time or start tasks
    1. Instantiating a asynchttpclient object
    2. Instantiate the Filehttpresponsehandler object and pass in the cache path of the parameter file
    3. Implement OnProgress (), onsuccess (), OnFailure ()) methods
    4. Filehttpresponsehandler.setinterrupt (false); method to stop the pause otherwise to open the download pause method
    5. Call synchttpclient Download (Url,filehttpresponsehandler); method, incoming URL address and filehttpresponsehandler object

PS: Generally in the onprogress to perform processing download progress, speed and other operations;

Instance code:

    • Single-threaded, multi-tasking download
    1. Instantiating a Downloadmanager object
    2. Call the AddHandler method and pass in
    3. Call the download callback method and implement Onloading (), Onadd and other methods
    4. Call Pausehandler to pause the download task

The instance code is as follows:

Final Downloadmanager Mdownloadmanager = Downloadmanager.getdownloadmanager ();

Mdownloadmanager.addhandler (URL);

Second, the Picture module method

    • ImageView loading pictures does not need to consider the image during the loading process of oom and the Android container rapid sliding when the picture dislocation phenomenon.

Load:

@TAInjectView (R. IDAdapter (This, gettaapplication ());// Incoming taapplication LV. Setadapter (adapter);
Binding Data:
1. Instantiate Tadownloadbitmaphandler object incoming parameter context, picture width high
2, instantiate the Tabitmapcallbackhanlder object, set the picture loading picture through the Setloadingimage method
3. Instantiating Tabitmapcahcework objects
4. The object calls the Setfilecache implementation to set the cache path
5. The object calls the bitmap method of step 2 and passes in the TCBH object
6. The object calls the Setprocessdatahandler method of step 1 to pass in the Tdbh object
7. Tabitmapcachework object calls Loadformcache method objects object and ImageView for picture cache reading
The instance code is as follows:

Data adaptation in the GetView method:

Third, the Cache module method
    • Very simple implementation of caching, take a simple file cache as an example
    • Download Processing class
    • Cache result Return Action class

Call the cache processing method:

Four, the download module
    • Can be a simple implementation of multi-threaded download, background download, breakpoint continuation, download control, such as Start, pause, delete and so on the principle of single-threaded multi-Task Download:
Downloadmanager Downloadmanager;
Downloadmanager=Downloadmanager. Getdownloadmanager (); Downloadmanager.setdownloadcallback (new downloadcallback () { @Override public void onsuccess (String URL {
} @Override public void onloading ( string URL, string speed, string progress) {//TODO auto-generated Method stub}); //add Downloadmanager.addhandler (URL); //continue Downloadmanager.continuehandler (URL); //pause downloadmanager.pausehandler (URL); //delete downloadmanager.deletehandler (URL);
  • Background download

    PrivateIdownloadservice Downloadservice;Serviceconnection serviceconnection=NewServiceconnection () { @Override public void onservicedisconnected (componentname name" {//TODO auto-generated Method stub}  @Override public void onserviceconnected (componentname name, ibinder service) {//TODO auto-generated Method stub downloadservice= (idownloadservice) service;} }; //Add task Downloadservice.addtask (URL) //pause task Downloadservice.pausetask (URL) //continue task Downloadservice//Delete task Downloadservice.deletetask (URL)    


Summary of Thinkandroid learning of third-party framework (II.)

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.