forked from jeasonlzy/okhttp-OkGo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
348 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
app/src/main/java/com/lzy/demo/base/BaseRxDetailActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
app/src/main/java/com/lzy/demo/okrx/RxFileDownloadActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.