public static void manageIfException( WebSocketSession session, String methodName, ExceptionMethodWrapper method) { try { method.exec(); } catch (Throwable t) { log.error(t.getMessage(), t); InternalErrorCodes iec; if (t instanceof AppException) { AppException ae = (AppException) t; iec = ae.getErrorCode(); } else { iec = InternalErrorCodes.UNHANDLED_ERROR; } Response resp = new Response.ResponseBuilder<>(methodName) .status(iec.getStatus().intValue()) .error(new AppError(iec.getMessage(), t.getMessage(), iec.getCode())) .build(); try { session.sendMessage(new TextMessage(resp.toJsonStr())); } catch (Exception e) { log.error(e.getMessage(), e); } } }
/** * 获取网络图片 * * @param url * @return */ public static Bitmap getBitmapByNet(String url) throws AppException { // System.out.println("image_url==> "+url); URI uri = null; try { uri = new URI(url, false, "UTF-8"); } catch (URIException e) { e.printStackTrace(); } if (uri != null) url = uri.toString(); HttpClient httpClient = null; GetMethod httpGet = null; Bitmap bitmap = null; int time = 0; do { try { httpClient = HttpHelper.getHttpClient(); httpGet = HttpHelper.getHttpGet(url, HttpHelper.getUserAgent()); int statusCode = httpClient.executeMethod(httpGet); if (statusCode != HttpStatus.SC_OK) { throw AppException.http(statusCode); } InputStream inStream = httpGet.getResponseBodyAsStream(); bitmap = BitmapFactory.decodeStream(inStream); inStream.close(); break; } catch (HttpException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // 发生致命的异常,可能是协议不对或者返回的内容有问题 e.printStackTrace(); throw AppException.http(e); } catch (IOException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // 发生网络异常 e.printStackTrace(); throw AppException.network(e); } finally { // 释放连接 httpGet.releaseConnection(); } } while (time < RETRY_TIME); return bitmap; }
public void printStackTrace(PrintWriter s) { super.printStackTrace(s); if (cause != null) { s.println("Caused by:"); cause.printStackTrace(s); } }
@Override public void onCreate() { super.onCreate(); // 注册App异常崩溃处�� Thread.setDefaultUncaughtExceptionHandler(AppException.getAppExceptionHandler()); init(); }
/** * 获取用户头像 * * @param key * @return * @throws AppException */ public Bitmap getUserFace(String key) throws AppException { FileInputStream fis = null; try { fis = openFileInput(key); return BitmapFactory.decodeStream(fis); } catch (Exception e) { throw AppException.run(e); } finally { try { fis.close(); } catch (Exception e) { } } }
@Override public void onCreate() { super.onCreate(); // 注册App异常崩溃处理器 Thread.setDefaultUncaughtExceptionHandler(AppException.getAppExceptionHandler()); }