关于Retofit+rxjava使用

项目下载链接:http://download.csdn.net/download/dreamj1991/9797694

1.项目引入依赖:

compile 'io.reactivex:rxandroid:1.2.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile project(':http')
2.添加网络访问权限:
<uses-permission android:name="android.permission.INTERNET" />
3.添加依赖的http网络模块
4.定义APIserver
//    http://dwd-qa.aiabc.com.cn/api/home  登陆
@GET("home")
Observable<HomeDataResponse> getHomeData();
5.定义model
public class LoginModel {

    public static void login(String name,String password,final RequestImpl listener) {
        Map<String,String> map = new HashMap<>();
        map.put("username",name);
        map.put("password",password);
//        String authorization = GlobalData.getInstance().getAuthorization();
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                new Gson().toJson(map));
        Subscription subscription = HttpClient.Builder.getTestServer().login(requestBody)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MemberModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("test", "retrofit error " + e.getMessage());

                    }

                    @Override
                    public void onNext(MemberModel memberModel) {
                        Log.i("test", "retrofit success " + memberModel.toString());
                        listener.loadSuccess(memberModel.getData());
//                        RxBus.getDefault().post(RxCodeConstants.JUMP_TYPE_TO_ONE, new RxBusBaseMessage());
                    }
                });
        listener.addSubscription(subscription);
    }
}
6.Activity调用即可
 

参考文章:http://gank.io/post/56e80c2c677659311bed9841 

retofit官网:http://square.github.io/retrofit/ 


    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值