分享

android下通过xstream解析xml格式信息

 instl 2015-04-13
package com.easymorse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
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 android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.thoughtworks.xstream.XStream;
public class NextActivity extends Activity {
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.next_activity);
        this.textView=(TextView) this.findViewById(R.id.TextView01);
        HttpClient client = new DefaultHttpClient();
        StringBuilder builder = new StringBuilder();
        HttpGet get = new HttpGet(
                “http://marshal.easymorse.com/wp-content/uploads/2009/10/product2.xml”);
        try {
            HttpResponse response = client.execute(get);
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()));
            for (String s = reader.readLine(); s != null; s = reader.readLine()) {
                builder.append(s);
            }
            Log.v(“response”,”product:”+builder.toString());
            XStream xstream = new XStream();
            xstream.alias(“product”, Product.class);
            Product product=(Product) xstream.fromXML(builder.toString());
            this.textView.setText(product.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多