首先看看效果图
布局文件
Activity
- package com.zking.analyzexml;
- import android.app.ListActivity;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Xml;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import com.zking.entity.Person;
- import com.zking.utils.HttpData;
- import org.xmlpull.v1.XmlPullParser;
- import org.xmlpull.v1.XmlPullParserException;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.UnsupportedEncodingException;
- import java.util.ArrayList;
- import java.util.List;
- public class MainActivity extends ListActivity {
- private List persons = new ArrayList < >();@Override protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initViews();
- }
- private void initViews() {
- final MyAdapter myAdapter = new MyAdapter();
- setListAdapter(myAdapter); //请求服务器拿到数据 new HttpData("http://192.168.0.103:8080/MySSM/getPersons", new HttpData.MyData() { @Override public void initData(InputStream inputStream, OutputStream outputStream) { XmlPullParser xmlPullParser = Xml.newPullParser(); try { xmlPullParser.setInput(inputStream,"utf-8"); { String s = null; Person person = null; //将xml解析成集合 int type = xmlPullParser.getEventType(); while (type!= XmlPullParser.END_DOCUMENT){ switch (type){ case XmlPullParser.START_TAG: if ("Person".equals(xmlPullParser.getName())){ person = new Person(); person.setId(Integer.parseInt(xmlPullParser.getAttributeValue(0))); }else if("name".equals(xmlPullParser.getName())){ person.setName(xmlPullParser.nextText()); }else if ("age".equals(xmlPullParser.getName())){ person.setAge(Integer.parseInt(xmlPullParser.nextText())); } break; case XmlPullParser.END_TAG: if ("Person".equals(xmlPullParser.getName())){ persons.add(person); } break; } type = xmlPullParser.next(); } } myAdapter.notifyDataSetChanged(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } } }); } private class MyAdapter extends BaseAdapter{ @Override public int getCount() { return persons.size(); } @Override public Object getItem(int position) { return persons.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { LinearLayout linearLayout = new LinearLayout(MainActivity.this); linearLayout.addView(getTextView(persons.get(position).getId().toString())); linearLayout.addView(getTextView(persons.get(position).getName().toString())); linearLayout.addView(getTextView(persons.get(position).getAge().toString())); return linearLayout; } public TextView getTextView(String text){ TextView textView = new TextView(MainActivity.this); textView.setText(text); return textView; } }}
工具类
- package com.zking.utils;
- import android.net.Uri;
- import android.os.AsyncTask;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- /** * Created by Administrator on 2017/2/24. */
- public class HttpData {
- public interface MyData {
- void initData(InputStream inputStream, OutputStream outputStream);
- } //利用接口的多态性实现动态回调 private MyData myData; private String path; public HttpData(String path,MyData myData) { this.path = path; this.myData = myData; MyTask myTask = new MyTask(); myTask.execute(); } private class MyTask extends AsyncTask{ private InputStream inputStream; private OutputStream outputStream; @Override protected Object doInBackground(Object[] params) { try { URL url = new URL(path); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setConnectTimeout(5000); inputStream = httpURLConnection.getInputStream(); outputStream = httpURLConnection.getOutputStream(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Object o) { super.onPostExecute(o); if (myData != null){ myData.initData(inputStream,outputStream); } } } public void setMyData(MyData myData){ this.myData = myData; }}
权限
就爱阅读 www.92to.com 网友整理上传, 为您提供最全的知识大全, 期待您的分享,转载请注明出处。
来源: http://www.92to.com/bangong/2017/02-27/17739154.html