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(); } }); }