Back to project page Test-SimpleTwitterClient.
The source code is released under:
This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...
If you think the Android project Test-SimpleTwitterClient listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.Twitter_Android.Net; //www .j a v a 2 s . c o m import com.example.Twitter_Android.Logic.Person; import com.example.Twitter_Android.Logic.Tweet; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.TimeZone; class Parser { private final JSONParser parser; private final DateFormat incomingDate = new SimpleDateFormat("EE MMM dd HH:mm:ss Z yyyy"); private final DateFormat formattedDate = new SimpleDateFormat("EE dd MMM HH:mm", new Locale("ru")); public Parser() { parser = new JSONParser(); formattedDate.setTimeZone(TimeZone.getTimeZone("GMT+7")); } //------------------------------------------------------------------------------------------------------------------ /** * ???????? ???????? ?????? ?? ???????. * ?????? ?????????????? ?????? JSON ARRAY ?? ??????. * 1. ????????? ??????? ?? ????????? ?????????. * 2. ????????? ?????? ????????. * SuppressWarnings("unchecked") - ?.?. jsonArray ????????? ?????? ????????? ???? JSONObject * ?, ? ?????? ???????, ?????????? ????? ??????????. * * @param str ??????? ???? ??????? ?? ?????. * @return ???????? ??????. * @throws ParseException ????? ??????? ????????????? ??? null. */ @SuppressWarnings("unchecked") public List<Tweet> getTweets(String str) throws ParseException { Object parsedString = parser.parse(str); JSONArray jsonArray = (JSONArray) parsedString; List<Tweet> tweets = new ArrayList<>(); for (JSONObject arrayValue : (Iterable<JSONObject>) jsonArray) { Tweet tweet; JSONObject retweeted = (JSONObject) arrayValue.get("retweeted_status"); //????????, ???? ???? ??? ??????? if (retweeted != null) { //??? ??????, ????? ?????????? ????????????? ??????. tweet = getSingleTweet(retweeted); JSONObject user = (JSONObject) arrayValue.get("user"); Person retweetedBy = getPerson(user); tweet.setRetweetedBy(retweetedBy); long afterRetweetID = (long) arrayValue.get("id"); tweet.setIdAfterRetweet(afterRetweetID); } else { //??? ?? ??????. tweet = getSingleTweet(arrayValue); } tweets.add(tweet); } return tweets; } //------------------------------------------------------------------------------------------------------------------ /** * ?????????? ????????? ????. * * @param object JSONObject ???? ???????. * @return ????. */ private Tweet getSingleTweet(JSONObject object) { JSONObject user = (JSONObject) object.get("user"); Person person = getPerson(user); String text = (String) object.get("text"); String createdAt = "Unknown"; try { createdAt = getCreationDate(object); } catch (java.text.ParseException e) { e.printStackTrace(); } String lang = (String) object.get("lang"); String[] tags = getTags(object); String[] media = getMedia(object); long ID = (long) object.get("id"); return new Tweet(text, createdAt, lang, tags, person, media, ID); } //------------------------------------------------------------------------------------------------------------------ public Tweet getSingleTweet(String str) throws ParseException { return getSingleTweet((JSONObject) parser.parse(str)); } //------------------------------------------------------------------------------------------------------------------ /** * Returning next_cursor value in followings timeline. * * @param str string to parse * @return next_cursor value * @throws ParseException incorrect or null string */ public long getNextCursor(String str) throws ParseException { JSONObject parsedString = (JSONObject) parser.parse(str); return (long) parsedString.get("next_cursor"); } //------------------------------------------------------------------------------------------------------------------ /** * ???????? ???????? ??????, ????????????????? ??????????? ????????. * * @param str JSON ?????? ???? ???????. * @return ???????? ??????. * @throws ParseException ????? ??????? ????????????? ??? null. */ public List<Tweet> getSearchedTweets(String str) throws ParseException { Object parsedString = parser.parse(str); JSONObject array = (JSONObject) parsedString; JSONArray jsonArray = (JSONArray) array.get("statuses"); return getTweets(jsonArray.toJSONString()); } public List<Person> getSearchedUsers(String str) throws ParseException { Object parsedString = parser.parse(str); JSONArray array = (JSONArray) parsedString; List<Person> result = new ArrayList<>(); for (JSONObject arrayValue : (Iterable<JSONObject>) array) { Person person = getPerson(arrayValue); result.add(person); } return result; } //------------------------------------------------------------------------------------------------------------------ public Person getPerson(String str) throws ParseException { Object parsedString = parser.parse(str); JSONObject data = (JSONObject) parsedString; return getPerson(data); } private Person getPerson(JSONObject person) { String name = (String) person.get("name"); String profileImage = (String) person.get("profile_image_url_https"); String location = (String) person.get("location"); String description = (String) person.get("description"); String screenName = (String) person.get("screen_name"); boolean friend = (boolean) person.get("following"); long ID = (long) person.get("id"); return new Person(name, screenName, profileImage, location, description, friend, ID); } //------------------------------------------------------------------------------------------------------------------ /** * ?????????? ???? ?????????? ?????. ?? ??????? ?? ????? ???????????? ????????? ??????, ???????? ????????? ????????? ????????. * * @param object JSONObject ???? ???????. * @return ?????????? ?????????????? ????. */ private String getCreationDate(JSONObject object) throws java.text.ParseException { String date = (String) object.get("created_at"); return formattedDate.format(incomingDate.parse(date)); } //------------------------------------------------------------------------------------------------------------------ /** * ?????????? ???????? ??????. * * @param object JSONObject ???? ???????. * @return ???????? ??????. */ @SuppressWarnings("unchecked") private String[] getTags(JSONObject object) { JSONObject entities = (JSONObject) object.get("entities"); JSONArray jsonHashTags = (JSONArray) entities.get("hashtags"); String[] tags = new String[jsonHashTags.size()]; int i = 0; for (JSONObject s : (Iterable<JSONObject>) jsonHashTags) { tags[i++] = (String) s.get("text"); } return tags; } //------------------------------------------------------------------------------------------------------------------ /** * ?????????? ???????? ???????? (??? ???????) ?? ??????????? ??????????. * * @param object JSONObject ???? ???????. * @return ???????? ???????? (??? ???????) */ @SuppressWarnings("unchecked") private String[] getMedia(JSONObject object) { JSONObject entities = (JSONObject) object.get("entities"); JSONArray jsonMedia = (JSONArray) entities.get("media"); if (jsonMedia != null) { String[] media = new String[jsonMedia.size()]; int i = 0; for (JSONObject s : (Iterable<JSONObject>) jsonMedia) { media[i++] = (String) s.get("media_url_https"); } return media; } return null; } //------------------------------------------------------------------------------------------------------------------ /** * ???????? ?? ??????? ???????????? ???? ????????????? ?????????????. * * @param str ??????? ?????? ???????? * @return ???????????? ???? ?????????????. * @throws ParseException */ public String getUserScreenName(String str) throws ParseException { Object parsedString = parser.parse(str); JSONObject object = (JSONObject) parsedString; return (String) object.get("screen_name"); } //------------------------------------------------------------------------------------------------------------------ /** * ???????? ???????? ?????? ??????????????. * * @param str ??????? ?????? ???????? * @return ???????? ?????? ?????????????. * @throws ParseException */ public List<Person> getFriends(String str) throws ParseException { List<Person> friends = new ArrayList<>(); Object parsedString = parser.parse(str); JSONObject object = (JSONObject) parsedString; JSONArray jsonArray = (JSONArray) object.get("users"); for (JSONObject arrayValue : (Iterable<JSONObject>) jsonArray) { Person person = getPerson(arrayValue); friends.add(person); } return friends; } //------------------------------------------------------------------------------------------------------------------ public boolean isFriends(String str) throws ParseException { Object parsedString = parser.parse(str); JSONObject jsonString = (JSONObject) parsedString; JSONObject relationship = (JSONObject) jsonString.get("relationship"); JSONObject targetFriend = (JSONObject) relationship.get("target"); return (boolean) targetFriend.get("followed_by"); } public List<Tweet> getMessages(String str) throws ParseException, java.text.ParseException { JSONArray jsonArray = (JSONArray) parser.parse(str); List<Tweet> result = new ArrayList<>(); for (JSONObject arrayValue : (Iterable<JSONObject>) jsonArray) { long messageId = (long) arrayValue.get("id"); String text = (String) arrayValue.get("text"); String time = getCreationDate(arrayValue); Person sender = getPerson((JSONObject) arrayValue.get("sender")); Tweet message = new Tweet(text, time, "", null, sender, null, messageId); result.add(message); } return result; } }