Search in sources :

Example 16 with LbryResponseException

use of com.odysee.app.exceptions.LbryResponseException in project odysee-android by OdyseeTeam.

the class Lbry method processErrorJson.

private static void processErrorJson(JSONObject json) throws JSONException, LbryResponseException {
    if (json.has("error")) {
        String errorMessage = null;
        Object jsonError = json.get("error");
        if (jsonError instanceof String) {
            errorMessage = jsonError.toString();
        } else {
            errorMessage = ((JSONObject) jsonError).getString("message");
        }
        throw new LbryResponseException(!Helper.isNullOrEmpty(errorMessage) ? errorMessage : json.getString("error"));
    } else {
        throw new LbryResponseException("Protocol error with unknown response signature.");
    }
}
Also used : JSONObject(org.json.JSONObject) LbryResponseException(com.odysee.app.exceptions.LbryResponseException)

Example 17 with LbryResponseException

use of com.odysee.app.exceptions.LbryResponseException in project odysee-android by OdyseeTeam.

the class CommentListTask method doInBackground.

protected List<Comment> doInBackground(Void... voids) {
    List<Comment> comments = null;
    try {
        Map<String, Object> options = new HashMap<>();
        options.put("claim_id", claim);
        options.put("page", page);
        options.put("page_size", pageSize);
        options.put("hidden", false);
        options.put("include_replies", false);
        options.put("is_channel_signature_valid", true);
        options.put("skip_validation", true);
        options.put("visible", true);
        JSONObject result = (JSONObject) Lbry.parseResponse(Comments.performRequest(Lbry.buildJsonParams(options), "comment.List"));
        if (result != null && result.has("items")) {
            JSONArray items = result.getJSONArray("items");
            List<Comment> children = new ArrayList<>();
            comments = new ArrayList<>();
            for (int i = 0; i < items.length(); i++) {
                Comment comment = Comment.fromJSONObject(items.getJSONObject(i));
                if (comment != null) {
                    if (!Helper.isNullOrEmpty(comment.getParentId())) {
                        children.add(comment);
                    } else {
                        comments.add(comment);
                    }
                }
            }
            // Sort all replies from oldest to newest at once and then group them by its parent comment
            Collections.sort(children);
            Map<String, List<Comment>> groupedChildrenList = children.stream().collect(groupingBy(Comment::getParentId));
            List<Comment> finalComments = comments;
            groupedChildrenList.forEach((key, value) -> {
                Comment c = finalComments.stream().filter(v -> key.equalsIgnoreCase(v.getId())).findFirst().orElse(null);
                finalComments.addAll(finalComments.indexOf(c) + 1, value);
            });
            comments = finalComments;
        }
    } catch (JSONException | LbryResponseException | IOException ex) {
        error = ex;
    }
    return comments;
}
Also used : Comment(com.odysee.app.model.Comment) HashMap(java.util.HashMap) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) IOException(java.io.IOException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

LbryResponseException (com.odysee.app.exceptions.LbryResponseException)17 JSONObject (org.json.JSONObject)15 JSONException (org.json.JSONException)12 LbryRequestException (com.odysee.app.exceptions.LbryRequestException)10 IOException (java.io.IOException)8 HashMap (java.util.HashMap)8 ApiCallException (com.odysee.app.exceptions.ApiCallException)7 ArrayList (java.util.ArrayList)7 LinkedHashMap (java.util.LinkedHashMap)6 JSONArray (org.json.JSONArray)6 OkHttpClient (okhttp3.OkHttpClient)5 Request (okhttp3.Request)5 Response (okhttp3.Response)5 Claim (com.odysee.app.model.Claim)3 Uri (android.net.Uri)2 ClaimCacheKey (com.odysee.app.model.ClaimCacheKey)2 LbryFile (com.odysee.app.model.LbryFile)2 ResponseBody (okhttp3.ResponseBody)2 Handler (android.os.Handler)1 WebResourceRequest (android.webkit.WebResourceRequest)1