|
@@ -88,202 +88,11 @@ public class CommonAddAndHandleInterceptor implements Interceptor {
|
|
|
Response originalResponse = chain.proceed(newRequest);
|
|
|
LogUtils.i(mTag + "调用拦截器处理获取结果originalResponse" + originalResponse);
|
|
|
|
|
|
- // 返回体
|
|
|
- ResponseBody responseBody = originalResponse.body();
|
|
|
|
|
|
- Response newResponse = null;
|
|
|
|
|
|
- try {
|
|
|
- JSONObject jsonObject = new JSONObject(responseBody.string());
|
|
|
-
|
|
|
- Object result = null;
|
|
|
- try {
|
|
|
- result = jsonObject.get(serverResult);
|
|
|
- } catch (JSONException e) {
|
|
|
- e.printStackTrace();
|
|
|
- // 处理服务端没有返回result的情况
|
|
|
- LogUtils.i(mTag + "需要处理,原始返回的Json没有result字段");
|
|
|
- JSONObject newJsonObject = new JSONObject();
|
|
|
- JSONObject obj = new JSONObject();
|
|
|
- newJsonObject.put(serverResult, obj); // serverResult: {}
|
|
|
- newJsonObject.put("code", jsonObject.getInt("code"));
|
|
|
- newJsonObject.put("message", jsonObject.getString("message"));
|
|
|
- newJsonObject.put("success", jsonObject.getBoolean("success"));
|
|
|
- LogUtils.i(mTag + "newJsonObject:" + newJsonObject.toString());
|
|
|
-
|
|
|
- //获取返回值类型
|
|
|
- MediaType mediaType = responseBody.contentType();
|
|
|
- newResponse = new Response.Builder()
|
|
|
- .code(200)
|
|
|
- .body(ResponseBody.create(mediaType,
|
|
|
-
|
|
|
- newJsonObject.toString()))
|
|
|
- .request(newRequest)
|
|
|
- .protocol(Protocol.HTTP_1_1)
|
|
|
- .message("OK")
|
|
|
- .build();
|
|
|
- LogUtils.i(mTag + "newResponse:" + newResponse.toString());
|
|
|
-
|
|
|
- //拿到结果进行处理后返回
|
|
|
- return newResponse;
|
|
|
- }
|
|
|
-
|
|
|
- //接下来都是有result字段的处理
|
|
|
-
|
|
|
- LogUtils.i(mTag + "result:" + result);
|
|
|
-
|
|
|
-// if(result.equals(null)){
|
|
|
-// LogUtils.i(mTag+"equals(null)");
|
|
|
-// }
|
|
|
-// if(jsonObject.has(serverResult)){
|
|
|
-// LogUtils.i(mTag+"has(\"result\")");
|
|
|
-// }
|
|
|
-
|
|
|
-
|
|
|
- if (result.equals(null)) {
|
|
|
- // 处理serverResult: null 的情况 result或data取决于服务端返回数据名
|
|
|
-
|
|
|
- JSONObject newJsonObject = new JSONObject();
|
|
|
- LogUtils.i(mTag + "需要处理,原始返回的Json--result==null");
|
|
|
- JSONObject obj = new JSONObject();
|
|
|
-// obj.put("list", jsonObject.getJSONArray("data"));
|
|
|
- newJsonObject.put(serverResult, obj); // serverResult: {}
|
|
|
- newJsonObject.put("code", jsonObject.getInt("code"));
|
|
|
- newJsonObject.put(message, jsonObject.getString(message));
|
|
|
-// newJsonObject.put("success", jsonObject.getBoolean("success"));
|
|
|
- LogUtils.i(mTag + "newJsonObject:" + newJsonObject.toString());
|
|
|
-
|
|
|
- //获取返回值类型
|
|
|
- MediaType mediaType = responseBody.contentType();
|
|
|
- newResponse = new Response.Builder()
|
|
|
- .code(200)
|
|
|
- .body(ResponseBody.create(mediaType,
|
|
|
-
|
|
|
- newJsonObject.toString()))
|
|
|
- .request(newRequest)
|
|
|
- .protocol(Protocol.HTTP_1_1)
|
|
|
- .message("OK")
|
|
|
- .build();
|
|
|
- LogUtils.i(mTag + "newResponse:" + newResponse.toString());
|
|
|
-
|
|
|
- //拿到结果进行处理后返回
|
|
|
- return newResponse;
|
|
|
-
|
|
|
- } else if (jsonObject.get(serverResult) instanceof JSONArray) {
|
|
|
- // 处理result是数组的情况
|
|
|
- JSONObject newJsonObject = new JSONObject();
|
|
|
- LogUtils.i(mTag + "需要处理,原始返回的Json--result是数组");
|
|
|
- JSONObject obj = new JSONObject();
|
|
|
- obj.put("list", jsonObject.getJSONArray(serverResult));
|
|
|
- newJsonObject.put(serverResult, obj);
|
|
|
- newJsonObject.put("code", jsonObject.getInt("code"));
|
|
|
- newJsonObject.put(message, jsonObject.getString(message));
|
|
|
-// newJsonObject.put("success", jsonObject.getBoolean("success"));
|
|
|
- LogUtils.i("newJsonObject:" + newJsonObject.toString());
|
|
|
-
|
|
|
- //获取返回值类型
|
|
|
- MediaType mediaType = responseBody.contentType();
|
|
|
-
|
|
|
- newResponse = new Response.Builder()
|
|
|
- .code(200)
|
|
|
- .body(ResponseBody.create(mediaType,
|
|
|
-
|
|
|
- newJsonObject.toString()))
|
|
|
- .request(newRequest)
|
|
|
- .protocol(Protocol.HTTP_1_1)
|
|
|
- .message("OK")
|
|
|
- .build();
|
|
|
- LogUtils.i(mTag + "newResponse:" + newResponse.toString());
|
|
|
-
|
|
|
- //拿到结果进行处理后返回
|
|
|
- return newResponse;
|
|
|
-
|
|
|
- } else if (jsonObject.get(serverResult) instanceof String) {
|
|
|
- // 处理result是string的情况
|
|
|
- JSONObject newJsonObject = new JSONObject();
|
|
|
- LogUtils.i(mTag + "需要处理,原始返回的Json--result是string");
|
|
|
- JSONObject obj = new JSONObject();
|
|
|
- obj.put("string", jsonObject.getString(serverResult));
|
|
|
- newJsonObject.put(serverResult, obj);
|
|
|
- newJsonObject.put("code", jsonObject.getInt("code"));
|
|
|
- newJsonObject.put(message, jsonObject.getString(message));
|
|
|
-// newJsonObject.put("success", jsonObject.getBoolean("success"));
|
|
|
- LogUtils.i("newJsonObject:" + newJsonObject.toString());
|
|
|
-
|
|
|
- //获取返回值类型
|
|
|
- MediaType mediaType = responseBody.contentType();
|
|
|
-
|
|
|
- newResponse = new Response.Builder()
|
|
|
- .code(200)
|
|
|
- .body(ResponseBody.create(mediaType,
|
|
|
-
|
|
|
- newJsonObject.toString()))
|
|
|
- .request(newRequest)
|
|
|
- .protocol(Protocol.HTTP_1_1)
|
|
|
- .message("OK")
|
|
|
- .build();
|
|
|
- LogUtils.i(mTag + "newResponse:" + newResponse.toString());
|
|
|
-
|
|
|
- //拿到结果进行处理后返回
|
|
|
- return newResponse;
|
|
|
-
|
|
|
- } else if (jsonObject.get(serverResult) instanceof Integer) {
|
|
|
- // 处理result是Integer的情况
|
|
|
- JSONObject newJsonObject = new JSONObject();
|
|
|
- LogUtils.i(mTag + "需要处理,原始返回的Json--result是Integer");
|
|
|
- JSONObject obj = new JSONObject();
|
|
|
- obj.put("intValue", jsonObject.getInt(serverResult));
|
|
|
- newJsonObject.put(serverResult, obj);
|
|
|
- newJsonObject.put("code", jsonObject.getInt("code"));
|
|
|
- newJsonObject.put(message, jsonObject.getString(message));
|
|
|
-// newJsonObject.put("success", jsonObject.getBoolean("success"));
|
|
|
- LogUtils.i("newJsonObject:" + newJsonObject.toString());
|
|
|
-
|
|
|
- //获取返回值类型
|
|
|
- MediaType mediaType = responseBody.contentType();
|
|
|
-
|
|
|
- newResponse = new Response.Builder()
|
|
|
- .code(200)
|
|
|
- .body(ResponseBody.create(mediaType,
|
|
|
-
|
|
|
- newJsonObject.toString()))
|
|
|
- .request(newRequest)
|
|
|
- .protocol(Protocol.HTTP_1_1)
|
|
|
- .message("OK")
|
|
|
- .build();
|
|
|
- LogUtils.i(mTag + "newResponse:" + newResponse.toString());
|
|
|
-
|
|
|
- //拿到结果进行处理后返回
|
|
|
- return newResponse;
|
|
|
-
|
|
|
- } else {
|
|
|
- LogUtils.i(mTag + "非需要处理的url,直接返回");
|
|
|
- // 重复使用chain.proceed(request)导致会出现重复请求问题
|
|
|
-// newResponse = chain.proceed(newRequest);
|
|
|
-// LogUtils.i(mTag + "originalResponse:" + originalResponse.toString());
|
|
|
-// return newResponse;
|
|
|
-
|
|
|
- //获取返回值类型
|
|
|
- MediaType mediaType = responseBody.contentType();
|
|
|
-
|
|
|
- newResponse = new Response.Builder()
|
|
|
- .code(200)
|
|
|
- .body(ResponseBody.create(mediaType,
|
|
|
- jsonObject.toString()))
|
|
|
- .request(newRequest)
|
|
|
- .protocol(Protocol.HTTP_1_1)
|
|
|
- .message("OK")
|
|
|
- .build();
|
|
|
-
|
|
|
- return newResponse;
|
|
|
- }
|
|
|
+ return originalResponse;
|
|
|
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- LogUtils.i(mTag + "e--:" + e.toString());
|
|
|
- }
|
|
|
// return originalResponse.newBuilder().body(responseBody).build();
|
|
|
- return originalResponse;
|
|
|
|
|
|
|
|
|
// return chain.proceed(request);
|