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