Android HTTP and XML Parsing

Source: Internet
Author: User

Form 1

Package HTTP. view; import Java. io. ioexception; import Java. io. stringreader; import Java. util. arraylist; import Java. util. iterator; import Java. util. list; import javax. XML. parsers. saxparserfactory; import Mars. modle. mp3info; import Org. apache. HTTP. httpentity; import Org. apache. HTTP. httpresponse; import Org. apache. HTTP. httpstatus; import Org. apache. HTTP. namevaluepair; import Org. apache. HTTP. client. clientprotocolexception; import Org. apache. HTTP. client. httpclient; import Org. apache. HTTP. client. entity. urlencodedformentity; import Org. apache. HTTP. client. methods. httppost; import Org. apache. HTTP. impl. client. defaulthttpclient; import Org. apache. HTTP. message. basicnamevaluepair; import Org. apache. HTTP. util. entityutils; import Org. XML. sax. inputsource; import Org. XML. sax. xmlreader; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. textview; public class Post extends activity {textview mtextview = NULL; @ override protected void oncreate (bundle savedinstancestate) {// todo auto-generated method stub super. oncreate (savedinstancestate); setcontentview (R. layout. post); mtextview = (textview) This. findviewbyid (R. id. my); // HTTP address string httpurl = "http://wap.51tes.com/ I .asmx/c"; // get the data httppost httprequest = new httppost (httpurl ); // use namevaluepair to save the list of post data to be passed <namevaluepair> Params = new arraylist <namevaluepair> (); // Add the required parameter Params. add (New basicnamevaluepair ("C1", "Wm"); Params. add (New basicnamevaluepair ("C2", "240*320"); Params. add (New basicnamevaluepair ("C3", "1.0"); Params. add (New basicnamevaluepair ("C4", "6365 t"); try {// set the character set httpentity = new urlencodedformentity (Params, "utf8 "); // request httprequest. setentity (httpentity); // get the default httpclient = new response (); // get httpresponse = destination cute (httprequest); // If (httpresponse. getstatusline (). getstatuscode () = httpstatus. SC _ OK) {// obtain the returned string strresult = entityutils. tostring (httpresponse. getentity (); mtextview. settext (strresult); parse (strresult);} else {mtextview. settext ("request error") ;}} catch (clientprotocolexception e) {mtextview. settext (E. getmessage (). tostring ();} catch (ioexception e) {mtextview. settext (E. getmessage (). tostring ();} catch (exception e) {mtextview. settext (E. getmessage (). tostring ();} // set the button event listening button button_back = (button) findviewbyid (R. id. button_back_post);/* Listen to the time information of the button */button_back.setonclicklistener (New button. onclicklistener () {public void onclick (view v) {/* Create an intent object */intent = new intent ();/* specify the intent class */intent. setclass (post. this, HTTP. class);/* start a new activity */startactivity (intent);/* close the current activity */post. this. finish () ;}}) ;}private list <mp3info> parse (string xmlstr) {saxparserfactory = saxparserfactory. newinstance (); List <mp3info> Infos = new arraylist <mp3info> (); try {xmlreader = saxparserfactory. newsaxparser (). getxmlreader (); mycontenthandler mp3listcontenthandler = new mycontenthandler (Infos); xmlreader. setcontenthandler (mp3listcontenthandler); xmlreader. parse (New inputsource (New stringreader (xmlstr); For (iterator <mp3info> iterator = Infos. iterator (); iterator. hasnext ();) {mp3info mp3info = iterator. next (); system. out. println (mp3info) ;}} catch (exception e) {e. printstacktrace () ;}return Infos ;}}

Form 2

Package HTTP. view; import Java. util. list; import Mars. modle. mp3info; import Org. XML. sax. attributes; import Org. XML. sax. saxexception; import Org. XML. sax. helpers. defaulthandler; public class mycontenthandler extends defaulthandler {private list <mp3info> Infos = NULL; Public mycontenthandler (list <mp3info> Infos) {super (); this. infos = Infos;} public list <mp3info> getinfos () {return Infos;} public void setinfos (list <mp3info> Infos) {This. infos = Infos;} private mp3info mp3info = NULL; private string tagname; Public void startdocument () throws saxexception {} public void enddocument () throws saxexception {} public void startelement (string namespaceuri, string localname, string QNAME, attributes ATTR) throws saxexception {This. tagname = localname; If (localname. equals ("e") {mp3info = new mp3info () ;}} public void endelement (string namespaceuri, string localname, string QNAME, attributes ATTR) throws saxexception {If (QNAME. equals ("e") {Infos. add (mp3info);} tagname = NULL;} public void characters (char [] CH, int start, int length) throws saxexception {string temp = new string (CH, start, length); If (tagname. equals ("E1") {mp3info. sete1 (temp);} else if (tagname. equals ("E2") {mp3info. sete2 (temp);} else if (tagname. equals ("E3") {mp3info. sete3 (temp);} else if (tagname. equals ("E4") {mp3info. sete4 (temp);} else if (tagname. equals ("E5") {mp3info. sete5 (temp );}}}
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.