Skip to content

Commit

Permalink
优化Log提示,优化无队列请求方式
Browse files Browse the repository at this point in the history
优化Log提示,优化无队列请求方式
  • Loading branch information
liqi committed Sep 4, 2017
1 parent 4e93d08 commit 50838d7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,27 @@ private int findBestSampleSize(int actualWidth, int actualHeight, int desiredWid
public T parseResponse(Headers responseHeaders, byte[] responseBody) throws Exception {
String response = StringRequest.parseResponseString(responseHeaders, responseBody);
if (null != clazz) {
Logger.e("parseResponse是否执行了>>>>>" + clazz.getName());
Logger.e("NohttpRxUtils服务器数据转换类型:" + clazz.getName());
//不是bitmap和byte[]进入
if (clazz != Bitmap.class && clazz != byte[].class) {
Logger.e("Http服务器响应数据:"+response);

//不是JSONObject和JSONArray类型进入
if (clazz != JSONObject.class && clazz != JSONArray.class) {
if (clazz == String.class) {
return (T) response;
} else
} else {
// 这里如果数据格式错误,或者解析失败,会在失败的回调方法中返回 ParseError 异常。
return JsonUtil.jsonToBean(response, clazz);
}
} else {
if (clazz == JSONArray.class) {
return (T) new JSONArray(response);
} else if (clazz == JSONObject.class) {
return (T) new JSONObject(response);
}
}

} else {
//是bitmap类型就转换bitmap类型
if (clazz == Bitmap.class) {
Expand Down
26 changes: 15 additions & 11 deletions nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@ <T> void request(final IProtocolRequest<T> request, DialogGetListener mDialogGet
public void call(Subscriber<? super Response<T>> subscriberOut) {
// 最关键的就是用NoHttp的同步请求请求到response了,其它的都是rxjava做的,跟nohttp无关的。
Response<T> response = NoHttp.startRequestSync(request);
byte[] oo=new byte[12];
try {
response.request().parseResponse(response.getHeaders(),oo);
} catch (Exception e) {
e.printStackTrace();
}

if (response.isSucceed())
// byte[] oo=new byte[12];
// try {
// response.request().parseResponse(response.getHeaders(),oo);
// } catch (Exception e) {
// e.printStackTrace();
// }

if (response.isSucceed()||response.isFromCache()) {

This comment has been minimized.

Copy link
@lby1992

lby1992 Sep 4, 2017

why do you cache the old responses?

This comment has been minimized.

Copy link
@LiqiNew

LiqiNew Sep 4, 2017

Owner

because of the cache

subscriberOut.onNext(response);
else
}
else {
subscriberOut.onError(response.getException());
}
subscriberOut.onCompleted();
}
}).subscribeOn(Schedulers.io())
Expand Down Expand Up @@ -118,14 +120,16 @@ public void onError(Throwable e) {
}
dialog = null;
}
if (null != responseInterfa)
if (null != responseInterfa) {
responseInterfa.onError(e);
}
}

@Override
public void onNext(Response<T> tResponse) {
if (null != responseInterfa)
if (null != responseInterfa) {
responseInterfa.onNext(tResponse.get());
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.liqi.nohttputils.nohttp.rx_threadpool.RxMessageSource;
import com.liqi.nohttputils.nohttp.rx_threadpool.model.RxRequestModel;
import com.yanzhenjie.nohttp.Binary;
import com.yanzhenjie.nohttp.Headers;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.rest.RestRequest;

Expand Down Expand Up @@ -83,7 +84,7 @@ private RestRequest<T> addParameter(RestRequest<T> entityRequest) {
Map<String, Object> parameterMap = mRxRequestConfig.getParameterMap();
//参数设置
if (null != parameterMap && !parameterMap.isEmpty()) {
mapP:

for (Map.Entry<String, Object> entry : parameterMap.entrySet()) {
String keyParameter = entry.getKey();
Object valueParameter = entry.getValue();
Expand All @@ -98,53 +99,46 @@ private RestRequest<T> addParameter(RestRequest<T> entityRequest) {
}
} else {
if (null != valueParameter) {
if (valueParameter instanceof String) {
entityRequest.add(keyParameter, valueParameter.toString());
continue mapP;
}
if (valueParameter instanceof Integer) {
entityRequest.add(keyParameter, Integer.parseInt(valueParameter.toString()));
continue mapP;
}
if (valueParameter instanceof Boolean) {
}else if (valueParameter instanceof Boolean) {
entityRequest.add(keyParameter, Boolean.parseBoolean(valueParameter.toString()));
continue mapP;
}
if (valueParameter instanceof Byte) {
}else if (valueParameter instanceof Byte) {
entityRequest.add(keyParameter, Byte.parseByte(valueParameter.toString()));
continue mapP;
}
if (valueParameter instanceof Double) {
}else if (valueParameter instanceof Double) {
entityRequest.add(keyParameter, Double.valueOf(valueParameter.toString()));
continue mapP;
}
if (valueParameter instanceof File) {
}else if (valueParameter instanceof File) {
entityRequest.add(keyParameter, (File) valueParameter);
continue mapP;
}
if (valueParameter instanceof Float) {
}else if (valueParameter instanceof Float) {
entityRequest.add(keyParameter, Float.parseFloat(valueParameter.toString()));
continue mapP;
}
if (valueParameter instanceof Binary) {
}else if (valueParameter instanceof Binary) {
entityRequest.add(keyParameter, (Binary) valueParameter);
continue mapP;
}
if (valueParameter instanceof Long) {
}else if (valueParameter instanceof Long) {
entityRequest.add(keyParameter, Long.parseLong(valueParameter.toString()));
continue mapP;
}
if (valueParameter instanceof List) {
entityRequest.add(keyParameter, (List<Binary>) valueParameter);
continue mapP;
}
if (valueParameter instanceof Short) {
}else if (valueParameter instanceof List) {
//确保传入的文件List集合中是指定类型
List list= (List) valueParameter;
if (!list.isEmpty()) {
Object object = list.get(0);
if (object instanceof Binary) {
entityRequest.add(keyParameter, (List<Binary>) valueParameter);
}else{
Logger.e("文件上传list参数值类型不符合.需要传入的类型:Binary类型");
}
}else{
Logger.e("文件上传list参数值为空");
}
}else if (valueParameter instanceof Short) {
entityRequest.add(keyParameter, Short.parseShort(valueParameter.toString()));
continue mapP;
}
if (valueParameter instanceof Map) {
entityRequest.add((Map<String, String>) valueParameter);
continue mapP;
}else if (valueParameter instanceof Map) {
try {
Map<String, String> mapString= (Map<String, String>) valueParameter;
entityRequest.add(mapString);
} catch (Exception e) {
Logger.e("参数Map传入值类型错误.Map键值类型:key=String类型,value=String类型");
}
}else{
entityRequest.add(keyParameter, valueParameter.toString());
}
}
}
Expand All @@ -168,15 +162,32 @@ private RestRequest<T> addParameter(RestRequest<T> entityRequest) {
RxRequestEntityBase rxRequestEntityBase = mRxRequestConfig.getRxRequestEntityBase();
if (null != rxRequestEntityBase) {
if (rxRequestEntityBase instanceof RxRequestJsonObjectEntity) {
entityRequest.setDefineRequestBodyForJson(JsonUtil.objectToJson(rxRequestEntityBase.getStringJsonMap()));
} else if (rxRequestEntityBase instanceof RxRequestStringEntity) {
entityRequest.setDefineRequestBody(rxRequestEntityBase.getStringEntity(), rxRequestEntityBase.getContentType());
} else if (rxRequestEntityBase instanceof RxRequestInputStreamEntity) {
entityRequest.setDefineRequestBody(rxRequestEntityBase.getInputStream(), rxRequestEntityBase.getContentType());
} else if (rxRequestEntityBase instanceof RxRequestJsonListEntity) {
entityRequest.setDefineRequestBodyForJson(JsonUtil.objectToJson(rxRequestEntityBase.getJsonMapList()));
String objectToJson = JsonUtil.objectToJson(rxRequestEntityBase.getStringJsonMap());
entityRequest.setDefineRequestBodyForJson(objectToJson);
Logger.e("JsonObject类型-Body值:"+objectToJson+"\nBody-ContentType类型:"+ Headers.HEAD_VALUE_ACCEPT_APPLICATION_JSON);
}

} else {
else if (rxRequestEntityBase instanceof RxRequestStringEntity) {
String stringEntity = rxRequestEntityBase.getStringEntity();
String contentType = rxRequestEntityBase.getContentType();
entityRequest.setDefineRequestBody(stringEntity, contentType);
Logger.e("字符串类型-Body值:"+stringEntity+"\nBody-ContentType类型:"+contentType);
}

else if (rxRequestEntityBase instanceof RxRequestInputStreamEntity) {
InputStream inputStream = rxRequestEntityBase.getInputStream();
String contentType = rxRequestEntityBase.getContentType();
entityRequest.setDefineRequestBody(inputStream, contentType);
Logger.e("字节流类型-Body值:(字节流"+")\nBody-ContentType类型:"+contentType);
}

else if (rxRequestEntityBase instanceof RxRequestJsonListEntity) {
String objectToJson = JsonUtil.objectToJson(rxRequestEntityBase.getJsonMapList());
entityRequest.setDefineRequestBodyForJson(objectToJson);
Logger.e("JsonArray类型-Body值:"+objectToJson+"\nBody-ContentType类型:"+Headers.HEAD_VALUE_ACCEPT_APPLICATION_JSON);
}

else {
Logger.e("RxRequestEntityBase类型未知");
}
}
Expand Down

0 comments on commit 50838d7

Please sign in to comment.