XML_解析 ——HttpUtils

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XML_解析 ——HttpUtils相关的知识,希望对你有一定的参考价值。


@XStreamAlias("oschina")
public class MyDatas {




public class Mynewslist {
    @XStreamImplicit(itemFieldName="news")
    private List<MyNews> news;

 

package com.example.fragment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.example.adapter.CopyOfMybaseadpter;
import com.example.adapter.Mybaseadpter;
import com.example.bean.Mmsupper;
import com.example.bean.Myblog;
import com.example.view.XListView;
import com.example.view.XListView.IXListViewListener;
import com.example.vo.Mynews;
import com.example.vo.Super;
import com.example.yk06.R;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.thoughtworks.xstream.XStream;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class F1 extends Fragment implements IXListViewListener{
private XListView listView;
String url;
int mm;
private Mybaseadpter mybaseadpter;
private CopyOfMybaseadpter mybaseadpte;
int count=1;
private List<Mynews> l=new ArrayList<Mynews>();
private List<Myblog> li=new ArrayList<Myblog>();

public F1(String url, int mm) {
    super();
    this.url = url;
    this.mm = mm;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    
    View view=inflater.inflate(R.layout.activity_f1, null);
    listView = (XListView) view.findViewById(R.id.lsitview);
    listView.setPullLoadEnable(true);
    listView.setPullRefreshEnable(true);
    listView.setXListViewListener(this);
    init();
    
    
    
    return view;
    
    
    
}

private void init() {
    // TODO Auto-generated method stub
    HttpUtils utils=new HttpUtils();
    utils.send(HttpMethod.GET, url+count++, new RequestCallBack<String>() {

    

        @Override
        public void onFailure(HttpException arg0, String arg1) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void onSuccess(ResponseInfo<String> arg0) {
            // TODO Auto-generated method stub
            String ss=arg0.result;
            
            XStream stream=new XStream();
            if (mm==0||mm==1) {
                
            
            stream.processAnnotations(Super.class);
            Super super1=(Super) stream.fromXML(ss);
            List<Mynews> ll=super1.getNewslist().getNews();
            
            l.addAll(0,ll);
            mybaseadpter = new Mybaseadpter(l, getActivity());
            
            listView.setAdapter(mybaseadpter);
            }
            if(mm==2||mm==3){
                stream.processAnnotations(Mmsupper.class);
                Mmsupper super1=(Mmsupper) stream.fromXML(ss);
                List<Myblog> ll=super1.getBlogs().getBlog();
                
                li.addAll(0,ll);
                System.out.println(li.toString());
                mybaseadpte= new CopyOfMybaseadpter(li, getActivity());
                
                
                listView.setAdapter(mybaseadpte);
            }
        }
    });
}

private void onLoad() {
    // TODO Auto-generated method stub
    listView.stopRefresh();
    listView.stopLoadMore();
    // 设置日期格式
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    // 获取当前系统时间
    String nowTime = df.format(new Date(System.currentTimeMillis()));
    // 释放时提示正在刷新时的当前时间
    listView.setRefreshTime(nowTime);
}

@Override
public void onRefresh() {
    // TODO Auto-generated method stub
    init();
    onLoad();
}

@Override
public void onLoadMore() {
    // TODO Auto-generated method stub
    HttpUtils utils=new HttpUtils();
    utils.send(HttpMethod.GET, url+count++, new RequestCallBack<String>() {

    

        @Override
        public void onFailure(HttpException arg0, String arg1) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void onSuccess(ResponseInfo<String> arg0) {
            // TODO Auto-generated method stub
            String ss=arg0.result;
            
            XStream stream=new XStream();
            if (mm==0||mm==1) {
                
            
            stream.processAnnotations(Super.class);
            Super super1=(Super) stream.fromXML(ss);
            List<Mynews> ll=super1.getNewslist().getNews();
            
            l.addAll(ll);
            mybaseadpter.notifyDataSetChanged();
            
            }
            if (mm==2||mm==3){
                stream.processAnnotations(Mmsupper.class);
                Mmsupper super1=(Mmsupper) stream.fromXML(ss);
                List<Myblog> ll=super1.getBlogs().getBlog();
                
                li.addAll(ll);
                mybaseadpte.notifyDataSetChanged();
            }
        }
    });
    onLoad();
}
}

 

以上是关于XML_解析 ——HttpUtils的主要内容,如果未能解决你的问题,请参考以下文章

从流输入中解析没有根元素的 XML 片段列表

HttpUtils

无法解析片段中的 findViewById [重复]

jsoup解析xml某片段的问题

Android 逆向使用 Python 解析 ELF 文件 ( Capstone 反汇编 ELF 文件中的机器码数据 | 创建反汇编解析器实例对象 | 设置汇编解析器显示细节 )(代码片段

为 Blogger 上的博客格式化代码片段 [关闭]