基本上跟之前写的okhttpUtils加fastJson差不多
okhttpUtils和retrofit都是对于okhttp的封装
但是retrofit和okhttp都是Square开源的轻量级框架
gradle:
compile 'com.squareup.okhttp3:okhttp:3.1.2' compile 'com.squareup.retrofit2:retrofit:2.0.2' //这俩是关键 retrofit的使用 compile 'com.squareup.retrofit2:converter-gson:2.0.2'//gson 也可以了解一下fastJson compile 'com.android.support:design:26.0.0-alpha1'//材料设计语言 recyclerView时用到 compile 'com.zhy:base-rvadapter:3.0.3'//万能适配器 compile 'com.github.bumptech.glide:glide:3.7.0'//网络图片加载 compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3'//刷新
http://www.imooc.com/api/teacher?type=4&num=10 //json数据
效果图
layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_retrofit"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.iamchan.allfunctiontest.RetrofitActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/smart"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyc"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
</RelativeLayout>
item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="100dp">
<ImageView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="match_parent" />
<LinearLayout
android:layout_marginLeft="5dp"
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:maxLines="1"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="0dp" />
<TextView
android:id="@+id/content"
android:maxLines="2"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<View
android:background="#f5f5f5"
android:layout_width="match_parent"
android:layout_height="2dp"></View>
</LinearLayout>
json数据格式:
{
"status": 1,
"data": [{
"id": 1,
"name": "Tony老师聊shell——环境变量配置文件",
"picSmall": "http:\/\/img.mukewang.com\/55237dcc0001128c06000338-300-170.jpg",
"picBig": "http:\/\/img.mukewang.com\/55237dcc0001128c06000338.jpg",
"description": "为你带来shell中的环境变量配置文件",
"learner": 12312
}],
"msg": "成功"
}
实体类;
最外层
属性与与json格式对应
可以简单理解 见着{}就是class
见着【】就是list 可以参照之前写的fastjson
public class Teacher {
private int status;
private String msg;
private List<Course> data;
public Teacher() {
}
public Teacher(int status, String msg, List<Course> data) {
this.status = status;
this.msg = msg;
this.data = data;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public List<Course> getData() {
return data;
}
public void setData(List<Course> data) {
this.data = data;
}
}
里层
属性与json数据对应
public class Course {
private int id;
private String name;
private String picSmall;
private String picBig;
private String description;
private int learner;
public Course() {
}
public Course(int id, String name, String picSmall, String picBig, String description, int learner) {
this.id = id;
this.name = name;
this.picSmall = picSmall;
this.picBig = picBig;
this.description = description;
this.learner = learner;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPicSmall() {
return picSmall;
}
public void setPicSmall(String picSmall) {
this.picSmall = picSmall;
}
public String getPicBig() {
return picBig;
}
public void setPicBig(String picBig) {
this.picBig = picBig;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getLearner() {
return learner;
}
public void setLearner(int learner) {
this.learner = learner;
}
}
接口:
使用retrofit需要定义接口
这个接口主要有两个东西 一个是get请求 ()中是什么 http://www.imooc.com/api/teacher?type=4&num=10
下面是一个方法 getCall()方法 里面有参数 参数是什么 http://www.imooc.com/api/teacher?type=4&num=10
retrofit采用注释的形式
public interface IRequest {
@GET("api/teacher")/*api/teacher?type=4&num=10*/
Call<Teacher> getCall(@Query("type") String type, @Query("num")String num);
}
java代码
private RecyclerView recyc;
private CommonAdapter comm;
private List<Course> courseList;
private SmartRefreshLayout smart;
private int type=4;
private int num=10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrofit);
recyc= (RecyclerView) findViewById(R.id.recyc);
smart= (SmartRefreshLayout) findViewById(R.id.smart);
initRecyc();
request();
}
private void initRecyc() {
courseList=new ArrayList<>();
comm=new CommonAdapter(RetrofitActivity.this,R.layout.item,courseList) {
@Override
protected void convert(ViewHolder holder, Object o, int position) {
Glide.with(RetrofitActivity.this).load(courseList.get(position).getPicSmall()).centerCrop().into((ImageView) holder.getView(R.id.img));
holder.setText(R.id.name,courseList.get(position).getName());
holder.setText(R.id.content,courseList.get(position).getDescription());
}
};
recyc.setAdapter(comm);
LinearLayoutManager lin=new LinearLayoutManager(this);
lin.setOrientation(LinearLayoutManager.VERTICAL);
recyc.setLayoutManager(lin);
smart.setEnableRefresh(false);
smart.setOnLoadmoreListener(new OnLoadmoreListener() {
@Override
public void onLoadmore(RefreshLayout refreshlayout) {
num+=10;
courseList.clear();
request();
smart.finishLoadmore();
}
});
}
private void request() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.imooc.com/") // 设置网络请求 Url
.addConverterFactory(GsonConverterFactory.create()) //设置使用Gson解析(记得加入依赖)
.build();
IRequest iRequest=retrofit.create(IRequest.class);
Call<Teacher> call=iRequest.getCall(type+"",num+""); //这就是方法传进去的参数
call.enqueue(new Callback<Teacher>() {
@Override
public void onResponse(Call<Teacher> call, Response<Teacher> response) {
List<Course> courses=response.body().getData();
for(int i=0;i<courses.size();i++){
courseList.add(courses.get(i));
}
comm.notifyDataSetChanged();
}
@Override
public void onFailure(Call<Teacher> call, Throwable t) {
//fail时调用
}
});
}
}