简单实用Retrofit和Rajava

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


导入config.gradle

ext {
    android = [
            compileSdkVersion: 27,
            applicationId    : "com.example.retrfitrxjava",
            minSdkVersion    : 15,
            targetSdkVersion : 27,
            versionCode      : 1,
            versionName      : "1.0"
    ]

    dependencies = [
            "appcompat-v7"     : "com.android.support:appcompat-v7:27.1.1",
            "constraint-layout": "com.android.support.constraint:constraint-layout:1.1.0",
            "retrofit2"        : "com.squareup.retrofit2:retrofit:2.4.0",
            "converter-gson"   : "com.squareup.retrofit2:converter-gson:2.4.0",
            "adapter-rxjava"  :  "com.squareup.retrofit2:adapter-rxjava2:2.4.0",
            "rxjava2"          : "io.reactivex.rxjava2:rxjava:2.1.13",
            "rxjava2:rxandroid": "io.reactivex.rxjava2:rxandroid:2.0.2",
            "dagger"           : "com.google.dagger:dagger:2.8",
            "dagger-compiler" : "com.google.dagger:dagger-compiler:2.7"

    ]
}

外部build

build.gradle

//加上
apply from:"config.gradle"


里面build加上依赖

build.gradle

implementation "com.squareup.retrofit2:retrofit:2.4.0"
implementation "com.squareup.retrofit2:converter-gson:2.4.0"
implementation  "com.squareup.retrofit2:adapter-rxjava2:2.4.0"
implementation  "io.reactivex.rxjava2:rxjava:2.1.13"
implementation "io.reactivex.rxjava2:rxandroid:2.0.2"
implementation "com.google.dagger:dagger:2.8"

LoginApiService

package com.example.retrfitrxjava;

import io.reactivex.Observable;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

/**
 * 姓名:${user}
 * 时间:${date}
 */

public interface LoginApiService {

    //登录

    @FormUrlEncoded
    @POST("user/login")
    Call<UserBean>  login(@Field("mobile") String mobile,@Field("password") String password);

    //登录2
    @FormUrlEncoded
    @POST("user/login")
    Observable<UserBean> login2(@Field("mobile") String mobile,@Field("password")  String password);

}

UserBean

package com.example.retrfitrxjava;

/**
 * 姓名:${user}
 * 时间:${date}
 */

public class UserBean {

    /**
     * msg : 登录成功
     * code : 0
     * data : {"age":null,"appkey":"032860bad4c453c0","appsecret":"D1D54601A094983A38000563A1ECC720","createtime":"2018-05-10T19:54:36","email":null,"fans":null,"follow":null,"gender":null,"icon":null,"latitude":null,"longitude":null,"mobile":"15133044161","money":null,"nickname":null,"password":"8F669074CAF5513351A2DE5CC22AC04C","praiseNum":null,"token":"C55773B10ABD8BFB6739BD5C16D6F803","uid":11068,"userId":null,"username":"15133044161"}
     */

    private String msg;
    private String code;
    private DataBean data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public DataBean getData() {
        return data;
    }

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

    public static class DataBean {
        /**
         * age : null
         * appkey : 032860bad4c453c0
         * appsecret : D1D54601A094983A38000563A1ECC720
         * createtime : 2018-05-10T19:54:36
         * email : null
         * fans : null
         * follow : null
         * gender : null
         * icon : null
         * latitude : null
         * longitude : null
         * mobile : 15133044161
         * money : null
         * nickname : null
         * password : 8F669074CAF5513351A2DE5CC22AC04C
         * praiseNum : null
         * token : C55773B10ABD8BFB6739BD5C16D6F803
         * uid : 11068
         * userId : null
         * username : 15133044161
         */

        private Object age;
        private String appkey;
        private String appsecret;
        private String createtime;
        private Object email;
        private Object fans;
        private Object follow;
        private Object gender;
        private Object icon;
        private Object latitude;
        private Object longitude;
        private String mobile;
        private Object money;
        private Object nickname;
        private String password;
        private Object praiseNum;
        private String token;
        private int uid;
        private Object userId;
        private String username;

        public Object getAge() {
            return age;
        }

        public void setAge(Object age) {
            this.age = age;
        }

        public String getAppkey() {
            return appkey;
        }

        public void setAppkey(String appkey) {
            this.appkey = appkey;
        }

        public String getAppsecret() {
            return appsecret;
        }

        public void setAppsecret(String appsecret) {
            this.appsecret = appsecret;
        }

        public String getCreatetime() {
            return createtime;
        }

        public void setCreatetime(String createtime) {
            this.createtime = createtime;
        }

        public Object getEmail() {
            return email;
        }

        public void setEmail(Object email) {
            this.email = email;
        }

        public Object getFans() {
            return fans;
        }

        public void setFans(Object fans) {
            this.fans = fans;
        }

        public Object getFollow() {
            return follow;
        }

        public void setFollow(Object follow) {
            this.follow = follow;
        }

        public Object getGender() {
            return gender;
        }

        public void setGender(Object gender) {
            this.gender = gender;
        }

        public Object getIcon() {
            return icon;
        }

        public void setIcon(Object icon) {
            this.icon = icon;
        }

        public Object getLatitude() {
            return latitude;
        }

        public void setLatitude(Object latitude) {
            this.latitude = latitude;
        }

        public Object getLongitude() {
            return longitude;
        }

        public void setLongitude(Object longitude) {
            this.longitude = longitude;
        }

        public String getMobile() {
            return mobile;
        }

        public void setMobile(String mobile) {
            this.mobile = mobile;
        }

        public Object getMoney() {
            return money;
        }

        public void setMoney(Object money) {
            this.money = money;
        }

        public Object getNickname() {
            return nickname;
        }

        public void setNickname(Object nickname) {
            this.nickname = nickname;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public Object getPraiseNum() {
            return praiseNum;
        }

        public void setPraiseNum(Object praiseNum) {
            this.praiseNum = praiseNum;
        }

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }

        public int getUid() {
            return uid;
        }

        public void setUid(int uid) {
            this.uid = uid;
        }

        public Object getUserId() {
            return userId;
        }

        public void setUserId(Object userId) {
            this.userId = userId;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }
    }
}

MainActivity

package com.example.retrfitrxjava;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

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

        //method1();
        method2();
    }

    private void method2() {

        //创建Retrofit和Rajava
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("https://www.zhaoapi.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加Rxjava支持
                .build();
        LoginApiService loginApiService=retrofit.create(LoginApiService.class);
        Observable<UserBean> login2 = loginApiService.login2("15133044161", "123456");

        login2
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<UserBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UserBean userBean) {
                        //表示网络请求成功的回调
                        System.out.print("");
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void method1() {

        //创建retrofit
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("https://www.zhaoapi.cn/")
                .addConverterFactory(GsonConverterFactory.create())//底层自定义使用Gson进行解析
                .build();
        //关联接口
        LoginApiService loginApiService = retrofit.create(LoginApiService.class);

        Call<UserBean> loginCall = loginApiService.login("15133044161", "123456");
        //开始网络请求
        loginCall.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                //底层做了处理,不用Handler去切换到主线程
                UserBean body = response.body();

            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {

            }
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值