关于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 getHomeData();
5.定义model
 
  
public class LoginModel {

    public static void login(String name,String password,final RequestImpl listener) {
        Map 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() {
                    @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/ 


    


你可能感兴趣的:(安卓进阶)