okthpp实现步骤:
1.导包
//butterKnife compile 'com.jakewharton:butterknife:8.4.0' apt 'com.jakewharton:butterknife-compiler:8.4.0' //okhttp compile 'com.squareup.okio:okio:1.5.0' compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' compile 'com.google.code.gson:gson:2.8.2' //Recyclerview compile 'com.android.support:recyclerview-v7:22.1.1' compile 'com.android.support:appcompat-v7:22.1.1' compile files('libs/universal-image-loader-1.9.3.jar')
2.添加权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
4.在布局文件里面设置RecyclerView控件
<android.support.v7.widget.RecyclerView
android:id="@+id/id_recyclerview"
android:divider="#ffff0000"
android:dividerHeight="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
5、写一个item布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/texts"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#f0f"
android:gravity="center"
android:textSize="20sp" />
</LinearLayout>
6.分装一个Bean类
public class Bean {
private int code;
private List<Data> data;
private String msg;
public Bean(int code, List<Data> data, String msg) {
this.code = code;
this.data = data;
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public List<Data> getData() {
return data;
}
public void setData(List<Data> data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public String toString() {
return "Bean{" +
"code=" + code +
", data=" + data +
", msg='" + msg + '\'' +
'}';
}
public class Data {
private int id;
private String efficacy;
private String goods_img;
public Data(int id, String efficacy, String goods_img) {
this.id = id;
this.efficacy = efficacy;
this.goods_img = goods_img;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEfficacy() {
return efficacy;
}
public void setEfficacy(String efficacy) {
this.efficacy = efficacy;
}
public String getGoods_img() {
return goods_img;
}
public void setGoods_img(String goods_img) {
this.goods_img = goods_img;
}
@Override
public String toString() {
return "Data{" +
"id=" + id +
", efficacy='" + efficacy + '\'' +
", goods_img='" + goods_img + '\'' +
'}';
}
}
}
6.Okhttp工具类
GsonArrayCallback: