导入的权限
implementation 'org.greenrobot:eventbus:3.1.1'
implementation 'com.facebook.fresco:fresco:1.13.0'
implementation 'com.jakewharton:butterknife:8.4.0'
annotationProcessor "com.jakewharton:butterknife-compiler:8.4.0"
implementation 'com.squareup.retrofit2:retrofit:2.0.0'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
implementation 'com.android.support:recyclerview-v7:28.+'
implementation 'org.greenrobot:greendao:3.0.1'
implementation 'org.greenrobot:greendao-generator:3.0.0'
implementation 'com.android.support:design:28.0.0'
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
首先拼接接口
public class Api {
//http://www.xieast.com/api/news/news.php?page=1
public static final String BSEE_URL=“http://www.xieast.com/api/news/”;
public static final String SHOU=“news.php?page=1”;
}
public interface ApiService {
@GET
Observable<MyBean> shuo(@Url String url);
@GET
Observable<MyBean> nn(@Url String url,@HeaderMap HashMap<String,String> hashMap);
}
1
public class Aip {
//http://mnews.gw.com.cn/wap/data/news/news/mobile/jbgg/page_1.json
public static final String Base_Url="http://mnews.gw.com.cn/wap/data/news/news/";
public static final String Main_url="mobile/jbgg/page_";
}
2
public interface ApiService {
@GET(Aip.Main_url+"{page}")
Call<List<MyBean>> getShow(@Path("page") String page);
}
3解析接口中的数据
public class MyBean {
public List<DataBean> data;
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
@Override
public String toString() {
return "MyBean{" +
"data=" + data +
'}';
}
}
另一个类
@Entity
public class DataBean {
@Property(nameInDb = "title")
private String title;
@Override
public String toString() {
return "DataBean{" +
"title='" + title + '\'' +
'}';
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
@Generated(hash = 1538556504)
public DataBean(String title) {
this.title = title;
}
@Generated(hash = 908697775)
public DataBean() {
}
}
然后去写MVP框架
1写网络请求
public class RetrofitUtils {
//单例模式
private static RetrofitUtils retrofitUtils;
private RetrofitUtils(){
}
public static RetrofitUtils getInstance(){
if (retrofitUtils==null){
synchronized (RetrofitUtils.class){
if (retrofitUtils==null){
retrofitUtils=new RetrofitUtils();
}
}
}
return retrofitUtils;
}
//获取Retrofit方法
private static Retrofit retrofit;
private static synchronized Retrofit getRetrofit(String BASE_URL){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.bui