com.hippo.util.ExceptionUtils Java Examples

The following examples show how to use com.hippo.util.ExceptionUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TypeSendFragment.java    From Nimingban with Apache License 2.0 6 votes vote down vote up
@Override
public void onFailure(final Exception e) {
    mImage = null;

    if (!TextUtils.isEmpty(mContent)) {
        DB.addDraft(mContent);
    }

    SimpleHandler.getInstance().postDelayed(new Runnable() {
        @Override
        public void run() {
            Context context = mContext;
            Toast.makeText(context, context.getString(mMethod == METHOD_REPLY ? R.string.reply_failed :
                    R.string.create_post_failed) + "\n" +
                    ExceptionUtils.getReadableString(context, e), Toast.LENGTH_SHORT).show();
            Messenger.getInstance().notify(METHOD_REPLY == mMethod ?
                    Constants.MESSENGER_ID_REPLY : Constants.MESSENGER_ID_CREATE_POST, mId);
        }
    }, 1000); // Wait a seconds to make sure the server has done with the post
}
 
Example #2
Source File: EhEngine.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
private static ProfileParser.Result getProfileInternal(@Nullable EhClient.Task task,
                                                       OkHttpClient okHttpClient, String url, String referer) throws Throwable {
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, referer).build();
    Call call = okHttpClient.newCall(request);

    // Put call
    if (null != task) {
        task.setCall(call);
    }

    String body = null;
    Headers headers = null;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        return ProfileParser.parse(body);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        throwException(call, code, headers, body, e);
        throw e;
    }
}
 
Example #3
Source File: EhTagDatabase.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
private static boolean save(OkHttpClient client, String url, File file) {
  Request request = new Request.Builder().url(url).build();
  Call call = client.newCall(request);
  try (Response response = call.execute()) {
    if (!response.isSuccessful()) {
      return false;
    }
    ResponseBody body = response.body();
    if (body == null) {
      return false;
    }

    try (InputStream is = body.byteStream(); OutputStream os = new FileOutputStream(file)) {
      IOUtils.copy(is, os);
    }

    return true;
  } catch (Throwable t) {
    ExceptionUtils.throwIfFatal(t);
    return false;
  }
}
 
Example #4
Source File: ContentLayout.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
public void onGetException(int taskId, Exception e) {
    if (mCurrentTaskId == taskId) {
        mRefreshLayout.setHeaderRefreshing(false);
        mRefreshLayout.setFooterRefreshing(false);

        String readableError;
        if (e != null) {
            e.printStackTrace();
            readableError = ExceptionUtils.getReadableString(e);
        } else {
            readableError = getContext().getString(R.string.error_unknown);
        }

        if (mViewTransition.getShownViewIndex() == 0) {
            Toast.makeText(getContext(), readableError, Toast.LENGTH_SHORT).show();
        } else {
            showText(readableError);
        }
    }
}
 
Example #5
Source File: EhTagDatabase.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
private static boolean save(OkHttpClient client, String url, File file) {
  Request request = new Request.Builder().url(url).build();
  Call call = client.newCall(request);
  try (Response response = call.execute()) {
    if (!response.isSuccessful()) {
      return false;
    }
    ResponseBody body = response.body();
    if (body == null) {
      return false;
    }

    try (InputStream is = body.byteStream(); OutputStream os = new FileOutputStream(file)) {
      IOUtils.copy(is, os);
    }

    return true;
  } catch (Throwable t) {
    ExceptionUtils.throwIfFatal(t);
    return false;
  }
}
 
Example #6
Source File: ActivityPreference.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
@Override
protected void onClick() {
    if (null == mActivityClazz) {
        return;
    }

    Context context = getContext();
    Intent intent = new Intent(context, mActivityClazz);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {
        context.startActivity(intent);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        Toast.makeText(context, R.string.error_cant_find_activity, Toast.LENGTH_SHORT).show();
    }
}
 
Example #7
Source File: EhProxySelector.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
@Override
public List<Proxy> select(URI uri) {
  int type = Settings.getProxyType();
  if (type == TYPE_HTTP || type == TYPE_SOCKS) {
    try {
      String ip = Settings.getProxyIp();
      int port = Settings.getProxyPort();
      if (!TextUtils.isEmpty(ip) && InetValidator.isValidInetPort(port)) {
        InetAddress inetAddress = InetAddress.getByName(ip);
        SocketAddress socketAddress = new InetSocketAddress(inetAddress, port);
        return Collections.singletonList(new Proxy(type == TYPE_HTTP ? Proxy.Type.HTTP : Proxy.Type.SOCKS, socketAddress));
      }
    } catch (Throwable t) {
      ExceptionUtils.throwIfFatal(t);
    }
  }

  if (delegation != null) {
    return delegation.select(uri);
  }

  return alternative.select(uri);
}
 
Example #8
Source File: GalleryDetailParser.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
@NonNull
public static GalleryTagGroup[] parseTagGroups(Elements trs) {
    try {
        List<GalleryTagGroup> list = new ArrayList<>(trs.size());
        for (int i = 0, n = trs.size(); i < n; i++) {
            GalleryTagGroup group = parseTagGroup(trs.get(i));
            if (null != group) {
                list.add(group);
            }
        }
        return list.toArray(new GalleryTagGroup[list.size()]);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        e.printStackTrace();
        return EMPTY_GALLERY_TAG_GROUP_ARRAY;
    }
}
 
Example #9
Source File: CommonOperations.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
@Override
protected JSONObject doInBackground(Void... params) {
    String url;
    if (Settings.getBetaUpdateChannel()) {
        url = "https://raw.githubusercontent.com/axlecho/MHViewer/api/update_beta.json";
    } else {
        url = "https://raw.githubusercontent.com/axlecho/MHViewer/api/update.json";
    }

    try {
        return fetchUpdateInfo(url);
    } catch (Throwable e2) {
        ExceptionUtils.throwIfFatal(e2);
        return null;
    }
}
 
Example #10
Source File: SpiderQueen.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
private SpiderInfo readSpiderInfoFromInternet() {
    try {
        SpiderInfo spiderInfo = new SpiderInfo();
        spiderInfo.gid = mGalleryInfo.gid;
        spiderInfo.token = mGalleryInfo.token;

        Request request = new EhRequestBuilder(EhUrl.getGalleryDetailUrl(
                mGalleryInfo.gid, mGalleryInfo.token, 0, false), EhUrl.getReferer()).build();
        Response response = mHttpClient.newCall(request).execute();
        String body = response.body().string();

        spiderInfo.pages = GalleryDetailParser.parsePages(body);
        spiderInfo.pTokenMap = new SparseArray<>(spiderInfo.pages);
        readPreviews(body, 0, spiderInfo);
        return spiderInfo;
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        return null;
    }
}
 
Example #11
Source File: Settings.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
@Nullable
public static UniFile getDownloadLocation() {
    UniFile dir = null;
    try {
        Uri.Builder builder = new Uri.Builder();
        builder.scheme(getString(KEY_DOWNLOAD_SAVE_SCHEME, null));
        builder.encodedAuthority(getString(KEY_DOWNLOAD_SAVE_AUTHORITY, null));
        builder.encodedPath(getString(KEY_DOWNLOAD_SAVE_PATH, null));
        builder.encodedQuery(getString(KEY_DOWNLOAD_SAVE_QUERY, null));
        builder.encodedFragment(getString(KEY_DOWNLOAD_SAVE_FRAGMENT, null));
        dir = UniFile.fromUri(sContext, builder.build());
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        // Ignore
    }
    return dir != null ? dir : UniFile.fromFile(AppConfig.getDefaultDownloadDir());
}
 
Example #12
Source File: ActivityPreference.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
@Override
protected void onClick() {
    if (null == mActivityClazz) {
        return;
    }

    Context context = getContext();
    Intent intent = new Intent(context, mActivityClazz);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {
        context.startActivity(intent);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        Toast.makeText(context, R.string.error_cant_find_activity, Toast.LENGTH_SHORT).show();
    }
}
 
Example #13
Source File: ContentLayout.java    From Nimingban with Apache License 2.0 6 votes vote down vote up
public void onGetExpection(int taskId, Exception e) {
    if (mCurrentTaskId == taskId) {
        if (e != null) {
            e.printStackTrace();
        }

        mRefreshLayout.setHeaderRefreshing(false);
        mRefreshLayout.setFooterRefreshing(false);
        Say.d(TAG, "Get page data failed " + e.getClass().getName() + " " + e.getMessage());
        String readableError = ExceptionUtils.getReadableString(getContext(), e);
        String reason = ExceptionUtils.getReasonString(getContext(), e);
        if (reason != null) {
            readableError += '\n' + reason;
        }
        if (mViewTransition.getShownViewIndex() == 0) {
            Toast.makeText(getContext(), readableError, Toast.LENGTH_SHORT).show();
        } else {
            showText(readableError);
        }
    }
}
 
Example #14
Source File: ContentLayout.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
public void onGetException(int taskId, Exception e) {
    if (mCurrentTaskId == taskId) {
        mRefreshLayout.setHeaderRefreshing(false);
        mRefreshLayout.setFooterRefreshing(false);

        String readableError;
        if (e != null) {
            e.printStackTrace();
            if (e instanceof HttpException && ((HttpException) e).code() == 401) {
                // do login
                mTipView.setOnClickListener(loginListener);
            }

            readableError = ExceptionUtils.getReadableString(e);
        } else {
            readableError = getContext().getString(R.string.error_unknown);
        }

        if (mViewTransition.getShownViewIndex() == 0) {
            Toast.makeText(getContext(), readableError, Toast.LENGTH_SHORT).show();
        } else {
            showText(readableError);
        }
    }
}
 
Example #15
Source File: EhEngine.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static String getGalleryToken(@Nullable EhClient.Task task, OkHttpClient okHttpClient,
        long gid, String gtoken, int page) throws Throwable {
    JSONObject json = new JSONObject()
            .put("method", "gtoken")
            .put("pagelist", new JSONArray().put(
                    new JSONArray().put(gid).put(gtoken).put(page + 1)));
    final RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JSON, json.toString());
    String url = EhUrl.getApiUrl();
    String referer = EhUrl.getReferer();
    String origin = EhUrl.getOrigin();
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, referer, origin)
            .post(requestBody)
            .build();
    Call call = okHttpClient.newCall(request);

    // Put call
    if (null != task) {
        task.setCall(call);
    }

    String body = null;
    Headers headers = null;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        return GalleryTokenApiParser.parse(body);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        throwException(call, code, headers, body, e);
        throw e;
    }
}
 
Example #16
Source File: EhEngine.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static String signIn(@Nullable EhClient.Task task, OkHttpClient okHttpClient,
        String username, String password) throws Throwable {
    FormBody.Builder builder = new FormBody.Builder()
            .add("UserName", username)
            .add("PassWord", password)
            .add("submit", "Log me in")
            .add("CookieDate", "1")
            .add("temporary_https", "off");
    String url = EhUrl.API_SIGN_IN;
    String referer = "https://forums.e-hentai.org/index.php?act=Login&CODE=00";
    String origin = "https://forums.e-hentai.org";
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, referer, origin)
            .post(builder.build())
            .build();
    Call call = okHttpClient.newCall(request);

    // Put call
    if (null != task) {
        task.setCall(call);
    }

    String body = null;
    Headers headers = null;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        return SignInParser.parse(body);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        throwException(call, code, headers, body, e);
        throw e;
    }
}
 
Example #17
Source File: EhEngine.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static RateGalleryParser.Result rateGallery(@Nullable EhClient.Task task,
        OkHttpClient okHttpClient, long apiUid, String apiKey, long gid,
        String token, float rating) throws Throwable {
    final JSONObject json = new JSONObject();
    json.put("method", "rategallery");
    json.put("apiuid", apiUid);
    json.put("apikey", apiKey);
    json.put("gid", gid);
    json.put("token", token);
    json.put("rating", (int) Math.ceil(rating * 2));
    final RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JSON, json.toString());
    String url = EhUrl.getApiUrl();
    String referer = EhUrl.getGalleryDetailUrl(gid, token);
    String origin = EhUrl.getOrigin();
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, referer, origin)
            .post(requestBody)
            .build();
    Call call = okHttpClient.newCall(request);

    // Put call
    if (null != task) {
        task.setCall(call);
    }

    String body = null;
    Headers headers = null;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        return RateGalleryParser.parse(body);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        throwException(call, code, headers, body, e);
        throw e;
    }
}
 
Example #18
Source File: GalleryListParser.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
private static int parsePages(Document d, String body) throws ParseException {
    try {
        Elements es = d.getElementsByClass("ptt").first().child(0).child(0).children();
        return Integer.parseInt(es.get(es.size() - 2).text().trim());
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        throw new ParseException("Can't parse gallery list pages", body);
    }
}
 
Example #19
Source File: EhEngine.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static Pair<String, Pair<String, String>[]> getArchiveList(@Nullable EhClient.Task task, OkHttpClient okHttpClient,
        String url, long gid, String token) throws Throwable {
    String referer = EhUrl.getGalleryDetailUrl(gid, token);
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, referer).build();
    Call call = okHttpClient.newCall(request);

    // Put call
    if (null != task) {
        task.setCall(call);
    }

    String body = null;
    Headers headers = null;
    Pair<String, Pair<String, String>[]> result;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        result = ArchiveParser.parse(body);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        throwException(call, code, headers, body, e);
        throw e;
    }

    return result;
}
 
Example #20
Source File: EhApplication.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Override
public void unbindService(ServiceConnection conn) {
    try {
        super.unbindService(conn);
    } catch (Throwable t) {
        ExceptionUtils.throwIfFatal(t);
    }
}
 
Example #21
Source File: EhApplication.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Override
public boolean bindService(Intent service, ServiceConnection conn, int flags) {
    try {
        return super.bindService(service, conn, flags);
    } catch (Throwable t) {
        ExceptionUtils.throwIfFatal(t);
        return false;
    }
}
 
Example #22
Source File: EhApplication.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Override
public ComponentName startService(Intent service) {
    try {
        return super.startService(service);
    } catch (Throwable t) {
        ExceptionUtils.throwIfFatal(t);
        return null;
    }
}
 
Example #23
Source File: GalleryActivity.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
private void saveImageTo(int page) {
    if (null == mGalleryProvider) {
        return;
    }
    File dir = getCacheDir();
    UniFile file;
    if (null == (file = mGalleryProvider.save(page, UniFile.fromFile(dir), mGalleryProvider.getImageFilename(page)))) {
        Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
        return;
    }
    String filename = file.getName();
    if (filename == null) {
        Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
        return;
    }
    mCacheFileName = filename;
    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_TITLE, filename);
    try {
        startActivityForResult(intent, WRITE_REQUEST_CODE);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        Toast.makeText(this, R.string.error_cant_find_activity, Toast.LENGTH_SHORT).show();
    }
}
 
Example #24
Source File: EhEngine.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static ProfileParser.Result getProfile(@Nullable EhClient.Task task,
        OkHttpClient okHttpClient) throws Throwable {
    String url = EhUrl.URL_FORUMS;
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, null).build();
    Call call = okHttpClient.newCall(request);

    // Put call
    if (null != task) {
        task.setCall(call);
    }

    String body = null;
    Headers headers = null;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        return getProfileInternal(task, okHttpClient, ForumsParser.parse(body), url);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        throwException(call, code, headers, body, e);
        throw e;
    }
}
 
Example #25
Source File: EhEngine.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static VoteCommentParser.Result voteComment(@Nullable EhClient.Task task, OkHttpClient okHttpClient,
        long apiUid, String apiKey, long gid, String token, long commentId, int commentVote) throws Throwable {
    final JSONObject json = new JSONObject();
    json.put("method", "votecomment");
    json.put("apiuid", apiUid);
    json.put("apikey", apiKey);
    json.put("gid", gid);
    json.put("token", token);
    json.put("comment_id", commentId);
    json.put("comment_vote", commentVote);
    final RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JSON, json.toString());
    String url = EhUrl.getApiUrl();
    String referer = EhUrl.getReferer();
    String origin = EhUrl.getOrigin();
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, referer, origin)
            .post(requestBody)
            .build();
    Call call = okHttpClient.newCall(request);

    // Put call
    if (null != task) {
        task.setCall(call);
    }

    String body = null;
    Headers headers = null;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        return VoteCommentParser.parse(body, commentVote);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        throwException(call, code, headers, body, e);
        throw e;
    }
}
 
Example #26
Source File: EhEngine.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static GalleryPageParser.Result getGalleryPage(@Nullable EhClient.Task task,
        OkHttpClient okHttpClient, String url, long gid, String token) throws Throwable {
    String referer = EhUrl.getGalleryDetailUrl(gid, token);
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, referer).build();
    Call call = okHttpClient.newCall(request);

    // Put call
    if (null != task) {
        task.setCall(call);
    }

    String body = null;
    Headers headers = null;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        return GalleryPageParser.parse(body);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        throwException(call, code, headers, body, e);
        throw e;
    }
}
 
Example #27
Source File: SafeCoordinatorLayout.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    try {
        return super.onInterceptTouchEvent(ev);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        return false;
    }
}
 
Example #28
Source File: TypeSendFragment.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
@Override
public void onFailure(Exception e) {
    if (mProgressDialog != null) {
        mProgressDialog.dismiss();
        mProgressDialog = null;
    }
    mNMBRequest = null;

    Toast.makeText(getContext(), getString(R.string.cant_get_cookies) + "\n" +
            ExceptionUtils.getReadableString(getContext(), e), Toast.LENGTH_SHORT).show();
}
 
Example #29
Source File: EhApplication.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@Override
public ComponentName startService(Intent service) {
    try {
        return super.startService(service);
    } catch (Throwable t) {
        ExceptionUtils.throwIfFatal(t);
        return null;
    }
}
 
Example #30
Source File: GalleryDetailParser.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
private static GalleryTagGroup parseTagGroup(Element element) {
    try {
        GalleryTagGroup group = new GalleryTagGroup();

        String nameSpace = element.child(0).text();
        // Remove last ':'
        nameSpace = nameSpace.substring(0, nameSpace.length() - 1);
        group.groupName = nameSpace;

        Elements tags = element.child(1).children();
        for (int i = 0, n = tags.size(); i < n; i++) {
            String tag = tags.get(i).text();
            // Sometimes parody tag is followed with '|' and english translate, just remove them
            int index = tag.indexOf('|');
            if (index >= 0) {
                tag = tag.substring(0, index).trim();
            }
            group.addTag(tag);
        }

        return group.size() > 0 ? group : null;
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        e.printStackTrace();
        return null;
    }
}