10.okhttp结合Gson的深入使用(主页的商品分类重构)

本文介绍了如何在Android开发中深入使用Okhttp结合Gson进行网络请求和数据解析。首先,引入了Okhttp和Gson的相关依赖。接着,通过封装的Okhttp方法获取网络数据,并利用Gson解析JSON。为了确保数据解析在子线程中进行,以避免UI线程阻塞,同时也将UI更新操作放在子线程中执行。文章提供了关键代码片段,包括网络请求接口和数据模型定义。
摘要由CSDN通过智能技术生成

1.在使用okhttp,Gson的时候都要导入相应的包,可以在build中直接加入依赖,或或者右击项目dependence

2.使用封装好的okhttp来获取数据,封装好的里面也已经有用Gson解析数据,然后编写对应网络数据的javabean,注意javabean中的数据id必须和json中的数据id一模一样

3.然后调用封装好的方法,之后将数据存储在list<.javabean>中,

4.***最重要的一点应为解析Json数据是放在子线程中使用的,所以要将UI更新也放在子线程里,要不然,UI不会刷新


代码:

1.

compile 'com.squareup.okhttp:okhttp:2.5.0'

compile 'io.github.openfeign:feign-gson:9.3.1'

2.json的javabean:

package zuo.com.ui.bean;

import java.io.Serializable;

/**真正的recyclor中的javabean
 * Created by Administrator on 2016/10/10.
 */
public class RecyclorBean {
    private int id;
    private String title;
    private RecyclorItemBean cpOne;
    private RecyclorItemBean cpTwo;
    private RecyclorItemBean cpThree;

    public RecyclorBean(int id, String title, RecyclorItemBean cpOne, RecyclorItemBean cpTwo, RecyclorItemBean cpThree) {
        this.id = id;
        this.title = title;
        this.cpOne = cpOne;
        this.cpTwo = cpTwo;
        this.cpThree = cpThree;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public RecyclorItemBean getCpOne() {
        return cpOne;
    }

    public void setCpOne(RecyclorItemBean cpOne) {
        this.cpOne = cpOne;
    }

    public RecyclorItemBean getCpTwo() {
        return cpTwo;
    }

    public void setCpTwo(RecyclorItemBean cpTwo) {
        this.cpTwo = cpTwo;
    }

    public RecyclorItemBean getCpThree() {
        return cpThree;
    }

    public void setCpThree(RecyclorItemBean cpThree) {
        this.cpThree = cpThree;
    }
}

package zuo.com.ui.bean;

import java.io.Serializable;

/**用于获取网络数据,recyclorview中的数据,子bean
 * Created by Administrator on 2016/10/10.
 */
public class RecyclorItemBean {

    private int id;
    private String title;
    private String imgUrl;

    public RecyclorItemBean(int id, String title, String imgUrl) {
        this.id = id;
        this.title = title;
        this.imgUrl = imgUrl;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }
}

3.调用封装的okhttp:

private void initList(final View view){

    String url ="http://112.124.22.238:8081/course_api/campaign/recommend";
    httpHelper.get(url, new SpotsCallBack<List<RecyclorBean>>(getContext()){


        @Override
        public void onSuccess(Response response, List<RecyclorBean> mbanners) {

            list = mbanners;
            initRecyclerView(view);
        }

        @Override
        public void onError(Response response, int code, Exception e) {

        }
    });


}

private void initRecyclerView(View view) {

    recyclerView= (RecyclerView) view.findViewById(R.id.recycler_view);
    homeCategoryAdapter=new HomeCategoryAdapter(list,getContext());
    //加载adapter
    recyclerView.setAdapter(homeCategoryAdapter);
    //必须写才能出现recyclorView,也可以是GirdLayoutManager,只是显示item的图形不一样
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    //设置分割线
    recyclerView.addItemDecoration(new HomeItemDecoration());

    homeCategoryAdapter.setOnItemClickListener(new HomeCategoryAdapter.OnItemClickListener() {
        @Override
        public void onClick(View v, int position, String city) {

            Toast.makeText(getContext(),"点击成功",Toast.LENGTH_SHORT).show();
        }
    });


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值