xml解析网络请求

实体类News.java

package com.baway.test.bean;

public class News {
    public String item;
    public String title;
    public String catalog;
    public String tags;
    public String sub1;
    public String sub2;
    public String img;
    public String reading;
    public String online;
    public String bytime;
}

MainActivity

package com.baway.test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
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 android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.widget.ListView;

import com.baway.test.adapter.MyAdapter;
import com.baway.test.bean.News;

public class MainActivity extends Activity {

    public static final String tag = "MainActivity";
    ArrayList<News> al;
    private News n;
    private String path;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(tag, "aaaaaa");
        path = "http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=244&rn=10&rn=10&dtype=xml";
        listView = (ListView) findViewById(R.id.listView);
        // 请求信息
        getsubmit();
    }

    public void getsubmit() {

        Async async = new Async();
        async.execute();
    }

    // 内部类
    class Async extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {

            // 请求
            try {

                URL url = new URL(path);
                HttpURLConnection connection = (HttpURLConnection) url
                        .openConnection();
                int responseCode = connection.getResponseCode();

                /*HttpClient client=new DefaultHttpClient();
                HttpGet get=new HttpGet(path);
                HttpResponse execute = client.execute(get);
                int statusCode = execute.getStatusLine().getStatusCode();*/

                Log.i(tag, responseCode + "");
                if (responseCode == 200) {
                    //InputStream inputStream = execute.getEntity().getContent();
                    InputStream inputStream = connection.getInputStream();
                    // xml解析
                    XmlPullParser newPullParser = Xml.newPullParser();
                    newPullParser.setInput(inputStream, "utf-8");
                    // 获取解析的事件类型
                    int eventType = newPullParser.getEventType();
                    while (eventType != XmlPullParser.END_DOCUMENT) {
                        // 获得标签的名字
                        String name = newPullParser.getName();

                        switch (eventType) {
                        // 开始接受文件(走一次)
                        case XmlPullParser.START_DOCUMENT:
                            al = new ArrayList<News>();
                            break;
                        case XmlPullParser.START_TAG:
                            if ("item".equals(name)) {
                                n = new News();
                            } else if ("title".equals(name)) {
                                String title = newPullParser.nextText();
                                n.title = title;
                            } else if ("catalog".equals(name)) {
                                String catalog = newPullParser.nextText();
                                n.catalog = catalog;
                            } else if ("tags".equals(name)) {
                                String tags = newPullParser.nextText();
                                n.tags = tags;
                            } else if ("sub1".equals(name)) {
                                String sub1 = newPullParser.nextText();
                                n.sub1 = sub1;
                            } else if ("sub2".equals(name)) {
                                String sub2 = newPullParser.nextText();
                                n.sub2 = sub2;
                            } else if ("img".equals(name)) {
                                String img = newPullParser.nextText();
                                n.img = img;
                            } else if ("reading".equals(name)) {
                                String reading = newPullParser.nextText();
                                n.reading = reading;
                            } else if ("online".equals(name)) {
                                String online = newPullParser.nextText();
                                n.online = online;
                            } else if ("bytime".equals(name)) {
                                String bytime = newPullParser.nextText();
                                n.bytime = bytime;
                            }
                            break;

                        case XmlPullParser.END_TAG:
                            if ("item".equals(name)) {
                                // 封装进集合
                                al.add(n);

                            }
                            break;

                        }
                        eventType = newPullParser.next();
                    }

                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        protected void onPostExecute(String result) {
            MyAdapter adapter = new MyAdapter(MainActivity.this);
            listView.setAdapter(adapter);

            adapter.addrest(al);

        };
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值