https://github.com/Tamicer/Novate
repositories {
maven { url "https://jitpack.io" }
jcenter()
}
dependencies {
compile 'com.tamic.novate:novate:1.5.2.3'
}
1,在oncreate中初始化进度条 ,这样在上传或者下载数据的时候,会显示进度
/**
* 初始化进度条
*/
private void initProgress(Context aContext) {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(aContext);
}
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setTitle("温馨提示");
mProgressDialog.setMax(100);
mProgressDialog.setMessage("正在上传中...");
mProgressDialog.setCancelable(true);
}
private void showPressDialog() {
if (mProgressDialog == null || this.isFinishing()) {
return;
}
mProgressDialog.show();
}
private void dismissProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
private void updateProgressDialog(int progress) {
if (mProgressDialog != null) {
if (!mProgressDialog.isShowing()) {
showPressDialog();
}
mProgressDialog.setProgress(progress);
}
}
2,get和post请求
private void performPost() {
Map<String, Object> parameters = new HashMap<>();
parameters.put("ip", "21.22.11.33");
novate = new Novate.Builder(this)
.connectTimeout(8)
.baseUrl(baseUrl)
//.addApiManager(ApiManager.class)
.addLog(true)
.build();
/**
*
*
* 调用post需要你自己解析数据
*
* 如果需要解析后返回 则调用novate.executeGet()
* 参考 performGet()中的方式
*/
novate.post("service/getIpInfo.php", parameters, new MyBaseSubscriber<ResponseBody>(ExempleActivity.this) {
@Override
public void onError(Throwable e) {
if (!TextUtils.isEmpty(e.getMessage())) {
Log.e("OkHttp", e.getMessage());
Toast.makeText(ExempleActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNext(ResponseBody responseBody) {
try {
String json = new String(responseBody.bytes());
System.out.println("------------jstr"+json);
if (json.trim().isEmpty()) {
return;
}
haha haha = JSON.parseObject(json, haha.class);
System.out.println("----------haha"+haha.getRows());
} catch (IOException e) { e.printStackTrace(); } } }); } 3,自定义的MyBaseSubscriber 里面,定义了一个进度条,这样在请求数据的时候,会显示正在加载中
public abstract class MyBaseSubscriber<T> extends BaseSubscriber<T> {
private ProgressDialog progress;
private Context context;
public MyBaseSubscriber(Context context) {
super(context);
this.context = context;
progress = new ProgressDialog(context);
progress.setMessage("novate拼命加载中....");
}
@Override
public void onStart() {
super.onStart();
if (!NetworkUtil.isNetworkAvailable(context)) {
Toast.makeText( context, "似乎没网O",Toast.LENGTH_SHORT).show();
onCompleted();
return;
}
if (progress != null){
if (progress.isShowing()) {
progress.dismiss();
}
progress.show();
}
}
@Override
public void onCompleted() {
super.onCompleted();
if (progress != null && progress.isShowing()) {
progress.dismiss();
}
}
}
4,如果要想将对象实例化 ,需要创建API接口
public interface MyAPI {
@POST("user/register")
Observable<SouguBean> getSougu(@QueryMap Map<String, Object> maps);
@GET("{url}")
Observable<ResponseBody>getWeatherStr(@Path("url") String url,
@QueryMap Map<String, String> maps);
}
public class SouguBean {
private String code;
private Data data;
private String message;
private boolean success;}
private void netPost() {
Map<String, Object> parameters = new HashMap<>();
parameters.put("username", "zhansa");
parameters.put("password", "12313");
novate = new Novate.Builder(this)
.connectTimeout(8)
.baseUrl(baseUrl)
//.addApiManager(ApiManager.class)
.addLog(true)
.build();
MyAPI myAPI = novate.create(MyAPI.class);
novate.call(myAPI.getSougu(parameters), new MyBaseSubscriber<SouguBean>(MainActivity.this) {
@Override
public void onNext(SouguBean souguBean) {
Toast.makeText(MainActivity.this, souguBean.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Throwable e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
5,上传照片,并显示进度
/**
* upload
*/
private void performUpLoadImage() {
String mPath = uploadPath; //"you File path ";
String url = "";
RequestBody requestFile =
RequestBody.create(MediaType.parse("image/jpg"), new File(mPath));
final NovateRequestBody requestBody = Utils.createNovateRequestBody(requestFile, new UpLoadCallback() {
@Override
public void onProgress(Object tag, int progress, long speed, boolean done) {
LogWraper.d("uplaod", "tag:" + tag.toString() + "progress:"+ progress);
updateProgressDialog(progress);
}
});
novate.rxUploadWithBody(url, requestBody, new RxStringCallback() {
@Override
public void onStart(Object tag) {
super.onStart(tag);
showPressDialog();
}
@Override
public void onNext(Object tag, String response) {
LogWraper.d("novate", response);
Toast.makeText(ExempleActivity.this, "成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Object tag, Throwable e) {
Toast.makeText(ExempleActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
dismissProgressDialog();
}
@Override
public void onCancel(Object tag, Throwable e) {
}
@Override
public void onCompleted(Object tag) {
super.onCompleted(tag);
dismissProgressDialog();
}
});
}
6,上传file 并显示进度
/**
* upload
*/
private void performUpLoadFlie() {
String mPath = uploadPath; //"you File path ";
String url = "";
File file = new File(mPath);
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data; charset=utf-8"), file);
final NovateRequestBody requestBody = Utils.createNovateRequestBody(requestFile, new UpLoadCallback() {
@Override
public void onProgress(Object tag, int progress, long speed, boolean done) {
LogWraper.d("uplaod", "tag:" + tag.toString() + "progress:"+ progress);
updateProgressDialog(progress);
}
});
MultipartBody.Part body2 =
MultipartBody.Part.createFormData("image", file.getName(), requestBody);
novate.rxUploadWithPart(url, body2, new RxStringCallback() {
@Override
public void onStart(Object tag) {
super.onStart(tag);
showPressDialog();
}
@Override
public void onError(Object tag, Throwable e) {
Toast.makeText(ExempleActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
dismissProgressDialog();
}
@Override
public void onCancel(Object tag, Throwable e) {
}
@Override
public void onNext(Object tag, String response) {
LogWraper.d(response);
Toast.makeText(ExempleActivity.this, "成功", Toast.LENGTH_SHORT).show();
dismissProgressDialog();
}
});
}
7,下载图片或者text
显示进度条
private Map<String, Object> parameters = new HashMap<String, Object>();
private Map<String, String> headers = new HashMap<>();
private void performDownMin() {
parameters.put("ip", "21.22.11.33");
headers.put("Accept", "application/json");
novate = new Novate.Builder(this)
//.addParameters(parameters)
.connectTimeout(20)
.writeTimeout(15)
.baseUrl(baseUrl)
.addHeader(headers)
.addCache(true)
.addLog(true)
.build();
String downUrl = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png";
novate.downloadMin("tag", downUrl, "my.jpg",new DownLoadCallBack() {
@Override
public void onStart(String s) {
super.onStart(s);
// mb.setText("DownLoadMin cancel");
showPressDialog();
}
@Override
public void onError(Throwable e) {
Toast.makeText(MainActivity.this, "onError:" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void onSucess(String key, String path, String name, long fileSize) {
// btn_download.setText("DownLoad start");
uploadPath = path + name;
dismissProgressDialog();
}
@Override
public void onProgress(String key, int progress, long fileSizeDownloaded, long totalSize) {
super.onProgress(key, progress, fileSizeDownloaded, totalSize);
updateProgressDialog(progress);
Toast.makeText(MainActivity.this, "progress: " + progress + " download: " + fileSizeDownloaded, Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
super.onCancel();
// btn_download.setText("DownLoadMin start");
}
});
}
8,下载file或者视频 显示进度
private Map<String, Object> parameters = new HashMap<String, Object>();
private Map<String, String> headers = new HashMap<>();
/**
* performDown file
* ex: apk , video...
*/
private void performDown() {
parameters.put("ip", "21.22.11.33");
headers.put("Accept", "application/json");
novate = new Novate.Builder(this)
//.addParameters(parameters)
.connectTimeout(20)
.writeTimeout(15)
.baseUrl(baseUrl)
.addHeader(headers)
.addCache(true)
.addLog(true)
.build();
String downUrl = "http://wifiapi02.51y5.net/wifiapi/rd.do?f=wk00003&b=gwanz02&rurl=http%3A%2F%2Fdl.lianwifi.com%2Fdownload%2Fandroid%2FWifiKey-3091-guanwang.apk";
novate.download(downUrl, "test.mei", new DownLoadCallBack() {
@Override
public void onStart(String s) {
super.onStart(s);
btn_download.setText("DownLoad cancel");
showPressDialog();
}
@Override
public void onError(Throwable e) {
Toast.makeText(ExempleActivity.this, "onError:" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void onProgress(String key, int progress, long fileSizeDownloaded, long totalSize) {
super.onProgress(key, progress, fileSizeDownloaded, totalSize);
Log.v("test", fileSizeDownloaded+"");
//Toast.makeText(ExempleActivity.this, "progress: " + progress + " download: " + fileSizeDownloaded, Toast.LENGTH_SHORT).show();
updateProgressDialog(progress);
}
@Override
public void onSucess(String key, String path, String name, long fileSize) {
Toast.makeText(ExempleActivity.this, "download onSucess", Toast.LENGTH_SHORT).show();
dismissProgressDialog();
}
@Override
public void onCancel() {
super.onCancel();
dismissProgressDialog();
}
});
}