Skip to content

Commit

Permalink
Rx扩展完成
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonlzy committed Oct 1, 2016
1 parent b4ebbea commit ee7081e
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 16 deletions.
8 changes: 5 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
<activity android:name=".okgo.SyncActivity"/>
<activity android:name=".okgo.RedirectActivity"/>
<activity android:name=".okgo.TestActivity"/>

<activity
android:name=".cache.CacheDemoActivity"
android:theme="@style/AppTheme.NoActionBar"/>
Expand All @@ -56,11 +55,14 @@
<activity
android:name=".okserver.UploadActivity"
android:theme="@style/AppTheme.NoActionBar"/>

<activity android:name=".okrx.RxCommonActivity"/>
<!--Rx相关-->
<activity
android:name=".okrx.RxActivity"
android:theme="@style/AppTheme.NoActionBar"/>
<activity android:name=".okrx.RxCommonActivity"/>
<activity android:name=".okrx.RxBitmapActivity"/>
<activity android:name=".okrx.RxFileDownloadActivity"/>
<activity android:name=".okrx.RxFormUploadActivity"/>
</application>

</manifest>
29 changes: 29 additions & 0 deletions app/src/main/java/com/lzy/demo/base/BaseRxDetailActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.lzy.demo.base;

import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
* ================================================
* 作 者:jeasonlzy(廖子尧)Github地址:https://github.com/jeasonlzy
* 版 本:1.0
* 创建日期:16/10/1
* 描 述:统一管理所有的订阅生命周期
* 修订历史:
* ================================================
*/
public abstract class BaseRxDetailActivity extends BaseDetailActivity {

private CompositeSubscription compositeSubscription;

public void addSubscribe(Subscription subscription) {
if (compositeSubscription == null) {
compositeSubscription = new CompositeSubscription();
}
compositeSubscription.add(subscription);
}

public void unSubscribe() {
if (compositeSubscription != null) compositeSubscription.unsubscribe();
}
}
3 changes: 3 additions & 0 deletions app/src/main/java/com/lzy/demo/okrx/RxActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public void bind(int position, String[] strings) {
@Override
public void onClick(View v) {
if (position == 0) startActivity(new Intent(RxActivity.this, RxCommonActivity.class));
if (position == 1) startActivity(new Intent(RxActivity.this, RxBitmapActivity.class));
if (position == 2) startActivity(new Intent(RxActivity.this, RxFormUploadActivity.class));
if (position == 3) startActivity(new Intent(RxActivity.this, RxFileDownloadActivity.class));
}
}
}
66 changes: 66 additions & 0 deletions app/src/main/java/com/lzy/demo/okrx/RxBitmapActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.lzy.demo.okrx;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.lzy.demo.R;
import com.lzy.demo.base.BaseRxDetailActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;

public class RxBitmapActivity extends BaseRxDetailActivity {

@Bind(R.id.imageView) ImageView imageView;

@Override
protected void onActivityCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_bitmap_request);
ButterKnife.bind(this);
setTitle("请求图片");
}

@Override
protected void onDestroy() {
super.onDestroy();
//Activity销毁时,取消网络请求
unSubscribe();
}

@OnClick(R.id.requestImage)
public void requestImage(View view) {
Subscription subscription = ServerApi.getBitmap("aaa", "bbb")//
.doOnSubscribe(new Action0() {
@Override
public void call() {
showLoading();
}
})//
.observeOn(AndroidSchedulers.mainThread())//
.subscribe(new Action1<Bitmap>() {
@Override
public void call(Bitmap bitmap) {
dismissLoading(); //请求成功
handleResponse(bitmap, null, null);
imageView.setImageBitmap(bitmap);
System.out.println("---------");
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace(); //请求失败
showToast("请求失败");
dismissLoading();
handleError(null, null);
}
});
addSubscribe(subscription);
}
}
32 changes: 22 additions & 10 deletions app/src/main/java/com/lzy/demo/okrx/RxCommonActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import android.view.View;

import com.lzy.demo.R;
import com.lzy.demo.base.BaseDetailActivity;
import com.lzy.demo.base.BaseRxDetailActivity;
import com.lzy.demo.callback.JsonConvert;
import com.lzy.demo.model.LzyResponse;
import com.lzy.demo.model.ServerModel;
Expand All @@ -20,12 +20,13 @@

import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;

public class RxCommonActivity extends BaseDetailActivity {
public class RxCommonActivity extends BaseRxDetailActivity {

@Override
protected void onActivityCreate(Bundle savedInstanceState) {
Expand All @@ -34,9 +35,16 @@ protected void onActivityCreate(Bundle savedInstanceState) {
setTitle("OkRx基本请求");
}

@Override
protected void onDestroy() {
super.onDestroy();
//Activity销毁时,取消网络请求
unSubscribe();
}

@OnClick(R.id.commonRequest)
public void commonRequest(View view) {
OkGo.post(Urls.URL_METHOD)//
Subscription subscription = OkGo.post(Urls.URL_METHOD)//
.headers("aaa", "111")//
.params("bbb", "222")//
.getCall(StringConvert.create(), RxAdapter.<String>create())//以上为产生请求事件,请求默认发生在IO线程
Expand All @@ -62,11 +70,12 @@ public void call(Throwable throwable) {
handleError(null, null);
}
});
addSubscribe(subscription);
}

@OnClick(R.id.retrofitRequest)
public void retrofitRequest(View view) {
ServerApi.getServerModel("aaa", "bbb")//
Subscription subscription = ServerApi.getServerModel("aaa", "bbb")//
.doOnSubscribe(new Action0() {
@Override
public void call() {
Expand Down Expand Up @@ -95,11 +104,12 @@ public void call(Throwable throwable) {
handleError(null, null);
}
});
addSubscribe(subscription);
}

@OnClick(R.id.jsonRequest)
public void jsonRequest(View view) {
OkGo.post(Urls.URL_JSONOBJECT)//
Subscription subscription = OkGo.post(Urls.URL_JSONOBJECT)//
.headers("aaa", "111")//
.params("bbb", "222")//一定要注意这里的写法,JsonConvert最后的大括号千万不能忘记
.getCall(new JsonConvert<LzyResponse<ServerModel>>() {}, RxAdapter.<LzyResponse<ServerModel>>create())//
Expand Down Expand Up @@ -131,11 +141,12 @@ public void call(Throwable throwable) {
handleError(null, null);
}
});
addSubscribe(subscription);
}

@OnClick(R.id.jsonArrayRequest)
public void jsonArrayRequest(View view) {
ServerApi.getServerListModel("aaa", "bbb")//
Subscription subscription = ServerApi.getServerListModel("aaa", "bbb")//
.doOnSubscribe(new Action0() {
@Override
public void call() {
Expand Down Expand Up @@ -164,12 +175,12 @@ public void call(Throwable throwable) {
handleError(null, null);
}
});
addSubscribe(subscription);
}

@OnClick(R.id.upString)
public void upString(View view) {
OkGo.post(Urls.URL_TEXT_UPLOAD)//
.tag(this)//
Subscription subscription = OkGo.post(Urls.URL_TEXT_UPLOAD)//
.headers("bbb", "222")//
.upString("上传的文本。。。")//
.getCall(StringConvert.create(), RxAdapter.<String>create())//以上为产生请求事件,请求默认发生在IO线程
Expand All @@ -195,6 +206,7 @@ public void call(Throwable throwable) {
handleError(null, null);
}
});
addSubscribe(subscription);
}

@OnClick(R.id.upJson)
Expand All @@ -206,8 +218,7 @@ public void upJson(View view) {
params.put("key4", "其实你怎么高兴怎么写都行");
JSONObject jsonObject = new JSONObject(params);

OkGo.post(Urls.URL_TEXT_UPLOAD)//
.tag(this)//
Subscription subscription = OkGo.post(Urls.URL_TEXT_UPLOAD)//
.headers("bbb", "222")//
.upJson(jsonObject.toString())//
.getCall(StringConvert.create(), RxAdapter.<String>create())//以上为产生请求事件,请求默认发生在IO线程
Expand All @@ -233,5 +244,6 @@ public void call(Throwable throwable) {
handleError(null, null);
}
});
addSubscribe(subscription);
}
}
69 changes: 69 additions & 0 deletions app/src/main/java/com/lzy/demo/okrx/RxFileDownloadActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.lzy.demo.okrx;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lzy.demo.R;
import com.lzy.demo.base.BaseRxDetailActivity;
import com.lzy.demo.ui.NumberProgressBar;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;

public class RxFileDownloadActivity extends BaseRxDetailActivity {

@Bind(R.id.fileDownload) Button btnFileDownload;
@Bind(R.id.downloadSize) TextView tvDownloadSize;
@Bind(R.id.tvProgress) TextView tvProgress;
@Bind(R.id.netSpeed) TextView tvNetSpeed;
@Bind(R.id.pbProgress) NumberProgressBar pbProgress;

@Override
protected void onActivityCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_file_download);
ButterKnife.bind(this);
setTitle("文件下载");
}

@Override
protected void onDestroy() {
super.onDestroy();
//Activity销毁时,取消网络请求
unSubscribe();
}

@OnClick(R.id.fileDownload)
public void fileDownload(View view) {
ServerApi.getFile("aaa", "bbb")//
.doOnSubscribe(new Action0() {
@Override
public void call() {
btnFileDownload.setText("正在下载中...\n使用Rx方式做进度监听稍显麻烦,推荐使用回调方式");
}
})//
.observeOn(AndroidSchedulers.mainThread())//
.subscribe(new Action1<File>() {
@Override
public void call(File file) {
btnFileDownload.setText("下载完成");
handleResponse(file, null, null);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
btnFileDownload.setText("下载出错");
showToast("请求失败");
handleError(null, null);
}
});
}
}
Loading

0 comments on commit ee7081e

Please sign in to comment.