Oschina Client Source Learning (1)--android interaction with the server

Source: Internet
Author: User

The core code is interpreted as an example of the " Information " module under this tab of "synthesis".

Preparation: Refer to one of my other blog posts
http://blog.csdn.net/csp277/article/details/46610893
Then click on the class below
Class Asynchttpresponsehandler

The first step: Define Asynchttpresponsehandler, which will be the parameter of the HTTP request handler object

protectedAsynchttpresponsehandler Mhandler =NewAsynchttpresponsehandler () {@Override         Public void onsuccess(intStatusCode, header[] headers,byte[] responsebytes) {if(Mcurrentpage = =0&& Needautorefresh ()) {Appcontext.puttolastrefreshtime (Getcachekey (), Strin            Gutils.getcurtimestr ()); }if(Isadded ()) {if(mstate = = State_refresh)                {onrefreshnetworksuccess ();            } executeparsertask (ResponseBytes); }        }@Override         Public void onfailure(intARG0, header[] arg1,byte[] arg2, Throwable arg3) {if(Isadded ())            {Readcachedata (Getcachekey ()); }        }    };

Step two: Send request data
The sending data is primarily called the Get method of the Asynchttpclient object, and the first step defines the handler as the parameter

protected void Sendrequestdata() {oschinaapi.getnewslist (Mcatalog, Mcurrentpage, Mhandler); } Public Static void getnewslist(intCatalogintpage, Asynchttpresponsehandler handler) {Requestparamsparams=NewRequestparams ();params. put ("Catalog", catalog);params. put ("PageIndex", page);params. put ("PageSize", appcontext.page_size);if(Catalog = = Newslist.catalog_week) {params. put ("Show","Week"); }Else if(Catalog = = Newslist.catalog_month) {params. put ("Show","Month"); } apihttpclient.Get("Action/api/news_list",params, handler); }

Step three: Parse the return result (in the red Code of the first step, open an asynchronous thread Parsertask)
Where inheritance Asynctask must implement two methods:
1.doInBackground typically handles more time-consuming operations in this approach
2.onPostExecute typically updates the UI in this method
Questions about the asynchronous operation of UI updates are summarized later. Here donuts.
The XStream framework is used for the specific parsing process. Core code:
obj = (T) xmstream.fromxml (IS);
Specific usage I will continue to update in the following blog.

 class parsertask extends asynctask<void, void,  String> {        Private FinalByte[] Reponsedata;PrivateBoolean parsererror;Private List<T>List; PublicParsertask (byte[] data) {this.reponsedata = data; } @OverrideprotectedString doinbackground (Void ... params) {Try{listentity<t> data = Parselist (NewBytearrayinputstream (Reponsedata));NewSavecachetask (Getactivity (), Data, Getcachekey ()). Execute ();List= Data.getlist ();if(List==NULL) {Resultbean Resultbean = Xmlutils.tobean (Resultbean.class, reponsedata);if(Resultbean! =NULL) {Mresult = Resultbean.getresult (); }                }            }Catch(Exceptione) {e.printstacktrace (); ParserError =true; }return NULL; } @Overrideprotectedvoid OnPostExecute (String result) {Super.onpostexecute (result);if(ParserError)            {Readcachedata (Getcachekey ()); }Else{executeonloaddatasuccess (List);            Executeonloadfinish (); }        }    }

Summary: Learn how to use asynchttpclient and Asynchttpresponsehandler to interact with Android and server side.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Oschina Client Source Learning (1)--android interaction with the server

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.