作者 crossoverJie

:bug: Fixing a bug.修复 okhttp 没关闭body

@@ -69,8 +69,12 @@ public class RouteRequestImpl implements RouteRequest { @@ -69,8 +69,12 @@ public class RouteRequestImpl implements RouteRequest {
69 .build(); 69 .build();
70 70
71 Response response = okHttpClient.newCall(request).execute() ; 71 Response response = okHttpClient.newCall(request).execute() ;
72 - if (!response.isSuccessful()){  
73 - throw new IOException("Unexpected code " + response); 72 + try {
  73 + if (!response.isSuccessful()){
  74 + throw new IOException("Unexpected code " + response);
  75 + }
  76 + }finally {
  77 + response.body().close();
74 } 78 }
75 } 79 }
76 80
@@ -92,12 +96,18 @@ public class RouteRequestImpl implements RouteRequest { @@ -92,12 +96,18 @@ public class RouteRequestImpl implements RouteRequest {
92 throw new IOException("Unexpected code " + response); 96 throw new IOException("Unexpected code " + response);
93 } 97 }
94 98
95 - String json = response.body().string() ;  
96 - BaseResponse baseResponse = JSON.parseObject(json, BaseResponse.class); 99 + ResponseBody body = response.body();
  100 + try {
  101 + String json = body.string() ;
  102 + BaseResponse baseResponse = JSON.parseObject(json, BaseResponse.class);
97 103
98 - //选择的账号不存在  
99 - if (baseResponse.getCode().equals(StatusEnum.OFF_LINE.getCode())){  
100 - LOGGER.error(p2PReqVO.getReceiveUserId() + ":" + StatusEnum.OFF_LINE.getMessage()); 104 + //选择的账号不存在
  105 + if (baseResponse.getCode().equals(StatusEnum.OFF_LINE.getCode())){
  106 + LOGGER.error(p2PReqVO.getReceiveUserId() + ":" + StatusEnum.OFF_LINE.getMessage());
  107 + }
  108 +
  109 + }finally {
  110 + body.close();
101 } 111 }
102 } 112 }
103 113
@@ -118,14 +128,20 @@ public class RouteRequestImpl implements RouteRequest { @@ -118,14 +128,20 @@ public class RouteRequestImpl implements RouteRequest {
118 if (!response.isSuccessful()){ 128 if (!response.isSuccessful()){
119 throw new IOException("Unexpected code " + response); 129 throw new IOException("Unexpected code " + response);
120 } 130 }
121 -  
122 - String json = response.body().string();  
123 - CIMServerResVO cimServerResVO = JSON.parseObject(json, CIMServerResVO.class);  
124 -  
125 - //重复失败  
126 - if (!cimServerResVO.getCode().equals(StatusEnum.SUCCESS.getCode())){  
127 - LOGGER.error(appConfiguration.getUserName() + ":" + cimServerResVO.getMessage());  
128 - System.exit(-1); 131 + CIMServerResVO cimServerResVO ;
  132 + ResponseBody body = response.body();
  133 + try {
  134 + String json = body.string();
  135 + cimServerResVO = JSON.parseObject(json, CIMServerResVO.class);
  136 +
  137 + //重复失败
  138 + if (!cimServerResVO.getCode().equals(StatusEnum.SUCCESS.getCode())){
  139 + LOGGER.error(appConfiguration.getUserName() + ":" + cimServerResVO.getMessage());
  140 + System.exit(-1);
  141 + }
  142 +
  143 + }finally {
  144 + body.close();
129 } 145 }
130 146
131 147
@@ -150,8 +166,15 @@ public class RouteRequestImpl implements RouteRequest { @@ -150,8 +166,15 @@ public class RouteRequestImpl implements RouteRequest {
150 } 166 }
151 167
152 168
153 - String json = response.body().string() ;  
154 - OnlineUsersResVO onlineUsersResVO = JSON.parseObject(json, OnlineUsersResVO.class); 169 + ResponseBody body = response.body();
  170 + OnlineUsersResVO onlineUsersResVO ;
  171 + try {
  172 + String json = body.string() ;
  173 + onlineUsersResVO = JSON.parseObject(json, OnlineUsersResVO.class);
  174 +
  175 + }finally {
  176 + body.close();
  177 + }
155 178
156 return onlineUsersResVO.getDataBody(); 179 return onlineUsersResVO.getDataBody();
157 } 180 }
@@ -163,8 +163,12 @@ public class AccountServiceRedisImpl implements AccountService { @@ -163,8 +163,12 @@ public class AccountServiceRedisImpl implements AccountService {
163 .build(); 163 .build();
164 164
165 Response response = okHttpClient.newCall(request).execute(); 165 Response response = okHttpClient.newCall(request).execute();
166 - if (!response.isSuccessful()) {  
167 - throw new IOException("Unexpected code " + response); 166 + try {
  167 + if (!response.isSuccessful()) {
  168 + throw new IOException("Unexpected code " + response);
  169 + }
  170 + }finally {
  171 + response.body().close();
168 } 172 }
169 } 173 }
170 174