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:
- Instantiating a asynchttpclient object
- Call the Async Get method incoming (String URL, new Asynchttpresponsehandler method)
- 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 ();});
- Instantiating a tasynchttpclient object
- Call the Async Get method incoming (String URL, new Asynchttpresponsehandler method)
- 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
- Instantiating a asynchttpclient object
- Instantiate a requestparams object and put add key value pair parameter
- Call the Async Post method incoming (String URL, new Asynchttpresponsehandler method)
- 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
- Instantiating a asynchttpclient object
- Instantiate the Filehttpresponsehandler object and pass in the cache path of the parameter file
- Implement OnProgress (), onsuccess (), OnFailure ()) methods
- Filehttpresponsehandler.setinterrupt (false); method to stop the pause otherwise to open the download pause method
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
- Instantiating a Downloadmanager object
- Call the AddHandler method and pass in
- Call the download callback method and implement Onloading (), Onadd and other methods
- 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.)