Search in sources :

Example 11 with JSONException

use of weibo4j.org.json.JSONException in project twitter-2-weibo by rjyo.

the class HttpClient method httpRequest.

public Response httpRequest(HttpMethod method, Boolean WithTokenHeader) throws WeiboException {
    InetAddress ipaddr;
    int responseCode = -1;
    try {
        ipaddr = InetAddress.getLocalHost();
        List<Header> headers = new ArrayList<Header>();
        if (WithTokenHeader) {
            if (token == null) {
                throw new IllegalStateException("Oauth2 token is not set!");
            }
            headers.add(new Header("Authorization", "OAuth2 " + token));
            headers.add(new Header("API-RemoteIP", ipaddr.getHostAddress()));
            client.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
            for (Header hd : headers) {
                log(hd.getName() + ": " + hd.getValue());
            }
        }
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
        client.executeMethod(method);
        Header[] resHeader = method.getResponseHeaders();
        responseCode = method.getStatusCode();
        log("Response:");
        log("https StatusCode:" + String.valueOf(responseCode));
        for (Header header : resHeader) {
            log(header.getName() + ":" + header.getValue());
        }
        Response response = new Response();
        response.setResponseAsString(method.getResponseBodyAsString());
        log(response.toString() + "\n");
        if (responseCode != OK) {
            try {
                throw new WeiboException(getCause(responseCode), response.asJSONObject(), method.getStatusCode());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return response;
    } catch (IOException ioe) {
        throw new WeiboException(ioe.getMessage(), ioe, responseCode);
    } finally {
        method.releaseConnection();
    }
}
Also used : WeiboException(weibo4j.model.WeiboException) Header(org.apache.commons.httpclient.Header) ArrayList(java.util.ArrayList) DefaultHttpMethodRetryHandler(org.apache.commons.httpclient.DefaultHttpMethodRetryHandler) JSONException(weibo4j.org.json.JSONException) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Example 12 with JSONException

use of weibo4j.org.json.JSONException in project twitter-2-weibo by rjyo.

the class Emotion method constructEmotions.

public static List<Emotion> constructEmotions(Response res) throws WeiboException {
    try {
        JSONArray list = res.asJSONArray();
        int size = list.length();
        List<Emotion> emotions = new ArrayList<Emotion>(size);
        for (int i = 0; i < size; i++) {
            emotions.add(new Emotion(list.getJSONObject(i)));
        }
        return emotions;
    } catch (JSONException jsone) {
        throw new WeiboException(jsone);
    } catch (WeiboException te) {
        throw te;
    }
}
Also used : JSONArray(weibo4j.org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(weibo4j.org.json.JSONException)

Example 13 with JSONException

use of weibo4j.org.json.JSONException in project twitter-2-weibo by rjyo.

the class FavoritesTag method constructTags.

public static List<FavoritesTag> constructTags(Response res) throws WeiboException {
    try {
        JSONArray list = res.asJSONArray();
        int size = list.length();
        List<FavoritesTag> tags = new ArrayList<FavoritesTag>(size);
        for (int i = 0; i < size; i++) {
            tags.add(new FavoritesTag(list.getJSONObject(i)));
        }
        return tags;
    } catch (JSONException jsone) {
        throw new WeiboException(jsone);
    } catch (WeiboException te) {
        throw te;
    }
}
Also used : JSONArray(weibo4j.org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(weibo4j.org.json.JSONException)

Example 14 with JSONException

use of weibo4j.org.json.JSONException in project twitter-2-weibo by rjyo.

the class Status method constructWapperStatus.

public static StatusWapper constructWapperStatus(Response res) throws WeiboException {
    //asJSONArray();
    JSONObject jsonStatus = res.asJSONObject();
    JSONArray statuses = null;
    try {
        if (!jsonStatus.isNull("statuses")) {
            statuses = jsonStatus.getJSONArray("statuses");
        }
        if (!jsonStatus.isNull("reposts")) {
            statuses = jsonStatus.getJSONArray("reposts");
        }
        int size = statuses.length();
        List<Status> status = new ArrayList<Status>(size);
        for (int i = 0; i < size; i++) {
            status.add(new Status(statuses.getJSONObject(i)));
        }
        long previousCursor = jsonStatus.getLong("previous_curosr");
        long nextCursor = jsonStatus.getLong("next_cursor");
        long totalNumber = jsonStatus.getLong("total_number");
        String hasvisible = jsonStatus.getString("hasvisible");
        return new StatusWapper(status, previousCursor, nextCursor, totalNumber, hasvisible);
    } catch (JSONException jsone) {
        throw new WeiboException(jsone);
    }
}
Also used : JSONObject(weibo4j.org.json.JSONObject) JSONArray(weibo4j.org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(weibo4j.org.json.JSONException)

Example 15 with JSONException

use of weibo4j.org.json.JSONException in project twitter-2-weibo by rjyo.

the class Tag method constructTagWapper.

public static TagWapper constructTagWapper(Response res) {
    try {
        JSONArray tags = res.asJSONArray();
        List<Tag> tagList = new ArrayList<Tag>();
        for (int i = 0; i < tags.getJSONObject(0).getJSONArray("tags").length(); i++) {
            tagList.add(new Tag(tags.getJSONObject(0).getJSONArray("tags").getJSONObject(i)));
        }
        String id = tags.getJSONObject(0).getString("id");
        return new TagWapper(tagList, id);
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (WeiboException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : JSONArray(weibo4j.org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(weibo4j.org.json.JSONException)

Aggregations

JSONException (weibo4j.org.json.JSONException)20 JSONArray (weibo4j.org.json.JSONArray)16 ArrayList (java.util.ArrayList)14 JSONObject (weibo4j.org.json.JSONObject)8 Date (java.util.Date)2 WeiboException (weibo4j.model.WeiboException)2 HttpServletRouter (h2weibo.HttpServletRouter)1 DBHelper (h2weibo.model.DBHelper)1 T2WUser (h2weibo.model.T2WUser)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 Iterator (java.util.Iterator)1 ServletConfig (javax.servlet.ServletConfig)1 HttpSession (javax.servlet.http.HttpSession)1 DefaultHttpMethodRetryHandler (org.apache.commons.httpclient.DefaultHttpMethodRetryHandler)1 Header (org.apache.commons.httpclient.Header)1 GenericObjectPool (org.apache.commons.pool.impl.GenericObjectPool)1 JedisPool (redis.clients.jedis.JedisPool)1 twitter4j (twitter4j)1 RequestToken (twitter4j.auth.RequestToken)1