Volley解析Xml数据

必须导入Volley包

package com.example.volley_2;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;


/** * @author HD * @date 2015-11-23 * @package_name com.example.volley_2 * @file_name MainActivity.java */
public class MainActivity extends Activity {
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.textview);
        RequestQueue mQueue = Volley.newRequestQueue(MainActivity.this);
        XMLRequest request = new XMLRequest(Method.GET,
                "http://flash.weather.com.cn/wmaps/xml/china.xml",
                new Listener<XmlPullParser>() {

                    @Override
                    public void onResponse(XmlPullParser response) {
                        // TODO 自动生成的方法存根
                        try {
                            int eventType = response.getEventType();
                            while (eventType != XmlPullParser.END_DOCUMENT) {
                                switch (eventType) {
                                case XmlPullParser.START_TAG:
                                    String nodeName = response.getName();
                                    if ("city".equals(nodeName)) {
                                        String pName = response
                                                .getAttributeName(0);
                                        String pValue = response.getAttributeValue(0);
                                        Log.i("hd", pName+":"+pValue);
                                    }
                                    break;
                                }
                                eventType = response.next();
                            }
                        } catch (XmlPullParserException e) {
                            // TODO 自动生成的 catch 块
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO 自动生成的 catch 块
                            e.printStackTrace();
                        }
                    }
                }, new ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO 自动生成的方法存根
                        Log.i("hd", error.getMessage(), error);
                    }
                });
        mQueue.add(request);
    }

}
package com.example.volley_2;

import java.io.StringReader;
import java.io.UnsupportedEncodingException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;


/** * @author HD * @date 2015-11-23 * @package_name com.example.volley_2 * @file_name XMLRequest.java */
public class XMLRequest extends Request<XmlPullParser> {
    private  Listener<XmlPullParser> mListener;

    public XMLRequest(int method, String url, Listener<XmlPullParser> listener,
            ErrorListener errolistener) {
        super(method, url, errolistener);
        mListener = listener;
        // TODO 自动生成的构造函数存根
    }

    public XMLRequest(int method, String url, ErrorListener listener) {
        super(method, url, listener);
        // TODO 自动生成的构造函数存根
    }

    @Override
    protected Response<XmlPullParser> parseNetworkResponse(
            NetworkResponse response) {
        // TODO 自动生成的方法存根
        String parsed;
        try {
            parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xmlPullParser = factory.newPullParser();
            xmlPullParser.setInput(new StringReader(parsed));
            return Response.success(xmlPullParser, HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            // TODO: handle exception
            return Response.error(new ParseError(e));
        } catch (XmlPullParserException e) {
            // TODO 自动生成的 catch 块
            return Response.error(new ParseError(e));
        }
    }

    @Override
    protected void deliverResponse(XmlPullParser response) {
        // TODO 自动生成的方法存根
        mListener.onResponse(response);
    }

}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.volley_2.MainActivity" >

    <TextView 
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout> 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.volley_2" android:versionCode="1" android:versionName="1.0" >

    <uses-sdk  android:minSdkVersion="14" android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application  android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
        <activity  android:name=".MainActivity" android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

你可能感兴趣的:(Volley解析Xml数据)