pull解析WebXML

从http://www.webxml.com.cn获取城市的天气,并解析,提供两种方法

一、常规Android 原生http请求

WebXml官网

Manifest 记得添加网络请求权限,

private static String url = "http://ws.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName";
private static String parameter = "theCityName";
private static String city = "南京";

 
  
private void methodOne() {
        mBulider = new StringBuilder();
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    URL u = new URL(url + "?" + parameter + "=" + URLEncoder.encode(city, "utf-8"));
                    HttpURLConnection conn = (HttpURLConnection) u.openConnection();
                    // conn.setDoInput(true);
                    // conn.setDoOutput(true);
                    conn.setRequestMethod("GET");
                    // conn.setUseCaches(true);
                    // conn.setRequestProperty("Content-Type",
                    // "application/x-www-form-url");
                    // conn.setRequestProperty("Charset", "utf-8");
                    // conn.connect();
                    if (conn.getResponseCode() == 200) {
                        InputStream in = conn.getInputStream();
                        XmlPullParser xmlPullParser = Xml.newPullParser();
                        xmlPullParser.setInput(in, "UTF-8");
                        int eventCode = xmlPullParser.getEventType();
                        while (eventCode != XmlPullParser.END_DOCUMENT) {
                            String name = xmlPullParser.getName();
                            switch (eventCode) {
                                case XmlPullParser.START_DOCUMENT:// 文档开始

                                    break;
                                case XmlPullParser.START_TAG:// 元素开始.
                                    if (name.equalsIgnoreCase("string"))
                                        mBulider.append(xmlPullParser.nextText().trim().toString() + "\n");
                                    break;
                                case XmlPullParser.END_TAG:// 元素结束

                                    break;
                                case XmlPullParser.END_DOCUMENT:// 文档结束

                                    break;

                                default:
                                    break;
                            }
                            eventCode = xmlPullParser.next();
                        }
                        in.close();
                        changeUI();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
二、其他网络请求框架

本文采用xUtils中的网络请求

// 天气查询接口

    public void methodTwo(String theCityName, Callback.CommonCallback callback) {
        org.xutils.http.RequestParams mpaParams = new org.xutils.http.RequestParams(url);
        mpaParams.addBodyParameter(parameter, city);
        x.http().get(mpaParams, callback);
    }

    Callback.CommonCallback callback = new Callback.CommonCallback() {

        @Override
        public void onCancelled(CancelledException arg0) {
        }

        @Override
        public void onError(Throwable arg0, boolean arg1) {
        }

        @Override
        public void onFinished() {
        }

        @Override
        public void onSuccess(String arg0) {
            try {
                mBulider = new StringBuilder();
                // InputStream is = StrToInput.getXml(arg0);
                XmlPullParser pullParser = Xml.newPullParser();
                StringReader reader = new StringReader(arg0);
                pullParser.setInput(reader);
                int event = pullParser.getEventType();
                while (event != XmlPullParser.END_DOCUMENT) {
                    switch (event) {
                        case XmlPullParser.START_DOCUMENT:
                            break;

                        case XmlPullParser.START_TAG:

                            if ("string".equals(pullParser.getName()))
                                mBulider.append(pullParser.nextText().trim().toString() + "\n");
                            break;
                        case XmlPullParser.END_TAG:
                            // if ("new".equals(pullParser.getName())) {
                            //
                            // wearthList = null;
                            // }
                            break;
                    }
                    event = pullParser.next();
                }
                // reader.close();
                changeUI();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };


两种方法的区别在于返回值,方法一的返回就是InputStream,方法二确实String,所以解析方法也有些区别,方法一方法二我们都可以用pull解析器的setInput方法,但是传递的参数确实不一样的。一直接把InputStream传进去,加上编码格式,但是方法二我们只能先把String转换成StringReader才能让解析器解析出来,如大牛有异议或其他方法,欢迎留言

Android studio 开发记住添加Lib

    compile 'org.xutils:xutils:3.3.36'
Eclipse 直接添加jar包

源码地址:

GitHub

CSDN


你可能感兴趣的:(Android应用开发)