Demo2:Retrofit+Rxjava+Okhttp+Gson+Fresco+Butterknife
生活随笔
收集整理的這篇文章主要介紹了
Demo2:Retrofit+Rxjava+Okhttp+Gson+Fresco+Butterknife
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
底層build.gradle
buildscript {repositories {google()jcenter()mavenCentral() // add repository}dependencies {classpath 'com.android.tools.build:gradle:3.0.0'classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
本app的build.gradle
apply plugin: 'com.android.application' apply plugin: 'org.greenrobot.greendao' implementation 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' compile 'com.android.support:design:27.+'implementation 'com.squareup.retrofit2:converter-gson:2.0.2' implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' implementation 'io.reactivex.rxjava2:rxjava:2.0.2' implementation 'com.squareup.retrofit2:retrofit:2.0.2' implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' implementation 'org.greenrobot:greendao:3.2.2' compile 'com.facebook.fresco:fresco:0.12.0' compile 'com.facebook.fresco:animated-base-support:0.12.0'// 支持 GIF 動(dòng)圖,需要添加 compile 'com.facebook.fresco:animated-gif:0.12.0'// 支持 WebP (靜態(tài)圖+動(dòng)圖),需要添加 compile 'com.facebook.fresco:animated-webp:0.12.0' compile 'com.facebook.fresco:webpsupport:0.12.0'AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" /><applicationandroid:name=".app.App"?
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.kson.twolianxi01.MainActivity"><FrameLayoutandroid:id="@+id/fragm"android:layout_width="match_parent"android:layout_weight="9"android:layout_height="0dp" /><RadioGroupandroid:id="@+id/rap"android:layout_weight="1"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="0dp"><RadioButtonandroid:id="@+id/rap1"android:layout_weight="1"android:textSize="20sp"android:text="首頁"android:button="@null"android:gravity="center"android:layout_width="0dp"android:layout_height="match_parent" /><RadioButtonandroid:id="@+id/rap2"android:layout_weight="1"android:textSize="20sp"android:text="附近"android:button="@null"android:gravity="center"android:layout_width="0dp"android:layout_height="match_parent" /><RadioButtonandroid:id="@+id/rap3"android:layout_weight="1"android:textSize="20sp"android:text="我的"android:button="@null"android:gravity="center"android:layout_width="0dp"android:layout_height="match_parent" /></RadioGroup></LinearLayout>frag1.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.kson.twolianxi01.fragment.Frag1"><android.support.v7.widget.RecyclerViewandroid:id="@+id/recycleview"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout>include1.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"xmlns:fresco="http://schemas.android.com/apk/res-auto"android:orientation="vertical"android:layout_height="match_parent"><com.facebook.drawee.view.SimpleDraweeViewandroid:id="@+id/my_image_view"android:layout_width="130dp"android:layout_height="130dp"fresco:placeholderImage="@mipmap/ic_launcher"/><TextViewandroid:id="@+id/title1"android:textSize="20sp"android:text="title1"android:layout_width="130dp"android:layout_height="wrap_content" /></LinearLayout>MainActivity
public class MainActivity extends AppCompatActivity {@BindView(R.id.fragm)FrameLayout fragm;@BindView(R.id.rap)RadioGroup rap;private FragmentManager manager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//初始化ButterKnife.bind(this);//創(chuàng)建對象final Frag1 frag1 = new Frag1();final Frag2 frag2 = new Frag2();final Frag3 frag3 = new Frag3();// 事務(wù)提交manager = getSupportFragmentManager();//默認(rèn)使用第一個(gè)manager.beginTransaction().replace(R.id.fragm,frag1).commit();//條目的監(jiān)聽事件rap.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup radioGroup, int i) {//獲取事務(wù)FragmentTransaction transaction = manager.beginTransaction();switch (i){case R.id.rap1:transaction.replace(R.id.fragm,frag1);break;case R.id.rap2:transaction.replace(R.id.fragm,frag2);break;case R.id.rap3:transaction.replace(R.id.fragm,frag3);break;}//提交事務(wù)transaction.commit();}});} }HttpUtils
public class HttpUtils {private static HttpUtils httpUtils;private OkHttpClient okHttpClient;//有參private HttpUtils(){okHttpClient = new OkHttpClient.Builder().addInterceptor(new longinterceptor()).build();}//創(chuàng)建攔截器class longinterceptor implements Interceptor{@Overridepublic Response intercept(Chain chain) throws IOException {Request request = chain.request();//創(chuàng)建方法String method = request.method();//請求地址HttpUrl url = request.url();//打印日志信息Log.i("xxx",method+"===="+url);//響應(yīng)Response response = chain.proceed(request);return response;}}//單例public static HttpUtils getinstance(){if (httpUtils==null){synchronized (HttpUtils.class){if (httpUtils==null){httpUtils = new HttpUtils();}}}return httpUtils;}//get方法public Api getpost(String base){Retrofit retrofit = new Retrofit.Builder().client(okHttpClient)//拼接地址.baseUrl(base)//添加retrofit對rxjava的支持.addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加一個(gè)gson對象.addConverterFactory(GsonConverterFactory.create()).build();//獲取接口Api api = retrofit.create(Api.class);return api;} }Api
public interface Api {@GET("content_lists&version=1.7&token=&type=1&cat=&catid2=&page=1")Observable<news> getresponse(); }app
public class app extends Application {private static app app;private DaoSession daoSession;@Overridepublic void onCreate() {super.onCreate();Fresco.initialize(this);app =app.this;//獲取數(shù)據(jù)庫DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "beiw");//獲取對象SQLiteDatabase db = helper.getWritableDatabase();//獲取數(shù)據(jù)DaoMaster daoMaster = new DaoMaster(db);//數(shù)據(jù)庫的增刪改查daoSession = daoMaster.newSession();}public static app getinstance(){if (app==null){app =new app();}return app;}public DaoSession getdoasession(){return daoSession;} }news
format
work
@Entity public class work {@Id(autoincrement = true)Long id;@PropertyString imgageurl;@PropertyString title1;build--make projeck
constant
public class constant {//定義接口地址public static final String URL="https://app.tuozhe8.com/api.php/api/Lists/content_lists&version=1.7&token=&type=1&cat=&catid2=&page=1";//定義根地址public static final String BASE_URL="https://app.tuozhe8.com/api.php/api/Lists/"; }icontract
public interface icontract {/*** iview層*/public interface iview{//顯示方法void showdata(List<work> works);}/*** ipresenter*/public interface ipresenter<iview>{//關(guān)聯(lián)void attachview(iview iview);//取消void detachview(iview iview);//邏輯void requestinfo();}/*** imoudle**/public interface imoudle{//接口回調(diào)public interface callisten{//信息回顯void responsemsg(List<work> works);}void requestdata(callisten callisten);} }moudleimp
public class moudleimp implements icontract.imoudle {@Overridepublic void requestdata(final callisten callisten) {//獲取數(shù)據(jù)庫表final workDao workDao = app.getinstance().getdoasession().getWorkDao();//進(jìn)行全查final List<work> works = workDao.loadAll();//判斷if (works.size()>0){//進(jìn)行接口回傳callisten.responsemsg(works);return;}//查不到進(jìn)行網(wǎng)絡(luò)請求HttpUtils httpUtils = HttpUtils.getinstance();//獲取接口Api api = httpUtils.getpost(constant.BASE_URL);//獲取接口中的方法Observable<news> observable = api.getresponse();//獲取具體的參數(shù)observable//運(yùn)行在io線程.subscribeOn(Schedulers.io())//回調(diào)在主線程.observeOn(AndroidSchedulers.mainThread())//進(jìn)行不完全回調(diào).subscribe(new Consumer<news>() {//成功@Overridepublic void accept(news news) throws Exception {//獲取具體的對象List<com.example.kson.twolianxi01.bean.news.DataBean> data = news.getData();//創(chuàng)建新的集合進(jìn)行回傳ArrayList<work> works1 = new ArrayList<>();//進(jìn)行遍歷for (int i = 0; i <data.size() ; i++) {String title1 = data.get(i).getTitle();String logo = data.get(i).getLogo();//創(chuàng)建對象work work = new work();work.setTitle1(title1);work.setImgageurl(logo);works1.add(work);}callisten.responsemsg(works1);workDao.insertInTx(works1);}}, new Consumer<Throwable>() {//失敗@Overridepublic void accept(Throwable throwable) throws Exception {callisten.responsemsg(null);}});} }presenterimp
public class presenterimp implements icontract.ipresenter<icontract.iview> {private icontract.iview iview;private com.example.kson.twolianxi01.model.moudleimp moudleimp;private WeakReference<icontract.iview> iviewWeakReference;private WeakReference<icontract.imoudle> weakReference;@Overridepublic void attachview(icontract.iview iview) {this.iview = iview;moudleimp = new moudleimp();//弱引用解綁iviewWeakReference = new WeakReference<>(iview);weakReference = new WeakReference<icontract.imoudle>(moudleimp);}@Overridepublic void detachview(icontract.iview iview) {iviewWeakReference.clear();weakReference.clear();}@Overridepublic void requestinfo() {//調(diào)用方法moudleimp.requestdata(new icontract.imoudle.callisten() {@Overridepublic void responsemsg(List<work> works) {//調(diào)用view方法iview.showdata(works);}});} }myadapter
public class myadapter extends RecyclerView.Adapter<myadapter.oneholder> {private Context context;private List<work> list;public myadapter(Context context, List<work> list) {this.context = context;this.list = list;}@Overridepublic oneholder onCreateViewHolder(ViewGroup parent, int viewType) {View view = LayoutInflater.from(context).inflate(R.layout.include1, null);oneholder oneholder = new oneholder(view);return oneholder;}@Overridepublic void onBindViewHolder(oneholder holder, int position) {holder.title1.setText(list.get(position).getTitle1());Uri uri = Uri.parse(list.get(position).getImgageurl());holder.my_image_view.setImageURI(uri);}@Overridepublic int getItemCount() {return list.size();}//創(chuàng)建視圖class oneholder extends RecyclerView.ViewHolder{private final SimpleDraweeView my_image_view;private final TextView title1;public oneholder(View itemView) {super(itemView);//初始化my_image_view = itemView.findViewById(R.id.my_image_view);title1 = itemView.findViewById(R.id.title1);}} }Frag1
public class Frag1 extends Fragment implements icontract.iview {@BindView(R.id.recycleview)RecyclerView recycleview;Unbinder unbinder;private presenterimp presenterimp;@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.frag1, container, false);unbinder = ButterKnife.bind(this, view);presenterimp = new presenterimp();//關(guān)聯(lián)presenterimp.attachview(this);//請求presenterimp.requestinfo();return view;}@Overridepublic void onDestroyView() {super.onDestroyView();unbinder.unbind();//取消關(guān)聯(lián)presenterimp.detachview(this);}//具體操作@Overridepublic void showdata(List<work> works) {//創(chuàng)建布局管理器GridLayoutManager manager = new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false);//添加布局管理器recycleview.setLayoutManager(manager);//創(chuàng)建適配器myadapter myadapter = new myadapter(getActivity(),works);//設(shè)置適配器recycleview.setAdapter(myadapter);} }總結(jié)
以上是生活随笔為你收集整理的Demo2:Retrofit+Rxjava+Okhttp+Gson+Fresco+Butterknife的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 错误接受率 (FAR), 错误拒绝率(F
- 下一篇: EER(等概率错误)