| | |
| | | .build(); |
| | | Request.Builder builder = request.newBuilder(); |
| | | |
| | | //添加头部token |
| | | String token = UserManager.getInstance().getToken(); |
| | | if (!TextUtils.isEmpty(token)) { |
| | | builder.addHeader("Authorization", token); |
| | | } |
| | | Request newRequest = builder.url(modifiedUrl).build(); |
| | | Response response = chain.proceed(newRequest); |
| | | |
| | | //解决response.body().string();只能打印一次 |
| | | ResponseBody responseBody = response.body(); |
| | | BufferedSource source = responseBody.source(); |
| | | source.request(Long.MAX_VALUE); // Buffer the entire body. |
| | | Buffer buffer = source.getBuffer(); |
| | | Charset UTF8 = Charset.forName("UTF-8"); |
| | | String string = buffer.clone().readString(UTF8); |
| | | try { |
| | | JSONObject jsonObject = new JSONObject(string); |
| | | int code = jsonObject.getInt("code"); |
| | | if (code == BaseConfig.TOKEN_INVALID ) { |
| | | ToastUtils.showShort("登录状态失效,请重新登录"); |
| | | UserManager.getInstance().clearToken(); |
| | | AppManager.getAppManager().finishAllActivity(); |
| | | AppManager.getAppManager().startActivityForName(BaseConfig.LOGIN_ACTIVITY); |
| | | } |
| | | } catch (JSONException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | |
| | | return response; |
| | | return chain.proceed(newRequest); |
| | | } |
| | | |
| | | } |