zoukankan      html  css  js  c++  java
  • 异步交互解析xml文件

    代码列表:

    MainActivity.class:

    package com.example.yuekao_one_try;
    
    import java.util.List;
    
    import com.example.vo.Item;
    import com.example.vo.Root;
    import com.google.gson.Gson;
    import com.thoughtworks.xstream.XStream;
    
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.GridView;
    import android.widget.ListView;
    
    public class MainActivity extends Activity {
         private String URL = "http://apis.juhe.cn/goodbook/catalog?key=9d6ef8c31647a206e05fcaff70527182&dtype=xml";
        private ListView list_view;
        private List<Item> list;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            list_view=(ListView) findViewById(R.id.list_view);
            new Thread(){
                public void run() {
                    
                    new Asyn().execute(URL);
                };
            }.start();
            
        }
    
        private class Asyn extends AsyncTask<String, Integer, String>{
    
            private  String s;
            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub
                
                s=Networkutil.httpol(URL);
                
                return s;
            }
            @Override
            protected void onPostExecute(String result) {
                // TODO Auto-generated method stub
                
                
                XStream xStream=new XStream();
                xStream.processAnnotations(Root.class);
                Root root=(Root) xStream.fromXML(result);
                List<Item> list=root.getResult().getItem();
                    Mybase base=new Mybase(list, getApplicationContext());
                    list_view.setAdapter(base);
            }
            
        }
    
    }  

    NetworkUtil.class:

    package com.example.yuekao_one_try;
    
    import java.io.IOException;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.params.HttpClientParams;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.params.BasicHttpParams;
    import org.apache.http.params.HttpConnectionParams;
    import org.apache.http.params.HttpParams;
    import org.apache.http.util.EntityUtils;
    
    import android.os.DeadObjectException;
    
    public class Networkutil {
            public static String httpol(String url){
                String result="";
                
                HttpGet httpGet=new HttpGet(url);
                //设置请求参数
                HttpParams parmars=new BasicHttpParams();
             HttpConnectionParams.setConnectionTimeout(parmars, 5000);
    //            HttpConnectionParams.setSoTimeout(parmars, 5*100);
                HttpClient httpClient=new DefaultHttpClient(parmars);
                try {
                    HttpResponse httpResponse=httpClient.execute(httpGet);
                    int len=httpResponse.getStatusLine().getStatusCode();
                    if(len==200){
                        HttpEntity httpEntity=httpResponse.getEntity();
                        result=EntityUtils.toString(httpEntity, "utf-8");
                        
                    }
                    
                    
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
                
                
                
                
                
                
                
                return result;
            }
    }

    vo类:
    Root.class:

    package com.example.vo;
    
    import com.thoughtworks.xstream.annotations.XStreamAlias;
    
    @XStreamAlias("root")
    public class Root {
        private String resultcode;
        private String reason;
        private Mylist result;
        private String error_code;
        public String getResultcode() {
            return resultcode;
        }
        public void setResultcode(String resultcode) {
            this.resultcode = resultcode;
        }
        public String getReason() {
            return reason;
        }
        public void setReason(String reason) {
            this.reason = reason;
        }
        public Mylist getResult() {
            return result;
        }
        public void setResult(Mylist result) {
            this.result = result;
        }
        public String getError_code() {
            return error_code;
        }
        public void setError_code(String error_code) {
            this.error_code = error_code;
        }
        
        
    }

    Mylist.class:

    package com.example.vo;
    
    import java.util.List;
    
    import com.thoughtworks.xstream.annotations.XStreamAlias;
    import com.thoughtworks.xstream.annotations.XStreamImplicit;
    
    @XStreamAlias("result")
    public class Mylist {
    @XStreamImplicit(itemFieldName="item")
    private List<Item> item;
    
    public List<Item> getItem() {
        return item;
    }
    
    public void setItem(List<Item> item) {
        this.item = item;
    }
    
    }

    Item.class:

    package com.example.vo;
    
    import com.thoughtworks.xstream.annotations.XStreamAlias;
    
    @XStreamAlias("item")
    public class Item {
    private String id;
    private String catalog;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getCatalog() {
        return catalog;
    }
    public void setCatalog(String catalog) {
        this.catalog = catalog;
    }
    
    }
  • 相关阅读:
    autocomplete="off" 不起作用
    IE8兼容模式设置
    H5学习
    wampserver安装配置
    HTML5音乐播放器(最新升级改造加强版)
    HTML5+CSS3+jquery实现简单的音乐播放器
    jquery+css3实现3d滚动旋转
    HTML5游戏设计与开发 小白7-9月的动态
    jquery submit()不执行
    html5手机常见问题与工具分享
  • 原文地址:https://www.cnblogs.com/123p/p/5452631.html
Copyright © 2011-2022 走看看