用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

xutils

2017-03-05 作者: 老四和老三的老二都老大了举报

[android]代码库

//初始化Xutils
        x.Ext.init(this);

public interface DataInterface<T> {

    void setdata(List<T> t);
}


public class HttpXutils {

    public static <T>void utils(String url,final Class<T> clazz,final DataInterface dataInterface) {

        x.http().get(new RequestParams(url), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                List<T> beanList = new ArrayList<T>();
                try {
                    JSONObject resultObject = new JSONObject(result);
                    Iterator<String> keys = resultObject.keys();
                    while (keys.hasNext()) {
                        String next = keys.next();
                        JSONArray nextArray = resultObject.optJSONArray(next);
                        for (int i = 0; i < nextArray.length(); i++) {
                            JSONObject object = nextArray.optJSONObject(i);
                            T tBean = gson.fromJson(object.toString(), clazz);
                            beanList.add(tBean);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                dataInterface.setdata(beanList);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("zzz", "onError " + ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.d("zzz", "onCancelled");
            }

            @Override
            public void onFinished() {
                Log.d("zzz", "onFinished");
            }
        });
    }
}


public class TitleFragment extends Fragment implements DataInterface<JsonBean>,PullToRefreshListView.OnRefreshListener2{

    private int homePage=0;
    private int endPage=20;
    private String titleType;
    private PullToRefreshListView pullto;
    private boolean flag=false;
    private String path;
    private NewsAdapter newsAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view1 = View.inflate(getActivity(), R.layout.fragment_title, null);

        //接收值
        titleType = getArguments().getString("type");
        initView(view1);
        return view1;
    }

    //视图加载完成
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        path = "http://c.m.163.com/nc/article/headline/"+titleType+"/"+homePage+"-"+endPage+".html";
        HttpXutils.utils(path,JsonBean.class,this);
        }

    public void initView(View view){
        pullto = (PullToRefreshListView) view.findViewById(R.id.pullto);
        newsAdapter = new NewsAdapter(getActivity());
        pullto.setAdapter(newsAdapter);
        pullto.setMode(PullToRefreshBase.Mode.BOTH);
        pullto.setOnRefreshListener(this);

    }


    @Override
    public void setdata(final List<JsonBean> t) {

        newsAdapter.addData(t,flag);
        newsAdapter.notifyDataSetChanged();

        pullto.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("url",t.get(i-1).getUrl_3w());
                startActivity(intent);
            }
        });
        pullto.onRefreshComplete();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {

        homePage=0;
        endPage=20;
        flag=true;
        HttpXutils.utils(path,JsonBean.class,this);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        homePage+=20;
        endPage+=20;
        flag=false;
        HttpXutils.utils(path,JsonBean.class,this);
    }
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...