关闭

xml解析网络请求

77人阅读 评论(0) 收藏 举报

实体类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);

        };
    }


}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8324次
    • 积分:370
    • 等级:
    • 排名:千里之外
    • 原创:28篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类