MainActivity
package com.example.secondweek_test2; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import com.example.utils.NetWorkUtil; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.util.Xml; import android.view.Menu; public class MainActivity extends Activity { private static final String ENDURL="http://10.1.2.175:8080/Day05_UrlCon/data.xml"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化数据 init(); } private void init() { /** * 请求服务器中xml文件 */ //判断网络是否开通 if(NetWorkUtil.isAvailbale(MainActivity.this))//开通网络了 { //请求网络数据 new MyAsyncTask().execute(ENDURL); } } class MyAsyncTask extends AsyncTask<String, Integer, InputStream>{ private List<Foods> list; private String tag; private Foods f; //耗时操作 @Override protected InputStream doInBackground(String... params) { //向服务器请求数据 String url=params[0]; InputStream byClient = NetWorkUtil.getDataByClient(url); System.out.println(byClient); return byClient; } //更新UI @Override protected void onPostExecute(InputStream result) { parserXml(result); System.out.println(list.toString()); super.onPostExecute(result); } private void parserXml(InputStream result) { //解析请求的数据 XmlPullParser parser=Xml.newPullParser();//获取解析器 try { parser.setInput(result, "utf-8"); // 触发第一次事件 int type = parser.getEventType(); // 循环遍历 while (type != XmlPullParser.END_DOCUMENT)// 筛选的条件是不为结束文档 { // 判断类型 switch (type) { case XmlPullParser.START_DOCUMENT://开始文档 //创建集合对象 list = new ArrayList<Foods>(); break; case XmlPullParser.START_TAG://开始标签 //记录标签的名字 tag = parser.getName(); //判断是否为person if(tag.equals("cate")) { //创建对象 f = new Foods(); //获取属性值 String id = parser.getAttributeValue(0); System.out.println(id); //为person对象赋值 f.setId(Integer.parseInt(id)); } break; case XmlPullParser.TEXT://文本标签 //获取文本标签的值 String text=parser.getText(); //判断文本标签是什么 if(tag.equals("cateurl")) { f.setCateurl(text); }else if(tag.equals("catetitle")){ f.setCatetitle(text); }else if(tag.equals("catecontent")){ f.setCatecontent(text); }else if(tag.equals("cateprice")){ f.setCateprice(text); } break; case XmlPullParser.END_TAG://结束标签 //记录结束标签的名字 tag=parser.getName(); //判断标签是扫描 if(tag.equals("cate")) { //向集合中添加对象 list.add(f); //将person对象制空 f=null; } tag=""; break; case XmlPullParser.END_DOCUMENT://结束文档 break; } type=parser.next();//触发下一次事件 } } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
网络请求
package com.example.utils;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
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.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetWorkUtil {
/*
private static BufferedInputStream is;*/
static InputStream is;
public static boolean isAvailbale(Context context) {
// 获取网络的服务器
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// 获取网络信息
NetworkInfo info = manager.getActiveNetworkInfo();
if (info == null || !info.isAvailable()) {
return false;
}
return true;
}
public static InputStream getDataByClient(String url) {
// 获取get请求对象
HttpGet get = new HttpGet(url);
// 设置参数
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);
// 获取执行请求的对象
HttpClient client = new DefaultHttpClient(params);
// 执行请求
try {
HttpResponse resp = client.execute(get);
// 判断响应是否成功
if (resp.getStatusLine().getStatusCode() == 200) {
// 获取响应的数据
HttpEntity entity = resp.getEntity();
is= entity.getContent();
/*is = new BufferedInputStream(
new DataInputStream());*/
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return is;
}
}