Comprehensive usage of HttpClient, Xmlpullparser, ListView and Baseadapter

Source: Internet
Author: User

Summary of Knowledge points

1, through the Android API built-in class httpclient from the background to extract data, httpclient is an interface, so with his subclass to instantiate Defaulthttpclient, the specific code is as follows:

Private List<news> getnewsfrominternet () {

HttpClient client = null;

try {

Define a client

Client = new Defaulthttpclient ();

Defining the Get method

HttpGet get = new HttpGet ("Http://10.0.2.2:8080/NetEaseServer/new.xml");

Execute request

HttpResponse response = Client.execute (get);

int statusCode = Response.getstatusline (). Getstatuscode ();

if (StatusCode = = 200) {

InputStream is = Response.getentity (). getcontent ();

list<news> newinfolist = Getnewlistfrominputstream (IS);

return newinfolist;

} else {

LOG.I (TAG, "Access failed:" + statusCode);

}

} catch (Exception e) {

E.printstacktrace ();

} finally {

if (client! = NULL) {

Client.getconnectionmanager (). Shutdown (); Close and release resources

}

}

return null;

}

3,xmlpullparser parsing from the later sent over the XML file

Private list<news> Getnewlistfrominputstream (InputStream is) throws Exception {

Xmlpullparser parser = Xml.newpullparser (); Create a Pull Parser

Parser.setinput (IS, "utf-8"); Specifies the parsing stream, and the encoding

int eventtype = Parser.geteventtype ();

List<news> newinfolist = null;

News newinfo = null;

while (eventtype! = xmlpullparser.end_document) {//If not at the end, continue looping

String tagName = Parser.getname (); Node name

Switch (eventtype) {

Case Xmlpullparser.start_tag://<news>

if ("News". Equals (TagName)) {

Newinfolist = new arraylist<news> ();

} else if ("new". Equals (TagName)) {

Newinfo = new News ();

} else if ("title". Equals (TagName)) {

Newinfo.settitle (Parser.nexttext ());

} else if ("detail". Equals (TagName)) {

Newinfo.setdetail (Parser.nexttext ());

} else if ("comment". Equals (TagName)) {

Newinfo.setcomment (Parser.nexttext ());

} else if ("image". Equals (TagName)) {

Newinfo.setbitmap (Parser.nexttext ());

}

Break

Case Xmlpullparser.end_tag://</news>

if ("New". Equals (TagName)) {

Newinfolist.add (Newinfo);

}

Break

Default

Break

}

EventType = Parser.next (); Remove an event type

}

return newinfolist;

}

3, bound to the ListView This control needs to set the properties of the Setadapter, belong to the first to define a class to implement Baseadapter, mainly to implement the baseadapter of the two methods, the specific code is as follows:

Class Myadapter extends baseadapter{

@Override

public int GetCount () {

TODO auto-generated Method Stub

return Listnewsinfo.size ();

}

@Override

Public Object getItem (int position) {

TODO auto-generated Method Stub

return null;

}

@Override

public long getitemid (int position) {

TODO auto-generated Method Stub

return 0;

}

@Override

Public View GetView (int position, View Convertview, ViewGroup parent) {

View view = null;

if (Convertview = = null) {

Layoutinflater inflater = Getlayoutinflater ();

View = Inflater.inflate (R.layout.activity_item, NULL);

} else {

view = Convertview;

}

Re-assignment does not result in the retention of the original data in the cached object

Smartimageview Sivicon = (smartimageview) View.findviewbyid (R.id.iv_itemimg_icon);

TextView Tvtitle = (TextView) View.findviewbyid (r.id.tv_item_tvtitle);

TextView Tvdetail = (TextView) View.findviewbyid (R.id.tv_item_discuss);

TextView tvcomment = (TextView) View.findviewbyid (r.id.tv_item_content);

News newinfo = listnewsinfo.get (position);

Sivicon.setimageurl (Newinfo.getbitmap ()); Set up a picture

Tvtitle.settext (Newinfo.gettitle ());

Tvdetail.settext (Newinfo.getdetail ());

Tvcomment.settext (newinfo.getcomment () + "thread");

return view;

}

}

General usage of HttpClient, Xmlpullparser, ListView, and Baseadapter

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.