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();
}
}
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;
}
}
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;
}
}
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);
}
}
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;
}
Aggregations