wemall app商城源码Android 获取XML网络数据并绑定到ListView

wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改。本文分享Android 获取XML网络数据并绑定到ListView,对数据进行获取并显示代码信息,供技术员参考学习。

1、添加ListView中每一项的item

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
  android:id="@+id/record"
  android:layout_width="230dp"
  android:layout_height="wrap_content" />
<TextView
  android:id="@+id/name"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />
</LinearLayout>

2、添加实体类NewsObj

public class NewsObj implements Serializable {
  private String name;
  private int id;
}// 省去属性get和set方法

3、添加一个类用于处理请求消息

public class NewsService {
    // 解析XML数据
    private static List parseXML(InputStream ins) throws Exception {
        List list = new ArrayList();
        NewsObj obj = null;
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(ins, "UTF-8");
        int event = parser.getEventType();
        while (event != XmlPullParser.END_DOCUMENT) {
            switch (event) {
            case XmlPullParser.START_TAG:
                if ("news".equals(parser.getName())) {
                    int id = new Integer(parser.getAttributeValue(0));
                    obj = new NewsObj();
                    obj.setId(id);
                } else if ("name".equals(parser.getName())) {
                    obj.setName(parser.nextText());
                }
                break;
            case XmlPullParser.END_TAG:
                if ("news".equals(parser.getName())) {
                    list.add(obj);
                    obj = null;
                }
                break;
            }
            event = parser.next();
        }
        return list;
    }

    // 获取最新的资讯
    public static List getLastnews(String Urlpath) throws Exception {
        URL url = new URL(Urlpath);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        Log.i("CONN", conn.toString());
        if (conn.getResponseCode() == 200) {
            InputStream ins = conn.getInputStream();
            return parseXML(ins);
        }
        return null;
    }
}

4、修改MainActivity

public class MainActivity extends Activity {
    private Handler handler;
    private SimpleAdapter adapter;
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);
        handler = new Handler();
        new Thread(runa).start();
    }

    private Runnable setadapter = new Runnable() {
        @Override
        public void run() {
            lv.setAdapter(adapter);
        }
    };

    private void SetView() {
        try {
            List videos = NewsService.getLastnews("http://10.0.2.2:4484/WebSite1/XMLFile.xml");
            List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
            for (NewsObj news : videos) {
                HashMap<String, Object> item = new HashMap<String, Object>();
                item.put("id", news.getId());
                item.put("name", news.getName());
                data.add(item);
            }
            adapter = new SimpleAdapter(getApplicationContext(), data,
                    R.layout.item, new String[] { "id", "name" }, new int[] { R.id.record, R.id.name });
            handler.post(setadapter); // 添加一个任务到主线程的handler!!!
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Runnable runa = new Runnable() {
        @Override
        public void run() {
            Looper.prepare();
            SetView();
        }
    };
}

5、添加一个web测试项

上一步中网站请求地址返回的数据如下

<?xml version="1.0" encoding="utf-8" ?>
<videos>
  <news id ="11">
    <name>hello world</name>
  </news>
  <news id ="12">
    <name>Livingstone</name>
  </news>
</videos>

6、添加网络访问权限,运行程序

原文详情地址:http://Git.oschina.NET/zzunet/wemall-doraemon/commit/e8f303df5663dc69fe47bb9623222149d40e3956

wemall doraemonAndroid app商城详情地址:http://www.koahub.com/home/product/55

wemall官网地址:http://www.wemallshop.com

wemall 开源微商城 ,微信商城,商城源码,三级分销,微生鲜,微水果,微外卖,微订餐---专业的o2o系统

wemall app商城源码Android 获取XML网络数据并绑定到ListView

上一篇:数据库(SQL SERVER)常用知识点


下一篇:java调用机器上的shell脚本