diff --git a/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RequestBeanObj.java b/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RequestBeanObj.java index 48af175..b1395b3 100644 --- a/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RequestBeanObj.java +++ b/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RequestBeanObj.java @@ -161,16 +161,19 @@ 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); @@ -178,6 +181,7 @@ public T parseResponse(Headers responseHeaders, byte[] responseBody) throws Exce return (T) new JSONObject(response); } } + } else { //是bitmap类型就转换bitmap类型 if (clazz == Bitmap.class) { diff --git a/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java b/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java index 1953ccb..1b91baf 100644 --- a/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java +++ b/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java @@ -59,17 +59,19 @@ void request(final IProtocolRequest request, DialogGetListener mDialogGet public void call(Subscriber> subscriberOut) { // 最关键的就是用NoHttp的同步请求请求到response了,其它的都是rxjava做的,跟nohttp无关的。 Response 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()) { subscriberOut.onNext(response); - else + } + else { subscriberOut.onError(response.getException()); + } subscriberOut.onCompleted(); } }).subscribeOn(Schedulers.io()) @@ -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 tResponse) { - if (null != responseInterfa) + if (null != responseInterfa) { responseInterfa.onNext(tResponse.get()); + } } }); } diff --git a/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RxRequestOperate.java b/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RxRequestOperate.java index 371f984..90dcb02 100644 --- a/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RxRequestOperate.java +++ b/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RxRequestOperate.java @@ -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; @@ -83,7 +84,7 @@ private RestRequest addParameter(RestRequest entityRequest) { Map parameterMap = mRxRequestConfig.getParameterMap(); //参数设置 if (null != parameterMap && !parameterMap.isEmpty()) { - mapP: + for (Map.Entry entry : parameterMap.entrySet()) { String keyParameter = entry.getKey(); Object valueParameter = entry.getValue(); @@ -98,53 +99,46 @@ private RestRequest addParameter(RestRequest 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) 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) 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) valueParameter); - continue mapP; + }else if (valueParameter instanceof Map) { + try { + Map mapString= (Map) valueParameter; + entityRequest.add(mapString); + } catch (Exception e) { + Logger.e("参数Map传入值类型错误.Map键值类型:key=String类型,value=String类型"); + } + }else{ + entityRequest.add(keyParameter, valueParameter.toString()); } } } @@ -168,15 +162,32 @@ private RestRequest addParameter(RestRequest 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类型未知"); } }