Main`package com.example.xml;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Xml;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MainActivity extends Activity {
List list=new ArrayList();
private ListView listView;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
String xml = (String) msg.obj;
getxml(xml);
listView.setAdapter(new MyAdapter(list, MainActivity.this));
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
MyBean myBean = list.get(position);
Intent intent = new Intent(MainActivity.this,
SecondActivity.class).putExtra("url", myBean.getLink());
startActivity(intent);
}
});
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.lv);
new Thread() {
public void run() {
String string = Data.Data();
getxml(string);
Message message = Message.obtain();
message.obj = string;
handler.sendMessage(message);
}
}.start();
}
private void getxml(String is) {
try {
XmlPullParser xpp = Xml.newPullParser();
xpp.setInput(new StringReader(is));
int type = xpp.getEventType();
MyBean mb = null;
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
case XmlPullParser.START_TAG:
if (xpp.getName().equals("item")) {
mb = new MyBean();
} else if (xpp.getName().equals("title")) {
mb.setTitle(xpp.nextText());
}else if(xpp.getName().equals("link")){
mb.setLink(xpp.nextText());
}else if (xpp.getName().equals("imgs")) {
mb.setImgs(xpp.nextText());
}
break;
case XmlPullParser.END_TAG:
if (xpp.getName().equals("item")) {
list.add(mb);
}
break;
}
type = xpp.next();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.example.xml;
Data
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class Data {
public static String Data() {
String path = “http://www.sciencenet.cn/xml/iphoneInterface.aspx?type=news&nums=20“;
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(path);
try {
HttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity);
return string;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
`
MyAdapter
package com.example.xml;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class Data {
public static String Data() {
String path = "http://www.sciencenet.cn/xml/iphoneInterface.aspx?type=news&nums=20";
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(path);
try {
HttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity);
return string;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
Bean`package com.example.xml;
public class MyBean {
private String title;
private String link;
private String imgs;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getImgs() {
return imgs;
}
public void setImgs(String imgs) {
this.imgs = imgs;
}
@Override
public String toString() {
return "MyBean [title=" + title + ", link=" + link + ", imgs=" + imgs
+ "]";
}
}
`