Android应用架构之Retrofit

Retrofit

可以自动解析Json为bean

是squareup公司出品,还有okhttp,picasso

可以配合RxJava实现网络请求

用法:
1、获取Retrofit。Builder创建过程中,做一些配置
我们一会用到的配置:设置一个主url地址,自动解析json。如果使用RxJava还需要配置和RxJava适配的部分
2、封装bean之后,创建请求数据的方法,所有请求方法定义在接口中,官方命名规范叫做XXXHttpService
3、想办法获取刚才创建的接口的实例,就可以去调用请求网络数据的方法,还是刚才我们自己定义的方法
4、调用方法后,将网络请求加入队列中,获得结果:成功/失败

5、如果成功,我们就获得到了已经自动解析过的数据模型类bean,我们再提取出想要的数据即可。



需要添加的依赖有:

    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
    compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.github.bumptech.glide:glide:3.8.0'
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    compile 'io.reactivex.rxjava2:rxjava:2.1.5'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    testCompile 'junit:junit:4.12'


    compile 'com.wang.avi:library:1.0.0'



需要添加的网络权限是:

<uses-permission android:name="android.permission.INTERNET"/>




布局文件.xml:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    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"
    tools:context="com.example.rxjava.activity.MainActivity">

    <Button
        android:id="@+id/btn1"
        android:text="create"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btn2"
        android:text="map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btn3"
        android:text="concatMap"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/start_second_btn"
        android:text="Retrofit+RxJava"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

activity_retrofit_rx_java.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context="com.example.rxjava.activity.RetrofitRxJavaActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>

    <com.wang.avi.AVLoadingIndicatorView
        android:id="@+id/loading_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        app:indicator="LineScaleParty"
        app:indicator_color="@color/colorAccent"/>

</RelativeLayout>

item_dish.xml

<?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"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <ImageView
        android:id="@+id/pic_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>




Java文件目录下创建activity、adapter、bean、httpservice四个文件夹:

activity目录下:

MainActivity.java

package com.example.rxjava.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.rxjava.R;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    protected Button mBtn1;
    protected Button mStartSecondBtn;
    private Button mBtn2;
    private Button mBtn3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
        initView();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                test1();
                break;
            case R.id.btn2:
                test2();
                break;
            case R.id.btn3:
                test3();
                break;
            case R.id.start_second_btn:
                startActivity(new Intent(this, RetrofitRxJavaActivity.class));
                break;
        }
    }

    private void initView() {
        mBtn1 = (Button) findViewById(R.id.btn1);
        mBtn1.setOnClickListener(MainActivity.this);
        mBtn2 = (Button) findViewById(R.id.btn2);
        mBtn2.setOnClickListener(this);
        mBtn3 = (Button) findViewById(R.id.btn3);
        mBtn3.setOnClickListener(this);
        mStartSecondBtn = (Button) findViewById(R.id.start_second_btn);
        mStartSecondBtn.setOnClickListener(this);
    }

    // 初步认识RxJava,使用create操作符
    private void test1() {
        // 原始数据
        final String cpu = "五行阴阳帝皇";
        // 给观察者设置数据源
        Observable<String> observable = Observable.just(cpu);
        // 操作数据:将cpu加工成手机
        observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<String> emitter) throws Exception {
                // 加工数据
                String phone = cpu + "天地人三才修罗";

                // 如果数据变换没问题,就可以将最终数据交给订阅者
                emitter.onNext(phone);
//                emitter.onError();
            }
        })
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        Log.d("1510", "地水火风四象雷霆:" + s);
                    }
                });

    }

    // map:数据变换


    // 最终获取平方值展示
    private void test2() {
        String startData = "12";
        // 给观察者(工人)设置数据源
        Observable<String> observable = Observable.just(startData);
        // 第一次变换:将字符串12转换为数字12
        observable.map(new Function<String, Integer>() {
            @Override
            public Integer apply(@NonNull String s) throws Exception {
                return Integer.valueOf(s);
            }
        })
                // 第二次变换:将数字12做平方处理
                .map(new Function<Integer, Integer>() {
                    @Override
                    public Integer apply(@NonNull Integer integer) throws Exception {
                        return integer * integer;
                    }
                })
                // 最终,订阅者拿到最终数据并进行消费
                .subscribe(new Consumer<Integer>() {
                               @Override
                               public void accept(Integer integer) throws Exception {
                                   Log.d("1510", "最终得到数据:" + integer);
                               }
                           }
                        , new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                Log.d("1510", "error: " + throwable.getMessage());
                            }
                        });
    }

    // concatMap:将集合中的数据拆分成独立的数据
    // flatMap和concatMap的区别:
    // concatMap一定按顺序拆分数据
    // flatMap不一定按顺序拆分数据,有可能出现数据顺序错乱
    private void test3() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add("数据" + i);
        }

        // 设置数据源
        Observable<List<String>> observable = Observable.just(list);
        // 将List集合拆分成单独数据:List<String> -> String
        observable.concatMap(new Function<List<String>, ObservableSource<String>>() {
            @Override
            public ObservableSource<String> apply(@NonNull List<String> list) throws Exception {
                // 将一个有List集合的观察者拆分成10个有String数据的观察者
                return Observable.fromIterable(list);
            }
        })
                // 订阅者接收每一个带有String数据的观察者
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        Thread.sleep(1000);
                        Log.d("1510", "接收到的每一个数据:" + s);
                    }
                });
    }

}

RetrofitRxJavaActivity.java

package com.example.rxjava.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.rxjava.R;
import com.example.rxjava.adapter.DishAdapter;
import com.example.rxjava.bean.DishBean;
import com.example.rxjava.httpservice.DishHttpService;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 初始化UI
 * 获取网络数据
 * Retrofit+RxJava
 * 展示
 */
// http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1
public class RetrofitRxJavaActivity extends AppCompatActivity {

    protected RecyclerView mRecyclerView;
    protected AVLoadingIndicatorView mLoadingView;
    private DishAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_retrofit_rx_java);
        initView();
        initData();
    }

    private void initView() {
        mLoadingView = (AVLoadingIndicatorView) findViewById(R.id.loading_view);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        mAdapter = new DishAdapter(this);
        mRecyclerView.setAdapter(mAdapter);
    }

    // 初始化网络数据
    private void initData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.qubaobei.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        DishHttpService httpService = retrofit.create(DishHttpService.class);
        Observable<DishBean> observable = httpService.getDishObservable(1);
        // 先将数据下载解析等操作跳转至子线程(数据变换也在子线程里)
//        observable.subscribeOn();// 跳转子线程
//        observable.observeOn();// 跳转主线程
        observable.subscribeOn(Schedulers.io())
                // 使用map操作符变换数据:DishBean -> List<DataBean>
                .map(new Function<DishBean, List<DishBean.DataBean>>() {
                    @Override
                    public List<DishBean.DataBean> apply(@NonNull DishBean dishBean) throws Exception {
                        return dishBean.getData();
                    }
                })
                // 将最终数据返回主线程
                .observeOn(AndroidSchedulers.mainThread())
                // 接收最终数据是List集合
                .subscribe(new Consumer<List<DishBean.DataBean>>() {
                               @Override
                               public void accept(List<DishBean.DataBean> datas) throws Exception {
                                   // 数据加载成功,添加到适配器中展示
                                   mAdapter.addDatas(datas);
                                   mLoadingView.setVisibility(View.GONE);
                                   Toast.makeText(RetrofitRxJavaActivity.this, "size:" + datas.size(), Toast.LENGTH_SHORT).show();
                                   Log.d("1510", "size: " + datas.size());
                               }
                           }
                        , new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                Log.d("1510", "error: " + throwable.getMessage());
                            }
                        });
    }
}


adapter目录下:

DishAdapter.java

package com.example.rxjava.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.rxjava.R;
import com.example.rxjava.bean.DishBean;

import java.util.ArrayList;
import java.util.List;

public class DishAdapter extends RecyclerView.Adapter<DishAdapter.ViewHolder> {

    private Context mContext;
    private List<DishBean.DataBean> mDatas;

    public DishAdapter(Context context) {
        mContext = context;
        mDatas = new ArrayList<>();
    }

    // 为了方便后期分页加载数据,单独封装一个方法,添加数据
    public void addDatas(List<DishBean.DataBean> datas) {
        mDatas.addAll(datas);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext)
                .inflate(R.layout.item_dish, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        DishBean.DataBean data = mDatas.get(position);

        holder.mTitleTextView.setText(data.getTitle());
        // Glide加载图片,和Picasso超级相似
        Glide.with(mContext)
                .load(data.getPic())
                .into(holder.mPicImageView);
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView mTitleTextView;
        ImageView mPicImageView;

        public ViewHolder(View itemView) {
            super(itemView);

            mTitleTextView = (TextView) itemView.findViewById(R.id.title_text_view);
            mPicImageView = ((ImageView) itemView.findViewById(R.id.pic_image_view));
        }
    }

}


bean目录下:

DishBean.java

package com.example.rxjava.bean;

import java.util.List;

public class DishBean {
    /**
     * ret : 1
     * data : [{"id":"8289","title":"油焖大虾","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg","collect_num":"1649","food_str":"大虾 葱 生姜 植物油 料酒","num":1649},{"id":"2127","title":"四川回锅肉","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2127.jpg","collect_num":"1583","food_str":"猪肉 青蒜 青椒 红椒 姜片","num":1583},{"id":"30630","title":"超简单芒果布丁","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/31/30630.jpg","collect_num":"1515","food_str":"QQ糖 牛奶 芒果","num":1515},{"id":"9073","title":"家常红烧鱼","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9073.jpg","collect_num":"1416","food_str":"鲜鱼 姜 葱 蒜 花椒","num":1416},{"id":"10097","title":"家常煎豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10097.jpg","collect_num":"1401","food_str":"豆腐 新鲜红椒 青椒 葱花 油","num":1401},{"id":"10509","title":"水煮肉片","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10509.jpg","collect_num":"1335","food_str":"瘦猪肉 生菜 豆瓣酱 干辣椒 花椒","num":1335},{"id":"46968","title":"红糖苹果银耳汤","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/47/46968.jpg","collect_num":"1242","food_str":"银耳 苹果 红糖","num":1242},{"id":"10191","title":"麻婆豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10191.jpg","collect_num":"1211","food_str":"豆腐 肉末 生抽 白糖 芝麻油","num":1211},{"id":"2372","title":"皮蛋瘦肉粥","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2372.jpg","collect_num":"1142","food_str":"大米 皮蛋 猪肉 油条 香葱","num":1142},{"id":"2166","title":"蚂蚁上树","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2166.jpg","collect_num":"1135","food_str":"红薯粉 肉 姜 蒜 花椒","num":1135}]
     */

    private int ret;
    private List<DataBean> data;

    public int getRet() {
        return ret;
    }

    public void setRet(int ret) {
        this.ret = ret;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * id : 8289
         * title : 油焖大虾
         * pic : http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg
         * collect_num : 1649
         * food_str : 大虾 葱 生姜 植物油 料酒
         * num : 1649
         */

        private String id;
        private String title;
        private String pic;
        private String collect_num;
        private String food_str;
        private int num;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getPic() {
            return pic;
        }

        public void setPic(String pic) {
            this.pic = pic;
        }

        public String getCollect_num() {
            return collect_num;
        }

        public void setCollect_num(String collect_num) {
            this.collect_num = collect_num;
        }

        public String getFood_str() {
            return food_str;
        }

        public void setFood_str(String food_str) {
            this.food_str = food_str;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }
    }
}



httpservice目录下:

DishHttpService.java

package com.example.rxjava.httpservice;

import com.example.rxjava.bean.DishBean;

import io.reactivex.Observable;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface DishHttpService {

    // 获取请求数据的任务对象
//    Call<DishBean> getDishCall();

    // 获取产生数据的观察者对象
    @GET("ios/cf/dish_list.php?stage_id=1&limit=10")
    Observable<DishBean> getDishObservable(@Query("page") int page);

}



本案例到此结束,若Android  Studio版本过低,可能导致部分效果无法实现,建议更新3.0以上版本

猜你喜欢

转载自blog.csdn.net/qq_39745566/article/details/78916213