Skip to content

Commit

Permalink
修复errorBody中为空时候异常信息无法获取的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyao committed Aug 17, 2018
1 parent eb5137b commit ec2f5a2
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 53 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/allen/rxhttputils/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ protected void onSuccess(ResponseBody responseBody) {
*/
private void uploadImgs(List<String> uploadPaths) {

RxHttpUtils.uploadImgs("http://t.xinhuo.com/index.php/Api/Pic/uploadPic", uploadPaths)
RxHttpUtils.uploadImages("http://t.xinhuo.com/index.php/Api/Pic/uploadPic", uploadPaths)
.compose(Transformer.<ResponseBody>switchSchedulers(loading_dialog))
.subscribe(new CommonObserver<ResponseBody>() {

Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 24
versionCode 214
versionName "2.1.4"
versionCode 215
versionName "2.1.5"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
40 changes: 28 additions & 12 deletions library/src/main/java/com/allen/library/RxHttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
import com.allen.library.manage.RxHttpManager;
import com.allen.library.upload.UploadRetrofit;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import okhttp3.Cookie;
import okhttp3.HttpUrl;
import okhttp3.ResponseBody;
Expand All @@ -37,15 +35,11 @@ public class RxHttpUtils {
@SuppressLint("StaticFieldLeak")
private static Application context;

private static List<Disposable> disposables;
private static CompositeDisposable mCompositeDisposable;

public static RxHttpUtils getInstance() {
if (instance == null) {
synchronized (RxHttpUtils.class) {
if (instance == null) {
instance = new RxHttpUtils();
disposables = new ArrayList<>();
}
}

Expand Down Expand Up @@ -101,9 +95,11 @@ public static <K> K createApi(Class<K> cls) {

/**
* 获取单个请求配置实例
* 后续版本即将移除---推荐使用全局配置的请求
*
* @return SingleRxHttp
*/
@Deprecated
public static SingleRxHttp getSInstance() {

return SingleRxHttp.getInstance();
Expand All @@ -113,8 +109,8 @@ public static SingleRxHttp getSInstance() {
/**
* 下载文件
*
* @param fileUrl
* @return
* @param fileUrl 地址
* @return ResponseBody
*/
public static Observable<ResponseBody> downloadFile(String fileUrl) {
return DownloadRetrofit.downloadFile(fileUrl);
Expand All @@ -128,7 +124,18 @@ public static Observable<ResponseBody> downloadFile(String fileUrl) {
* @return ResponseBody
*/
public static Observable<ResponseBody> uploadImg(String uploadUrl, String filePath) {
return UploadRetrofit.uploadImg(uploadUrl, filePath);
return UploadRetrofit.uploadImage(uploadUrl, filePath);
}

/**
* 上传多张图片
*
* @param uploadUrl 地址
* @param filePaths 文件路径
* @return ResponseBody
*/
public static Observable<ResponseBody> uploadImages(String uploadUrl, List<String> filePaths) {
return UploadRetrofit.uploadImages(uploadUrl, filePaths);
}

/**
Expand All @@ -138,8 +145,17 @@ public static Observable<ResponseBody> uploadImg(String uploadUrl, String filePa
* @param filePaths 文件路径
* @return ResponseBody
*/
public static Observable<ResponseBody> uploadImgs(String uploadUrl, List<String> filePaths) {
return UploadRetrofit.uploadImgs(uploadUrl, filePaths);
/**
* 上传多张图片
*
* @param uploadUrl 地址
* @param fileName 后台接收文件流的参数名
* @param paramsMap 参数
* @param filePaths 文件路径
* @return ResponseBody
*/
public static Observable<ResponseBody> uploadImagesWithParams(String uploadUrl, String fileName, Map<String, Object> paramsMap, List<String> filePaths) {
return UploadRetrofit.uploadFilesWithParams(uploadUrl, fileName, paramsMap, filePaths);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,11 @@
public abstract class DownloadObserver extends BaseDownloadObserver {

private String fileName;
private Dialog mProgressDialog;

public DownloadObserver(String fileName) {
this.fileName = fileName;
}

public DownloadObserver(String fileName, Dialog mProgressDialog) {
this.fileName = fileName;
this.mProgressDialog = mProgressDialog;
}

/**
* 失败回调
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;

import java.io.IOException;
import java.io.NotSerializableException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
Expand Down Expand Up @@ -46,12 +45,7 @@ public static ApiException handleException(Throwable e) {
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
ex = new ApiException(httpException, httpException.code());
try {
ex.message = httpException.response().errorBody().string();
} catch (IOException e1) {
e1.printStackTrace();
ex.message = e1.getMessage();
}
ex.message = httpException.getMessage();
} else if (e instanceof SocketTimeoutException) {
ex = new ApiException(e, ERROR.TIMEOUT_ERROR);
ex.message = "网络连接超时,请检查您的网络状态,稍后重试!";
Expand Down
16 changes: 2 additions & 14 deletions library/src/main/java/com/allen/library/upload/UploadFileApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@

public interface UploadFileApi {

/**
* 上传
*
* @param uploadUrl 地址
* @param file 文件
* @return ResponseBody
*/
@Multipart
@POST
Observable<ResponseBody> uploadImg(@Url String uploadUrl,
@Part MultipartBody.Part file);


/**
* 上传多个文件
Expand All @@ -42,6 +30,6 @@ Observable<ResponseBody> uploadImg(@Url String uploadUrl,
*/
@Multipart
@POST
Observable<ResponseBody> uploadImgs(@Url String uploadUrl,
@Part List<MultipartBody.Part> files);
Observable<ResponseBody> uploadFiles(@Url String uploadUrl,
@Part List<MultipartBody.Part> files);
}
23 changes: 11 additions & 12 deletions library/src/main/java/com/allen/library/upload/UploadRetrofit.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,10 @@ public Retrofit getRetrofit() {
* @param filePath 图片路径
* @return Observable
*/
public static Observable<ResponseBody> uploadImg(String uploadUrl, String filePath) {
public static Observable<ResponseBody> uploadImage(String uploadUrl, String filePath) {
List<String> filePaths = new ArrayList<>();
filePaths.add(filePath);
return uploadImgsWithParams(uploadUrl, "uploaded_file", null, filePaths);

return uploadFilesWithParams(uploadUrl, "uploaded_file", null, filePaths);
}

/**
Expand All @@ -80,34 +79,34 @@ public static Observable<ResponseBody> uploadImg(String uploadUrl, String filePa
* @param filePaths 图片路径
* @return Observable
*/
public static Observable<ResponseBody> uploadImgs(String uploadUrl, List<String> filePaths) {
return uploadImgsWithParams(uploadUrl, "uploaded_file", null, filePaths);
public static Observable<ResponseBody> uploadImages(String uploadUrl, List<String> filePaths) {
return uploadFilesWithParams(uploadUrl, "uploaded_file", null, filePaths);
}

/**
* 图片和参数同时上传的请求
*
* @param uploadUrl 上传图片的服务器url
* @param fileName 后台协定的接受图片的name(没特殊要求就可以随便写)
* @param map 普通参数
* @param paramsMap 普通参数
* @param filePaths 图片路径
* @return Observable
*/
public static Observable<ResponseBody> uploadImgsWithParams(String uploadUrl, String fileName, Map<String, Object> map, List<String> filePaths) {
public static Observable<ResponseBody> uploadFilesWithParams(String uploadUrl, String fileName, Map<String, Object> paramsMap, List<String> filePaths) {

MultipartBody.Builder builder = new MultipartBody.Builder()
.setType(MultipartBody.FORM);

if (null != map) {
for (String key : map.keySet()) {
builder.addFormDataPart(key, (String) map.get(key));
if (null != paramsMap) {
for (String key : paramsMap.keySet()) {
builder.addFormDataPart(key, (String) paramsMap.get(key));
}
}

for (int i = 0; i < filePaths.size(); i++) {
File file = new File(filePaths.get(i));
RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
//"medias"+i 后台接收图片流的参数名
//"fileName"+i 后台接收图片流的参数名
builder.addFormDataPart(fileName, file.getName(), imageBody);
}

Expand All @@ -117,6 +116,6 @@ public static Observable<ResponseBody> uploadImgsWithParams(String uploadUrl, St
.getInstance()
.getRetrofit()
.create(UploadFileApi.class)
.uploadImgs(uploadUrl, parts);
.uploadFiles(uploadUrl, parts);
}
}

0 comments on commit ec2f5a2

Please sign in to comment.