Skip to content

Commit

Permalink
* 增加自动解析功能 不需要type 通过反射获取httpobserver的泛型 类型
Browse files Browse the repository at this point in the history
  • Loading branch information
eajon authored and wengyijiong committed Jan 25, 2019
1 parent 6210404 commit 208f18b
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 85 deletions.
2 changes: 1 addition & 1 deletion RxHttp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ dependencies {


// rxbus
api 'com.github.eajon:rxbus2:1.3.5'
api 'com.github.eajon:rxbus2:1.3.6'
api 'io.reactivex.rxjava2:rxandroid:2.1.0'


Expand Down
14 changes: 10 additions & 4 deletions RxHttp/src/main/java/com/github/eajon/RxHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.github.eajon.upload.UploadRequestBody;
import com.github.eajon.util.GsonUtils;
import com.github.eajon.util.NetUtils;
import com.github.eajon.util.ReflectUtils;
import com.github.eajon.util.RetrofitUtils;
import com.github.eajon.util.RxBusUtils;
import com.github.eajon.util.RxUtils;
Expand Down Expand Up @@ -230,15 +231,15 @@ private Disposable doUpload() {
UploadTask uploadTask = ( UploadTask ) task;
file = uploadTask.getFile();
requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part part = MultipartBody.Part.createFormData(uploadTask.getName(), uploadTask.getFileName(), new UploadRequestBody(requestBody, tag, isStick, uploadTask));
MultipartBody.Part part = MultipartBody.Part.createFormData(uploadTask.getName(), NetUtils.getHeaderValueEncoded(uploadTask.getFileName()).toString(), new UploadRequestBody(requestBody, tag, isStick, uploadTask));
observable = RetrofitUtils.get().getRetrofit(getBaseUrl()).upload(disposeApiUrl(), convertParameter(), header, part);
} else {
MultiUploadTask multiUploadTask = ( MultiUploadTask ) task;
for (int i = 0; i < multiUploadTask.getUploadTasks().size(); i++) {
UploadTask task = multiUploadTask.getUploadTasks().get(i);
file = task.getFile();
requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part part = MultipartBody.Part.createFormData(task.getName(), task.getFileName(), new UploadRequestBody(requestBody, tag, isStick, task, multiUploadTask));
MultipartBody.Part part = MultipartBody.Part.createFormData(task.getName(), NetUtils.getHeaderValueEncoded(task.getFileName()).toString(), new UploadRequestBody(requestBody, tag, isStick, task, multiUploadTask));
partList.add(part);
}
observable = RetrofitUtils.get().getRetrofit(getBaseUrl()).upload(disposeApiUrl(), convertParameter(), header, partList);
Expand Down Expand Up @@ -270,15 +271,20 @@ private Disposable subscribe(Observable observable) {
httpObserver = new HttpObserver() {
@Override
public void onSuccess(Object o) {
RxBusUtils.sendBus(tag, isStick, o);
RxBusUtils.sendBus(tag, o, isStick);
}

@Override
public void onError(ApiException t) {
RxBusUtils.sendBus(tag, isStick, t);
RxBusUtils.sendBus(tag, t, isStick);
}
};
}

//如果没有设置type获取泛型type
if (type == null) {
type = ReflectUtils.getParameterizedType(httpObserver);
}
if (dialog != null || dialogContext != null) {
dialogObserver(observable).subscribe(httpObserver);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.text.TextUtils;

import com.github.eajon.task.DownloadTask;
import com.github.eajon.util.RxBusUtils;

import java.io.IOException;

Expand Down Expand Up @@ -110,7 +111,7 @@ public long read(Buffer sink, final long byteCount) throws IOException {
}
downloadTask.setCurrentSize(readBytesCount);
downloadTask.setTotalSize(totalBytesCount);
downloadTask.sendBus(tag, isStick);
RxBusUtils.sendBus(tag, downloadTask, isStick);
return bytesRead;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public HttpResponseFunction(Type type) {
}

@Override
public Object apply(@NonNull JsonElement response) throws Exception {
public Object apply(@NonNull JsonElement response) {
//打印服务器回传结果
LoggerUtils.json(response.toString());
/*此处不再处理业务相关逻辑交由开发者重写httpCallback*/
if (type == null) {
if (response.isJsonPrimitive()) {
return response.getAsString();
} else if (response.isJsonNull()) {
return JsonNull.INSTANCE;
return JsonNull.INSTANCE.toString();
} else {
return response.toString();
}
Expand Down
56 changes: 37 additions & 19 deletions RxHttp/src/main/java/com/github/eajon/task/BaseTask.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.github.eajon.task;

import android.text.TextUtils;

import com.github.eajon.util.SpeedUtils;
import com.threshold.rxbus2.RxBus;

import java.util.concurrent.TimeUnit;

Expand All @@ -22,6 +19,10 @@ public enum State {
CANCEL, //取消
}

private long startTime;
private long finishTime;


private State state = State.NONE;//下载状态

private long speed;
Expand All @@ -34,9 +35,21 @@ public void setState(State state) {
this.state = state;
if (state != State.LOADING) {
this.speed = 0;
} else {
startTime = System.currentTimeMillis();
}
if (state == State.FINISH) {
finishTime = System.currentTimeMillis();
}
}

public long getDuration() {
if (finishTime == 0L) {
return System.currentTimeMillis() - startTime;
}
return finishTime - startTime;
}

public long getSpeed() {
return speed;
}
Expand All @@ -50,10 +63,30 @@ public String getSpeedFormat() {

}


public String getSpeedFormat(TimeUnit timeUnit) {
return SpeedUtils.formatSpeed(speed, timeUnit);
}

public float getAverageSpeed(long currentSize) {
long dur = getDuration();
if (currentSize != 0 && dur != 0) {
float speed = 1000F * currentSize / dur;
return speed;
} else {
return 0F;
}

}

public String getAverageSpeedFormat(long currentSize) {
return SpeedUtils.formatSpeedPerSecond(getAverageSpeed(currentSize));
}

public String getAverageSpeedFormat(long currentSize, TimeUnit timeUnit) {
return SpeedUtils.formatSpeed(getAverageSpeed(currentSize), timeUnit);
}

public boolean isFinish() {
return state == State.FINISH;
}
Expand All @@ -62,20 +95,5 @@ public boolean isError() {
return state == State.ERROR;
}

public void sendBus(String tag, boolean isStick) {
if (isStick) {
RxBus.getDefault().removeStickyEventType(this.getClass());
if (TextUtils.isEmpty(tag)) {
RxBus.getDefault().postSticky(this);
} else {
RxBus.getDefault().postSticky(tag, this);
}
} else {
if (TextUtils.isEmpty(tag)) {
RxBus.getDefault().post(this);
} else {
RxBus.getDefault().post(tag, this);
}
}
}

}
30 changes: 28 additions & 2 deletions RxHttp/src/main/java/com/github/eajon/task/DownloadTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.os.Environment;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;


public class DownloadTask extends BaseTask implements Serializable {
Expand All @@ -13,10 +14,12 @@ public class DownloadTask extends BaseTask implements Serializable {
private String localDir;//本地存储目录
private long totalSize;//文件大小
private long currentSize;//当前大小
private long rangeSize;//纪录上次下载位置

public DownloadTask() {
this.localDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
}

public DownloadTask(String name) {
this.name = name;
this.localDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
Expand All @@ -27,6 +30,14 @@ public DownloadTask(String name, String localDir) {
this.localDir = localDir;
}

@Override
public void setState(State state) {
super.setState(state);
if (state == State.PAUSE) {
rangeSize = currentSize;
}

}

public String getName() {
return name;
Expand Down Expand Up @@ -71,21 +82,36 @@ public void setCurrentSize(long currentSize) {

public int getProgress() {
if (totalSize != 0) {
float progress = (float) currentSize / (float) totalSize;
return (int) (progress * 100);
float progress = ( float ) currentSize / ( float ) totalSize;
return ( int ) (progress * 100);
} else {
return 0;
}
}

public float getAverageSpeed() {
return getAverageSpeed(currentSize - rangeSize);

}

public String getAverageSpeedFormat() {
return getAverageSpeedFormat(currentSize - rangeSize);
}

public String getAverageSpeedFormat(TimeUnit timeUnit) {
return getAverageSpeedFormat(currentSize - rangeSize, timeUnit);
}


@Override
public String toString() {
return "DownloadTask{" +
"name='" + name + '\'' +
", originalName='" + originalName + '\'' +
", localDir='" + localDir + '\'' +
", totalSize=" + totalSize +
", currentSize=" + currentSize +
", rangeSize=" + rangeSize +
'}';
}
}
38 changes: 32 additions & 6 deletions RxHttp/src/main/java/com/github/eajon/task/MultiUploadTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import java.io.Serializable;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class MultiUploadTask extends BaseTask implements Serializable {

private ArrayList <UploadTask> uploadTasks;
private ArrayList<UploadTask> uploadTasks;

public State getState(int position) {
if (uploadTasks.size() > position) {
Expand All @@ -15,12 +16,12 @@ public State getState(int position) {
}
}

public MultiUploadTask(ArrayList <UploadTask> uploadTasks) {
public MultiUploadTask(ArrayList<UploadTask> uploadTasks) {

this.uploadTasks = uploadTasks;
}

public ArrayList <UploadTask> getUploadTasks() {
public ArrayList<UploadTask> getUploadTasks() {
return uploadTasks;
}

Expand All @@ -34,15 +35,14 @@ public int getProgress() {
currentSize += uploadTasks.get(i).getCurrentSize();
}
if (totalSize != 0L) {
float progress = (float) currentSize / (float) totalSize;
return (int) (progress * 100);
float progress = ( float ) currentSize / ( float ) totalSize;
return ( int ) (progress * 100);
} else {
return 0;
}
}



public int getProgress(int position) {
if (uploadTasks.size() > position) {
return this.getUploadTasks().get(position).getProgress();
Expand All @@ -51,6 +51,32 @@ public int getProgress(int position) {
}
}

public float getAverageSpeed() {
long currentSize = 0L;
for (int i = 0; i < uploadTasks.size(); i++) {
currentSize += uploadTasks.get(i).getCurrentSize();
}

return getAverageSpeed(currentSize);

}

public String getAverageSpeedFormat() {
long currentSize = 0L;
for (int i = 0; i < uploadTasks.size(); i++) {
currentSize += uploadTasks.get(i).getCurrentSize();
}
return getAverageSpeedFormat(currentSize);
}

public String getAverageSpeedFormat(TimeUnit timeUnit) {
long currentSize = 0L;
for (int i = 0; i < uploadTasks.size(); i++) {
currentSize += uploadTasks.get(i).getCurrentSize();
}
return getAverageSpeedFormat(currentSize, timeUnit);
}


@Override
public String toString() {
Expand Down
13 changes: 13 additions & 0 deletions RxHttp/src/main/java/com/github/eajon/task/UploadTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;

public class UploadTask extends BaseTask implements Serializable {

Expand Down Expand Up @@ -72,6 +73,18 @@ public int getProgress() {
}
}

public float getAverageSpeed() {
return getAverageSpeed(currentSize);
}

public String getAverageSpeedFormat() {
return getAverageSpeedFormat(currentSize);
}

public String getAverageSpeedFormat(TimeUnit timeUnit) {
return getAverageSpeedFormat(currentSize, timeUnit);
}


@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.github.eajon.task.MultiUploadTask;
import com.github.eajon.task.UploadTask;
import com.github.eajon.util.RxBusUtils;

import java.io.IOException;

Expand Down Expand Up @@ -132,9 +133,9 @@ public void write(Buffer source, long byteCount) throws IOException {
uploadTask.setState(UploadTask.State.LOADING);
}
if (multiUploadTask != null) {
multiUploadTask.sendBus(tag, isStick);
RxBusUtils.sendBus(tag, multiUploadTask, isStick);
} else {
uploadTask.sendBus(tag, isStick);
RxBusUtils.sendBus(tag, uploadTask, isStick);
}
}
};
Expand Down
Loading

0 comments on commit 208f18b

Please sign in to comment.