Example usage for com.squareup.okhttp Call execute

List of usage examples for com.squareup.okhttp Call execute

Introduction

In this page you can find the example usage for com.squareup.okhttp Call execute.

Prototype

public Response execute() throws IOException 

Source Link

Document

Invokes the request immediately, and blocks until the response can be processed or is in error.

Usage

From source file:com.hippo.nimingban.client.ac.ACEngine.java

License:Apache License

public static Boolean doGetCookie(Call call) throws Exception {
    String body = null;/*  w  w  w  .  j  av a  2 s.  c o  m*/
    try {
        Response response = call.execute();
        ResponseUtils.storeCookies(response);
        body = response.body().string();

        if (!"\"ok\"".equals(body)) {
            throw new NMBException(ACSite.getInstance(), UNKNOWN);
        }

        return true;
    } catch (Exception e) {
        throwException(call, body, e);
        throw e;
    }
}

From source file:com.hippo.nimingban.client.ac.ACEngine.java

License:Apache License

public static List<CommonPost> doGetCommonPosts(Call call) throws Exception {
    String body = null;//  w  w w .ja v  a  2 s  . c  om
    try {
        Response response = call.execute();
        body = response.body().string();

        List<CommonPost> result = JSON.parseArray(body, CommonPost.class);
        if (result == null) {
            throw new NMBException(ACSite.getInstance(), "Can't parse json when getForumList");
        }
        return result;
    } catch (Exception e) {
        throwException(call, body, e);
        throw e;
    }
}

From source file:com.hippo.nimingban.client.ac.ACEngine.java

License:Apache License

public static List<ACForumGroup> doGetForumList(Call call) throws Exception {
    String body = null;//from   w  ww  .  j a  v a  2 s.co  m
    try {
        Response response = call.execute();
        body = response.body().string();

        List<ACForumGroup> result = JSON.parseArray(body, ACForumGroup.class);
        if (result == null) {
            throw new NMBException(ACSite.getInstance(), "Can't parse json when getForumList");
        }
        return result;
    } catch (Exception e) {
        throwException(call, body, e);
        throw e;
    }
}

From source file:com.hippo.nimingban.client.ac.ACEngine.java

License:Apache License

public static List<Post> doGetPostList(Call call) throws Exception {
    String body = null;//from   w w  w .j a v  a  2 s  . co  m
    try {
        Response response = call.execute();
        body = response.body().string();
        List<ACPost> acPosts = JSON.parseArray(body, ACPost.class);
        if (acPosts == null) {
            throw new NMBException(ACSite.getInstance(), "Can't parse json when getPostList");
        }

        List<Post> result = new ArrayList<>(acPosts.size());
        for (ACPost acPost : acPosts) {
            if (acPost != null) {
                acPost.generateSelfAndReplies(ACSite.getInstance());
                result.add(acPost);
            }
        }

        return result;
    } catch (Exception e) {
        throwException(call, body, e);
        throw e;
    }
}

From source file:com.hippo.nimingban.client.ac.ACEngine.java

License:Apache License

public static Pair<Post, List<Reply>> doGetPost(Call call) throws Exception {
    String body = null;//from   ww  w . ja  v  a2 s  .  c  o m
    try {
        Response response = call.execute();
        body = response.body().string();
        ACPost acPost = JSON.parseObject(body, ACPost.class);
        if (acPost == null) {
            throw new NMBException(ACSite.getInstance(), "Can't parse json when getPost");
        }
        acPost.generateSelfAndReplies(ACSite.getInstance());
        return new Pair<Post, List<Reply>>(acPost, new ArrayList<Reply>(acPost.replys));
    } catch (Exception e) {
        throwException(call, body, e);
        throw e;
    }
}

From source file:com.hippo.nimingban.client.ac.ACEngine.java

License:Apache License

public static Reply doGetReference(Call call) throws Exception {
    String body = null;//  w  w  w . j  a  va  2  s . c o  m
    try {
        Response response = call.execute();
        body = response.body().string();

        ACReference reference = new ACReference();
        Document doc = Jsoup.parse(body, ACUrl.HOST + "/");
        List<Element> elements = doc.getAllElements();
        for (Element element : elements) {
            String className = element.className();
            if ("h-threads-item-reply h-threads-item-ref".equals(className)) {
                reference.id = element.attr("data-threads-id");
            } else if ("h-threads-img-a".equals(className)) {
                reference.image = element.attr("href");
            } else if ("h-threads-img".equals(className)) {
                reference.thumb = element.attr("src");
            } else if ("h-threads-info-title".equals(className)) {
                reference.title = element.text();
            } else if ("h-threads-info-email".equals(className)) {
                // TODO email or user ?
                reference.user = element.text();
            } else if ("h-threads-info-createdat".equals(className)) {
                reference.time = element.text();
            } else if ("h-threads-info-uid".equals(className)) {
                String user = element.text();
                if (user.startsWith("ID:")) {
                    reference.userId = user.substring(3);
                } else {
                    reference.userId = user;
                }
                reference.admin = element.childNodeSize() > 1;
            } else if ("h-threads-info-id".equals(className)) {
                String href = element.attr("href");
                if (href.startsWith("/t/")) {
                    int index = href.indexOf('?');
                    if (index >= 0) {
                        reference.postId = href.substring(3, index);
                    } else {
                        reference.postId = href.substring(3);
                    }
                }
            } else if ("h-threads-content".equals(className)) {
                reference.content = element.html();
            }
        }

        reference.generate(ACSite.getInstance());

        return reference;
    } catch (Exception e) {
        throwException(call, body, e);
        throw e;
    }
}

From source file:com.hippo.nimingban.client.ac.ACEngine.java

License:Apache License

public static Void doReply(Call call) throws Exception {
    String body = null;//ww  w . j a va 2s. com
    try {
        Response response = call.execute();
        ResponseUtils.storeCookies(response);
        body = response.body().string();

        try {
            JSONObject jo = JSON.parseObject(body);
            if (jo.getBoolean("success")) {
                return null;
            } else {
                throw new NMBException(ACSite.getInstance(), jo.getString("msg"));
            }
        } catch (Exception e) {
            if (body.contains("class=\"success\"")) {
                return null;
            } else {
                throw e;
            }
        }
    } catch (Exception e) {
        throwException(call, body, e);
        throw e;
    }
}

From source file:com.hippo.nimingban.client.ac.ACEngine.java

License:Apache License

public static List<Post> doGetFeed(Call call) throws Exception {
    String body = null;//from   w w  w  .ja va  2  s. c om
    try {
        Response response = call.execute();
        body = response.body().string();

        List<ACFeed> acFeeds = JSON.parseArray(body, ACFeed.class);

        if (acFeeds == null) {
            throw new NMBException(ACSite.getInstance(), "Can't parse json when getPostList");
        }

        List<Post> result = new ArrayList<>(acFeeds.size());
        for (ACFeed feed : acFeeds) {
            if (feed != null) {
                feed.generate(ACSite.getInstance());
                result.add(feed);
            }
        }

        return result;
    } catch (Exception e) {
        throwException(call, body, e);
        throw e;
    }
}

From source file:com.hippo.nimingban.client.ac.ACEngine.java

License:Apache License

public static Void doAddFeed(Call call) throws Exception {
    String body = null;/* w ww. j  a va  2s .com*/
    try {
        Response response = call.execute();
        body = response.body().string();

        if (body.equals("\"\\u8ba2\\u9605\\u5927\\u6210\\u529f\\u2192_\\u2192\"")) {
            return null;
        } else {
            throw new NMBException(ACSite.getInstance(), UNKNOWN);
        }
    } catch (Exception e) {
        throwException(call, body, e);
        throw e;
    }
}

From source file:com.hippo.nimingban.client.ac.ACEngine.java

License:Apache License

public static Void doDelFeed(Call call) throws Exception {
    String body = null;/* w w  w.j a  v  a2s.  c om*/
    try {
        Response response = call.execute();
        body = response.body().string();

        if (body.equals("\"\\u53d6\\u6d88\\u8ba2\\u9605\\u6210\\u529f!\"")) {
            return null;
        } else {
            throw new NMBException(ACSite.getInstance(), UNKNOWN);
        }
    } catch (Exception e) {
        throwException(call, body, e);
        throw e;
    }
}