请求到的是xml文件
package com.example.kaoshi;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
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.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import com.example.adapter.MyAdapter;
import com.example.bean.News;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Xml;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView lv_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_list = (ListView) findViewById(R.id.lv_list);
// 网络请求数据
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute("http://www.oschina.net/action/api/news_list");
}
public class MyAsyncTask extends AsyncTask<String, Void, String> {
private ArrayList<News> newList;
private News news;
private XmlPullParser pullParser;
@Override
protected String doInBackground(String... params) {
//得到数据
String info = getInfo(params[0]);
return info;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
pullParser = Xml.newPullParser();
try {
//将string转换成字节流
pullParser.setInput(
new ByteArrayInputStream(result.getBytes("UTF-8")),
"utf-8");
//得到事件类型
int eventType = pullParser.getEventType();
while (eventType != pullParser.END_DOCUMENT) {
String name = pullParser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if ("newslist".equals(name)) {
// 创建集合
newList = new ArrayList<News>();
} else if ("news".equals(name)) {
// 创建对象
news = new News();
} else if ("id".equals(name)) {
// 属性
news.id = pullParser.nextText();
} else if ("title".equals(name)) {
// 属性
news.title = pullParser.nextText();
} else if ("body".equals(name)) {
// 属性
news.body = pullParser.nextText();
}
break;
case XmlPullParser.END_TAG:
if ("news".equals(name)) {
newList.add(news);
}
break;
default:
break;
}
eventType = pullParser.next();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lv_list.setAdapter(new MyAdapter(MainActivity.this, newList));
super.onPostExecute(result);
}
}
public String getInfo(String string) {
try {
URL url = new URL(string);
HttpURLConnection openConnection = (HttpURLConnection) url
.openConnection();
openConnection.setRequestMethod("GET");
openConnection.setReadTimeout(5000);
int responseCode = openConnection.getResponseCode();
if (responseCode == 200) {
//得到流
InputStream inputStream = openConnection.getInputStream();
int len;
byte[] b = new byte[1024];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
while ((len = inputStream.read(b)) != -1) {
outputStream.write(b, 0, len);
}
System.out.println(outputStream.toString());
return outputStream.toString();
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
}