从去年开始就比较流行 Rxjava,Rxandroid,当然也有Retrofit 。
git地址:https://github.com/meijius/RxRetrofitDemo
1.bulid.gradle 文件
compile 'io.reactivex:rxjava:1.1.6'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'com.bigkoo:svprogresshud:1.0.2'
compile 'com.jakewharton:butterknife:7.0.1'
2.封装network请求框架。
Network.java
public class Network {
private static LiaoMeiApi zhuangbiApi;
private static OkHttpClient okHttpClient = new OkHttpClient();
private static Converter.Factory gsonConverterFactory = GsonConverterFactory.create();
private static CallAdapter.Factory rxJavaCallAdapterFactory = RxJavaCallAdapterFactory.create();
public static LiaoMeiApi getZhuangbiApi() {
NetworkDialogUtils.getInstance().ShowNetworkDialog(); // 加载框
if (zhuangbiApi == null) {
Retrofit retrofit = new Retrofit.Builder()
.client(okHttpClient)
.baseUrl("http://www.560797.com/")
.addConverterFactory(gsonConverterFactory)
.addCallAdapterFactory(rxJavaCallAdapterFactory)
.build();
zhuangbiApi = retrofit.create(LiaoMeiApi.class);
}
return zhuangbiApi;
}
}
RxObserver.java Rxjava 封装返回类
public abstract class RxObserver<T> extends Subscriber<T> {
@Override
public void onStart() {
super.onStart();
}
@Override
public void onCompleted() {
NetworkDialogUtils.getInstance().HideNetworkDialog();
}
@Override
public void onError(Throwable e) {
NetworkDialogUtils.getInstance().HideNetworkDialog();
Logger.e(this,e.getMessage());
e.printStackTrace();
//在这里做全局的错误处理
if (e instanceof HttpException) {
ToastUtils.getInstance().showToast(e.getMessage());
}
ToastUtils.getInstance().showToast(e.getMessage());
}
@Override
public void onNext(T t) {
onSuccess(t);
}
public abstract void onSuccess(T t);
}
LiaoMeiApi.java Retrofit接口类
public interface LiaoMeiApi {
/**
* 这里需要稍作说明,@GET注解就表示get请求,@Query表示请求参数,将会以key=value的方式拼接在url后面 @QueryMap 传map 把key value丢进去
* @POST注解就表示post请求,@Field表示请求参数,将会以表单的方式提交 @FieldMap 意思一样
* 除此之外还有 @Body @Path 添加header等方法
* @return
*/
// http://www.560797.com/sms/login.html account password
@FormUrlEncoded
@POST("sms/login")
Observable<Rersults> login(@Field(value = "account", encoded = true) String account, @Field(value = "password", encoded = true) String pwd);
}
LoginActivity.java
public class LoginActivity extends AppCompatActivity {
@Bind(R.id.email)
AutoCompleteTextView email;
@Bind(R.id.password)
EditText password;
@Bind(R.id.content)
TextView content;
String account, pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
NetworkDialogUtils.init(this);
}
@OnClick({R.id.email_sign_in_button})
public void onClick(View view){
switch (view.getId()){
case R.id.email_sign_in_button:
login();
break;
}
}
Observer<Rersults> observer = new RxObserver<Rersults>() {
@Override
public void onSuccess(Rersults s) {
Toast.makeText(LoginActivity.this, s.getErrmsg(), Toast.LENGTH_SHORT).show();
content.setText(s.toString());
}
};
protected Subscription subscription;
private void login(){
account = email.getText().toString();
pwd = password.getText().toString();
if(TextUtils.isEmpty(account)){
Toast.makeText(LoginActivity.this, "用户名不能为空!", Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(pwd)){
Toast.makeText(LoginActivity.this, "密码不能为空!", Toast.LENGTH_SHORT).show();
return;
}
subscription = Network.getZhuangbiApi()
.login("a123456", "a123456")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
}
}
4.其它相关类。
APPApplication
public class APPApplication extends Application {
public void onCreate() {
super.onCreate();
init();
}
private void init() {
ToastUtils.init(this);
}
}
NetworkDialogUtils
public class NetworkDialogUtils {
private static Context mContext;
private static NetworkDialogUtils mInstance;
private static SVProgressHUD svProgressHUD;
private static boolean isShowHUD = true;
public static NetworkDialogUtils getInstance() {
return mInstance;
}
public static void init(Context ctx) {
mInstance = new NetworkDialogUtils(ctx);
}
private NetworkDialogUtils(Context ctx) {
mContext = ctx;
}
public void ShowNetworkDialog(){
if(isShowHUD){
if(svProgressHUD == null){
svProgressHUD = new SVProgressHUD(mContext);
}
svProgressHUD.showWithStatus(mContext.getString(R.string.wait));
}
}
public void HideNetworkDialog(){
if(isShowHUD){
if(svProgressHUD.isShowing()){
svProgressHUD.dismiss();
}
}
}
}
相关图片:
git地址:https://github.com/meijius/RxRetrofitDemo