Advertisement
Guest User

Untitled

a guest
Mar 31st, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 147.48 KB | None | 0 0
  1. package ru.euphoriadev.vk.api;
  2.  
  3. import android.util.Log;
  4.  
  5. import org.json.JSONArray;
  6. import org.json.JSONException;
  7. import org.json.JSONObject;
  8.  
  9. import java.io.BufferedInputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.net.HttpURLConnection;
  13. import java.net.URL;
  14. import java.util.ArrayList;
  15. import java.util.Collection;
  16. import java.util.Locale;
  17. import java.util.zip.GZIPInputStream;
  18.  
  19. import ru.euphoriadev.vk.BuildConfig;
  20. import ru.euphoriadev.vk.api.model.AudioAlbum;
  21. import ru.euphoriadev.vk.api.model.BannArg;
  22. import ru.euphoriadev.vk.api.model.City;
  23. import ru.euphoriadev.vk.api.model.CommentList;
  24. import ru.euphoriadev.vk.api.model.Counters;
  25. import ru.euphoriadev.vk.api.model.Country;
  26. import ru.euphoriadev.vk.api.model.FriendsList;
  27. import ru.euphoriadev.vk.api.model.GroupBanItem;
  28. import ru.euphoriadev.vk.api.model.GroupTopic;
  29. import ru.euphoriadev.vk.api.model.Newsfeed;
  30. import ru.euphoriadev.vk.api.model.PhotoTag;
  31. import ru.euphoriadev.vk.api.model.SearchDialogItem;
  32. import ru.euphoriadev.vk.api.model.VKAlbum;
  33. import ru.euphoriadev.vk.api.model.VKAttachment;
  34. import ru.euphoriadev.vk.api.model.VKAudio;
  35. import ru.euphoriadev.vk.api.model.VKChat;
  36. import ru.euphoriadev.vk.api.model.VKComment;
  37. import ru.euphoriadev.vk.api.model.VKDocument;
  38. import ru.euphoriadev.vk.api.model.VKFullUser;
  39. import ru.euphoriadev.vk.api.model.VKGift;
  40. import ru.euphoriadev.vk.api.model.VKGroup;
  41. import ru.euphoriadev.vk.api.model.VKLink;
  42. import ru.euphoriadev.vk.api.model.VKLongPollServer;
  43. import ru.euphoriadev.vk.api.model.VKMessage;
  44. import ru.euphoriadev.vk.api.model.VKMessageAttachment;
  45. import ru.euphoriadev.vk.api.model.VKNote;
  46. import ru.euphoriadev.vk.api.model.VKNotifications;
  47. import ru.euphoriadev.vk.api.model.VKPhoto;
  48. import ru.euphoriadev.vk.api.model.VKPoll;
  49. import ru.euphoriadev.vk.api.model.VKResolveScreenName;
  50. import ru.euphoriadev.vk.api.model.VKStatus;
  51. import ru.euphoriadev.vk.api.model.VKUser;
  52. import ru.euphoriadev.vk.api.model.VKVideo;
  53. import ru.euphoriadev.vk.api.model.VKWallMessage;
  54. import ru.euphoriadev.vk.common.AppLoader;
  55. import ru.euphoriadev.vk.util.Account;
  56.  
  57. /**
  58.  * VK Api. Модифицированная мной версия от разработчиков Kate Mobile
  59.  * SDK ВКонтакте для основных мобильных платформ: iOS, Android и Windows Phone
  60.  * — сокращает время интеграции API ВКонтакте и предоставляет дополнительные преимущества
  61.  * <p/>
  62.  * Подробнее - http://vk.com/dev/main
  63.  * <p/>
  64.  * TODO: Некоторые методы были скрыти от пользователей
  65.  * Такие как gifts.send - отправка подарка
  66.  * Полный список можно получить здесь https://vkapi.zf-projects.ru/methods-list
  67.  */
  68. public class Api {
  69.     public static final String BASE_URL = "https://api.vk.com/method/";
  70.     public static final String API_VERSION = "5.14";
  71.     static final String TAG = "Kate.Api";
  72.     static final boolean DEBUG = BuildConfig.DEBUG;
  73.     private final static int MAX_TRIES = 3;
  74.     //TODO: it's not faster, even slower on slow devices. Maybe we should add an option to disable it. It's only good for paid internet connection.
  75.     static boolean enable_compression = true;
  76.     private static Api mSingleton;
  77.     String language = Locale.getDefault().getLanguage();
  78.     String access_token;
  79.     String api_id;
  80.     private Account mAccount;
  81.  
  82.     private Api(String access_token, String api_id) {
  83.         this.access_token = access_token;
  84.         this.api_id = api_id;
  85.     }
  86.  
  87.     private Api(Account account) {
  88.         this(account.access_token, Account.API_ID);
  89.         this.mAccount = account;
  90.     }
  91.  
  92.     public static Api init(String access_token, String api_id) {
  93.         if (mSingleton == null) {
  94.             mSingleton = new Api(access_token, api_id);
  95.         }
  96.         return mSingleton;
  97.     }
  98.  
  99.     public static synchronized Api init(Account account) {
  100.         if (mSingleton == null) {
  101.             mSingleton = new Api(account);
  102.         }
  103.         return mSingleton;
  104.     }
  105.  
  106.     public static Api get() {
  107.         return mSingleton;
  108.     }
  109.  
  110.     public static String sendRequestInternal(String url, String body, boolean is_post) throws IOException {
  111.         HttpURLConnection connection = null;
  112.         try {
  113.             connection = (HttpURLConnection) new URL(url).openConnection();
  114. //            connection.connect();
  115.             connection.setConnectTimeout(30000);
  116.             connection.setReadTimeout(30000);
  117.             connection.setUseCaches(false);
  118.             connection.setDoOutput(is_post);
  119.             connection.setDoInput(true);
  120.             connection.setRequestMethod(is_post ? "POST" : "GET");
  121.             if (enable_compression)
  122.                 connection.setRequestProperty("Accept-Encoding", "gzip");
  123.             if (is_post)
  124.                 connection.getOutputStream().write(body.getBytes("UTF-8"));
  125.             int code = connection.getResponseCode();
  126.             Log.i(TAG, "code=" + code);
  127.             //It may happen due to keep-alive problem http://stackoverflow.com/questions/1440957/httpurlconnection-getresponsecode-returns-1-on-second-invocation
  128.             if (code == -1)
  129.                 throw new WrongResponseCodeException("Network error");
  130.             //может стоит проверить на код 200
  131.             //on error can also read error stream from connection.
  132.             InputStream is = new BufferedInputStream(connection.getInputStream(), 8192);
  133.             String enc = connection.getHeaderField("Content-Encoding");
  134.             if (enc != null && enc.equalsIgnoreCase("gzip"))
  135.                 is = new GZIPInputStream(is);
  136.             return Utils.convertStreamToString(is);
  137.         } finally {
  138.             if (connection != null)
  139.                 connection.disconnect();
  140.         }
  141.     }
  142.  
  143.     public static String unescape(String text) {
  144.         if (text == null)
  145.             return null;
  146.         return text.replace("&amp;", "&").replace("&quot;", "\"").replace("<br>", "\n").replace("&gt;", ">").replace("&lt;", "<")
  147.                 .replace("<br/>", "\n").replace("&ndash;", "-").trim();
  148.         //Баг в API
  149.         //amp встречается в сообщении, br в Ответах тип comment_photo, gt lt на стене - баг API, ndash в статусе когда аудио транслируется
  150.         //quot в тексте сообщения из LongPoll - то есть в уведомлении
  151.     }
  152.  
  153.     public static String unescapeWithSmiles(String text) {
  154.         return unescape(text)
  155.                 //May be useful to someone
  156.                 //.replace("\uD83D\uDE0A", ":-)")
  157.                 //.replace("\uD83D\uDE03", ":D")
  158.                 //.replace("\uD83D\uDE09", ";-)")
  159.                 //.replace("\uD83D\uDE06", "xD")
  160.                 //.replace("\uD83D\uDE1C", ";P")
  161.                 //.replace("\uD83D\uDE0B", ":p")
  162.                 //.replace("\uD83D\uDE0D", "8)")
  163.                 //.replace("\uD83D\uDE0E", "B)")
  164.                 //
  165.                 //.replace("\ud83d\ude12", ":(")  //F0 9F 98 92
  166.                 //.replace("\ud83d\ude0f", ":]")  //F0 9F 98 8F
  167.                 //.replace("\ud83d\ude14", "3(")  //F0 9F 98 94
  168.                 //.replace("\ud83d\ude22", ":'(")  //F0 9F 98 A2
  169.                 //.replace("\ud83d\ude2d", ":_(")  //F0 9F 98 AD
  170.                 //.replace("\ud83d\ude29", ":((")  //F0 9F 98 A9
  171.                 //.replace("\ud83d\ude28", ":o")  //F0 9F 98 A8
  172.                 //.replace("\ud83d\ude10", ":|")  //F0 9F 98 90
  173.                 //
  174.                 //.replace("\ud83d\ude0c", "3)")  //F0 9F 98 8C
  175.                 //.replace("\ud83d\ude20", ">(")  //F0 9F 98 A0
  176.                 //.replace("\ud83d\ude21", ">((")  //F0 9F 98 A1
  177.                 //.replace("\ud83d\ude07", "O:)")  //F0 9F 98 87
  178.                 //.replace("\ud83d\ude30", ";o")  //F0 9F 98 B0
  179.                 //.replace("\ud83d\ude32", "8o")  //F0 9F 98 B2
  180.                 //.replace("\ud83d\ude33", "8|")  //F0 9F 98 B3
  181.                 //.replace("\ud83d\ude37", ":X")  //F0 9F 98 B7
  182.                 //
  183.                 //.replace("\ud83d\ude1a", ":*")  //F0 9F 98 9A
  184.                 //.replace("\ud83d\ude08", "}:)")  //F0 9F 98 88
  185.                 //.replace("\u2764", "<3")  //E2 9D A4
  186.                 //.replace("\ud83d\udc4d", ":like:")  //F0 9F 91 8D
  187.                 //.replace("\ud83d\udc4e", ":dislike:")  //F0 9F 91 8E
  188.                 //.replace("\u261d", ":up:")  //E2 98 9D
  189.                 //.replace("\u270c", ":v:")  //E2 9C 8C
  190.                 //.replace("\ud83d\udc4c", ":ok:")  //F0 9F 91 8C
  191.                 ;
  192.     }
  193.  
  194.     public int getUserId() {
  195.         if (mAccount == null) {
  196.             mAccount = new Account(AppLoader.appContext);
  197.             mAccount.restore();
  198.         }
  199.         return (int) mAccount.user_id;
  200.     }
  201.  
  202.     public Account getAccount() {
  203.         return mAccount;
  204.     }
  205.  
  206.     public void setAccount(Account account) {
  207.         this.mAccount = account;
  208.     }
  209.  
  210.     public void setAccessToken(String access_token) {
  211.         this.access_token = access_token;
  212.     }
  213.  
  214.     public void setApiId(String api_id) {
  215.         this.api_id = api_id;
  216.     }
  217.  
  218.     /**
  219.      * utils methods**
  220.      */
  221.     private void checkError(JSONObject root, String url) throws JSONException, KException {
  222.         if (!root.isNull("error")) {
  223.             JSONObject error = root.getJSONObject("error");
  224.             int code = error.getInt("error_code");
  225.             String message = error.getString("error_msg");
  226.             KException e = new KException(code, message, url);
  227.             if (code == 14) {
  228.                 e.captcha_img = error.optString("captcha_img");
  229.                 e.captcha_sid = error.optString("captcha_sid");
  230.             }
  231.             if (code == 17)
  232.                 e.redirect_uri = error.optString("redirect_uri");
  233.             throw e;
  234.         }
  235.         if (!root.isNull("execute_errors")) {
  236.             JSONArray errors = root.getJSONArray("execute_errors");
  237.             if (errors.length() == 0)
  238.                 return;
  239.             // only first error is processed if there are multiple
  240.             JSONObject error = errors.getJSONObject(0);
  241.             int code = error.getInt("error_code");
  242.             String message = error.getString("error_msg");
  243.             KException e = new KException(code, message, url);
  244.             if (code == 14) {
  245.                 e.captcha_img = error.optString("captcha_img");
  246.                 e.captcha_sid = error.optString("captcha_sid");
  247.             }
  248.             if (code == 17)
  249.                 e.redirect_uri = error.optString("redirect_uri");
  250.             throw e;
  251.         }
  252.     }
  253.  
  254.     public JSONObject sendRequest(VKParams params) throws JSONException, IOException, KException {
  255.         return sendRequest(params, false);
  256.     }
  257.  
  258.     private JSONObject sendRequest(VKParams params, boolean is_post) throws IOException, JSONException, KException {
  259.         String url = getSignedUrl(params, is_post);
  260.         String body = "";
  261.         if (is_post) body = params.getParamsString();
  262.  
  263.         Log.i(TAG, "url=" + url);
  264.         if (body.length() != 0)
  265.             Log.i(TAG, "body = ".concat(body));
  266.         String response = "";
  267.         for (int i = 1; i <= MAX_TRIES; ++i) {
  268.             try {
  269.                 if (i != 1)
  270.                     Log.i(TAG, "try " + i);
  271.                 response = sendRequestInternal(url, body, is_post);
  272.                 break;
  273.             } catch (IOException ex) {
  274.                 processNetworkException(i, ex);
  275.             }
  276.         }
  277.         if (DEBUG) Log.i(TAG, "response = ".concat(response));
  278.  
  279.         JSONObject root = new JSONObject(response);
  280.         checkError(root, url);
  281.         return root;
  282.     }
  283.  
  284.     private void processNetworkException(int i, IOException ex) throws IOException {
  285.         ex.printStackTrace();
  286.         if (i == MAX_TRIES)
  287.             throw ex;
  288.     }
  289.  
  290.     private String getSignedUrl(VKParams params, boolean is_post) {
  291.         params.put("access_token", access_token);
  292.         if (!params.contains("v"))
  293.             params.put("v", API_VERSION);
  294.         if (!params.contains("lang"))
  295.             params.put("lang", language);
  296.  
  297.         String args = "";
  298.         if (!is_post)
  299.             args = params.getParamsString();
  300.  
  301.         return BASE_URL + params.method_name + "?" + args;
  302.     }
  303.  
  304.     /**
  305.      * API methods **
  306.      */
  307.     //http://vk.com/dev/database.getCities
  308.     public ArrayList<City> getCitiesByCountry(int country, String q) throws IOException, JSONException, KException {
  309.         VKParams params = new VKParams("database.getCities");
  310.         params.put("country_id", country);
  311.         params.put("q", q);
  312.         JSONObject root = sendRequest(params);
  313.         JSONObject response = root.optJSONObject("response");
  314.         JSONArray array = response.optJSONArray("items");
  315.         ArrayList<City> cities = new ArrayList<City>();
  316.         if (array != null) {
  317.             for (int i = 0; i < array.length(); i++) {
  318.                 JSONObject o = (JSONObject) array.get(i);
  319.                 City c = City.parse(o);
  320.                 cities.add(c);
  321.             }
  322.         }
  323.         return cities;
  324.     }
  325.  
  326.     //http://vk.com/dev/database.getCitiesById
  327.     public ArrayList<City> getCitiesById(Collection<Long> cids) throws IOException, JSONException, KException {
  328.         if (cids == null || cids.size() == 0)
  329.             return null;
  330.         VKParams params = new VKParams("database.getCitiesById");
  331.         params.put("city_ids", arrayToString(cids));
  332.         JSONObject root = sendRequest(params);
  333.         JSONArray array = root.optJSONArray("response");
  334.         ArrayList<City> cities = new ArrayList<City>();
  335.         if (array != null) {
  336.             for (int i = 0; i < array.length(); i++) {
  337.                 JSONObject o = (JSONObject) array.get(i);
  338.                 City c = City.parse(o);
  339.                 cities.add(c);
  340.             }
  341.         }
  342.         return cities;
  343.     }
  344.  
  345.     <T> String arrayToString(Collection<T> items) {
  346.         if (items == null || items.isEmpty()) {
  347.             return null;
  348.         }
  349.  
  350.         StringBuilder buffer = new StringBuilder(32);
  351.         for (Object item : items) {
  352.             buffer.append(item);
  353.             buffer.append(',');
  354.         }
  355.         return buffer.toString();
  356.     }
  357.  
  358.     //http://vk.com/dev/database.getCountries
  359.     public ArrayList<Country> getCountries(Integer need_full, String code) throws IOException, JSONException, KException {
  360.         VKParams params = new VKParams("database.getCountries");
  361.         params.put("need_all", need_full);
  362.         params.put("code", code);
  363.         if (need_full != null && need_full == 1)
  364.             params.put("count", 1000);
  365.         JSONObject root = sendRequest(params);
  366.         JSONObject response = root.optJSONObject("response");
  367.         JSONArray array = response.optJSONArray("items");
  368.         ArrayList<Country> countries = new ArrayList<Country>();
  369.         int category_count = array.length();
  370.         for (int i = 0; i < category_count; i++) {
  371.             JSONObject o = (JSONObject) array.get(i);
  372.             Country c = Country.parse(o);
  373.             countries.add(c);
  374.         }
  375.         return countries;
  376.     }
  377.  
  378.     //http://vk.com/dev/database.getCountriesById
  379.     public ArrayList<Country> getCountriesById(Collection<Long> cids) throws IOException, JSONException, KException {
  380.         if (cids == null || cids.size() == 0)
  381.             return null;
  382.         VKParams params = new VKParams("database.getCountriesById");
  383.         String str_cids = arrayToString(cids);
  384.         params.put("country_ids", str_cids);
  385.         JSONObject root = sendRequest(params);
  386.         JSONArray array = root.getJSONArray("response");
  387.         ArrayList<Country> countries = new ArrayList<Country>();
  388.         int category_count = array.length();
  389.         for (int i = 0; i < category_count; i++) {
  390.             JSONObject o = (JSONObject) array.get(i);
  391.             Country c = Country.parse(o);
  392.             countries.add(c);
  393.         }
  394.         return countries;
  395.     }
  396.  
  397.     //*** methods for users ***//
  398.     //http://vk.com/dev/users.get
  399.     public ArrayList<VKFullUser> getProfilesFull(Collection<Integer> uids, Collection<String> domains, String fields, String name_case, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  400.         if (uids == null && domains == null)
  401.             return null;
  402.         if ((uids != null && uids.size() == 0) || (domains != null && domains.size() == 0))
  403.             return null;
  404.         VKParams params = new VKParams("users.get");
  405.         if (uids != null && uids.size() > 0)
  406.             params.put("user_ids", arrayToString(uids));
  407.         if (domains != null && domains.size() > 0)
  408.             params.put("user_ids", arrayToString(domains));
  409.         params.put("fields", fields);
  410.         params.put("name_case", name_case);
  411.         addCaptchaParams(captcha_key, captcha_sid, params);
  412.         JSONObject root = sendRequest(params);
  413.         JSONArray array = root.optJSONArray("response");
  414.         return VKFullUser.parseUsers(array);
  415.     }
  416.  
  417.     public VKFullUser getProfileFull(long user_id, String fields) throws JSONException, IOException, KException {
  418.         VKParams params = new VKParams("users.get");
  419.         params.put("user_id", user_id);
  420.         params.put("fields", fields);
  421.         addCaptchaParams(null, null, params);
  422.  
  423.         JSONObject root = sendRequest(params);
  424.         JSONArray response = root.optJSONArray("response");
  425.         return VKFullUser.parse(response.optJSONObject(0));
  426.     }
  427.  
  428.     public ArrayList<VKUser> getProfiles(Collection<Integer> uids, Collection<String> domains, String name_case, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  429.         if (uids == null && domains == null)
  430.             return null;
  431.         if ((uids != null && uids.size() == 0) || (domains != null && domains.size() == 0))
  432.             return null;
  433.         VKParams params = new VKParams("users.get");
  434.         if (uids != null && uids.size() > 0)
  435.             params.put("user_ids", arrayToString(uids));
  436.         if (domains != null && domains.size() > 0)
  437.             params.put("user_ids", arrayToString(domains));
  438.         params.put("fields", VKUser.FIELDS_DEFAULT);
  439.         params.put("name_case", name_case);
  440.         addCaptchaParams(captcha_key, captcha_sid, params);
  441.         JSONObject root = sendRequest(params);
  442.         JSONArray array = root.optJSONArray("response");
  443.         return VKUser.parseUsers(array);
  444.     }
  445.  
  446.     public VKUser getProfile(int user_id) {
  447.         try {
  448.             ArrayList<Integer> uids = new ArrayList<>();
  449.             uids.add(user_id);
  450.  
  451.             return getProfiles(uids, null, null, null, null).get(0);
  452.         } catch (Exception e) {
  453.             e.printStackTrace();
  454.         }
  455.         return null;
  456.     }
  457.  
  458.     /**
  459.      * methods for friends **
  460.      */
  461.     //http://vk.com/dev/friends.get
  462.     public ArrayList<VKFullUser> getFriendsFull(Long user_id, String fields, String order, Integer lid, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  463.         VKParams params = new VKParams("friends.get");
  464.         params.put("fields", fields);
  465.         params.put("user_id", user_id);
  466.         params.put("list_id", lid);
  467.         params.put("order", order);
  468.  
  469.         /** UPD: У меня все работает */
  470.         // сортировка по популярности не даёт запросить друзей из списка
  471.         //     if(lid==null)
  472.         //         params.put("order","hints");
  473.  
  474.         addCaptchaParams(captcha_key, captcha_sid, params);
  475.         JSONObject root = sendRequest(params);
  476.         JSONObject response = root.optJSONObject("response");
  477.         JSONArray array = response.optJSONArray("items");
  478.         ArrayList<VKFullUser> users = new ArrayList<VKFullUser>();
  479.         //if there are no friends "response" will not be array
  480.         if (array == null)
  481.             return users;
  482.         int category_count = array.length();
  483.         for (int i = 0; i < category_count; ++i) {
  484.             JSONObject o = (JSONObject) array.get(i);
  485.             VKFullUser u = VKFullUser.parse(o);
  486.             users.add(u);
  487.         }
  488.         return users;
  489.     }
  490.  
  491.     //http://vk.com/dev/friends.get
  492.     public ArrayList<VKUser> getFriends(long user_id, String order, Integer count, Integer lid, String captcha_key, String captcha_sid) throws JSONException, IOException, KException {
  493.         VKParams params = new VKParams("friends.get");
  494.         params.put("user_id", user_id);
  495.         params.put("fields", VKUser.FIELDS_DEFAULT);
  496.         params.put("list_id", lid);
  497.         params.put("count", count);
  498.         params.put("order", order);
  499.  
  500.         addCaptchaParams(captcha_key, captcha_sid, params);
  501.         JSONObject root = sendRequest(params);
  502.         JSONObject response = root.optJSONObject("response");
  503.         JSONArray array = response.optJSONArray("items");
  504.  
  505.         return VKUser.parseUsers(array);
  506.     }
  507.  
  508.     //http://vk.com/dev/friends.getOnline
  509.     public ArrayList<Long> getOnlineFriends(Long uid) throws IOException, JSONException, KException {
  510.         VKParams params = new VKParams("friends.getOnline");
  511.         params.put("user_id", uid);
  512.         JSONObject root = sendRequest(params);
  513.         JSONArray array = root.optJSONArray("response");
  514.         ArrayList<Long> users = new ArrayList<Long>();
  515.         if (array != null) {
  516.             int category_count = array.length();
  517.             for (int i = 0; i < category_count; ++i) {
  518.                 Long id = array.optLong(i, -1);
  519.                 if (id != -1) users.add(id);
  520.             }
  521.         }
  522.         return users;
  523.     }
  524.  
  525.     //http://vk.com/dev/friends.getAppUsers
  526.     public ArrayList<Long> getAppUsersFriends() throws JSONException, IOException, KException {
  527.         VKParams params = new VKParams("friends.getAppUsers");
  528.         JSONObject root = sendRequest(params);
  529.         JSONArray array = root.optJSONArray("response");
  530.  
  531.         ArrayList<Long> users = new ArrayList<>();
  532.         if (array != null)
  533.             for (int i = 0; i < array.length(); i++) {
  534.                 users.add(array.optLong(i, -1));
  535.             }
  536.         return users;
  537.     }
  538.  
  539.     //http://vk.com/dev/likes.getList
  540.     public ArrayList<Long> getLikeUsers(String item_type, long item_id, long owner_id, String filter) throws IOException, JSONException, KException {
  541.         VKParams params = new VKParams("likes.getList");
  542.         params.put("type", item_type);
  543.         params.put("owner_id", owner_id);
  544.         params.put("item_id", item_id);
  545.         params.put("filter", filter); //likes - default, copies
  546.         JSONObject root = sendRequest(params);
  547.         JSONObject response = root.getJSONObject("response");
  548.         JSONArray array = response.optJSONArray("items");
  549.         ArrayList<Long> users = new ArrayList<Long>();
  550.         if (array != null) {
  551.             int category_count = array.length();
  552.             for (int i = 0; i < category_count; ++i) {
  553.                 Long id = array.optLong(i, -1);
  554.                 if (id != -1)
  555.                     users.add(id);
  556.             }
  557.         }
  558.         return users;
  559.     }
  560.  
  561.     //http://vk.com/dev/friends.getMutual
  562.     public ArrayList<Long> getMutual(Long target_uid, Long source_uid) throws IOException, JSONException, KException {
  563.         VKParams params = new VKParams("friends.getMutual");
  564.         params.put("target_uid", target_uid);
  565.         params.put("source_uid", source_uid);
  566.         JSONObject root = sendRequest(params);
  567.         JSONArray array = root.optJSONArray("response");
  568.         ArrayList<Long> users = new ArrayList<Long>();
  569.         if (array != null) {
  570.             int category_count = array.length();
  571.             for (int i = 0; i < category_count; ++i) {
  572.                 Long id = array.optLong(i, -1);
  573.                 if (id != -1)
  574.                     users.add(id);
  575.             }
  576.         }
  577.         return users;
  578.     }
  579.  
  580.     /**
  581.      * methods for photos **
  582.      */
  583.     //http://vk.com/dev/photos.getAlbums
  584.     public ArrayList<VKAlbum> getAlbums(Long oid, Collection<Long> aids, Integer need_system, Integer need_covers, Integer photo_sizes) throws IOException, JSONException, KException {
  585.         VKParams params = new VKParams("photos.getAlbums");
  586.         params.put("owner_id", oid);
  587.         params.put("album_ids", arrayToString(aids));
  588.         params.put("need_system", need_system);
  589.         params.put("need_covers", need_covers);
  590.         params.put("photo_sizes", photo_sizes);
  591.         JSONObject root = sendRequest(params);
  592.         ArrayList<VKAlbum> albums = new ArrayList<VKAlbum>();
  593.         JSONObject response = root.optJSONObject("response");
  594.         JSONArray array = response.optJSONArray("items");
  595.         if (array == null)
  596.             return albums;
  597.         int category_count = array.length();
  598.         for (int i = 0; i < category_count; ++i) {
  599.             JSONObject o = (JSONObject) array.get(i);
  600.             VKAlbum a = VKAlbum.parse(o);
  601.             if (a.title.equals("DELETED"))
  602.                 continue;
  603.             albums.add(a);
  604.         }
  605.         return albums;
  606.     }
  607.  
  608.     //http://vk.com/dev/photos.get
  609.     public ArrayList<VKPhoto> getPhotos(Long uid, Long aid, Integer offset, Integer count, boolean rev) throws IOException, JSONException, KException {
  610.         VKParams params = new VKParams("photos.get");
  611.         params.put("owner_id", uid);
  612.         params.put("album_id", aid);
  613.         params.put("extended", "1");
  614.         params.put("offset", offset);
  615.         params.put("limit", count);
  616.         if (rev)
  617.             params.put("rev", 1);
  618.         JSONObject root = sendRequest(params);
  619.         JSONObject response = root.optJSONObject("response");
  620.         JSONArray array = response.optJSONArray("items");
  621.         if (array == null)
  622.             return new ArrayList<VKPhoto>();
  623.         ArrayList<VKPhoto> photos = parsePhotos(array);
  624.         return photos;
  625.     }
  626.  
  627.     //http://vk.com/dev/photos.getUserPhotos
  628.     public ArrayList<VKPhoto> getUserPhotos(Long uid, Integer offset, Integer count) throws IOException, JSONException, KException {
  629.         VKParams params = new VKParams("photos.getUserPhotos");
  630.         params.put("user_id", uid);
  631.         params.put("sort", "0");
  632.         params.put("count", count);
  633.         params.put("offset", offset);
  634.         params.put("extended", 1);
  635.         JSONObject root = sendRequest(params);
  636.         JSONObject response = root.optJSONObject("response");
  637.         JSONArray array = response.optJSONArray("items");
  638.         if (array == null)
  639.             return new ArrayList<VKPhoto>();
  640.         ArrayList<VKPhoto> photos = parsePhotos(array);
  641.         return photos;
  642.     }
  643.  
  644.     //http://vk.com/dev/photos.getAll
  645.     public ArrayList<VKPhoto> getAllPhotos(Long owner_id, Integer offset, Integer count, boolean extended) throws IOException, JSONException, KException {
  646.         VKParams params = new VKParams("photos.getAll");
  647.         params.put("owner_id", owner_id);
  648.         params.put("offset", offset);
  649.         params.put("count", count);
  650.         params.put("extended", extended ? 1 : 0);
  651.         JSONObject root = sendRequest(params);
  652.         JSONObject response = root.optJSONObject("response");
  653.         JSONArray array = response.optJSONArray("items");
  654.         if (array == null)
  655.             return new ArrayList<VKPhoto>();
  656.         ArrayList<VKPhoto> photos = parsePhotos(array);
  657.         return photos;
  658.     }
  659.  
  660.     //http://vk.com/dev/photos.getComments
  661.     public CommentList getPhotoComments(Long pid, Long owner_id, int offset, int count) throws IOException, JSONException, KException {
  662.         VKParams params = new VKParams("photos.getComments");
  663.         params.put("photo_id", pid);
  664.         params.put("owner_id", owner_id);
  665.         if (count > 0)
  666.             params.put("count", count);
  667.         if (offset > 0)
  668.             params.put("offset", offset);
  669.         params.put("sort", "asc");
  670.         params.put("need_likes", "1");
  671.         JSONObject root = sendRequest(params);
  672.         CommentList commnets = new CommentList();
  673.         JSONObject response = root.optJSONObject("response");
  674.         JSONArray array = response.optJSONArray("items");
  675.         commnets.count = response.getInt("count");
  676.         int category_count = array.length();
  677.         for (int i = 0; i < category_count; ++i) {
  678.             JSONObject o = (JSONObject) array.get(i);
  679.             VKComment comment = VKComment.parse(o);
  680.             commnets.comments.add(comment);
  681.         }
  682.         return commnets;
  683.     }
  684.  
  685.     //http://vk.com/dev/notes.getComments
  686.     public CommentList getNoteComments(Long nid, Long owner_id, int offset, int count) throws IOException, JSONException, KException {
  687.         VKParams params = new VKParams("notes.getComments");
  688.         params.put("note_id", nid);
  689.         params.put("owner_id", owner_id);
  690.         if (count > 0)
  691.             params.put("count", count);
  692.         if (offset > 0)
  693.             params.put("offset", offset);
  694.         JSONObject root = sendRequest(params);
  695.         JSONObject response = root.optJSONObject("response");
  696.         JSONArray array = response.optJSONArray("items");
  697.         CommentList commnets = new CommentList();
  698.         commnets.count = response.getInt("count");
  699.         int category_count = array.length();
  700.         for (int i = 0; i < category_count; ++i) {
  701.             JSONObject o = (JSONObject) array.get(i);
  702.             VKComment comment = VKComment.parseNoteComment(o);
  703.             commnets.comments.add(comment);
  704.         }
  705.         return commnets;
  706.     }
  707.  
  708.     //http://vk.com/dev/video.getComments
  709.     public CommentList getVideoComments(long video_id, Long owner_id, int offset, int count) throws IOException, JSONException, KException {
  710.         VKParams params = new VKParams("video.getComments");
  711.         params.put("video_id", video_id);
  712.         params.put("owner_id", owner_id);
  713.         if (count > 0)
  714.             params.put("count", count);
  715.         if (offset > 0)
  716.             params.put("offset", offset);
  717.         params.put("need_likes", "1");
  718.         JSONObject root = sendRequest(params);
  719.         CommentList commnets = new CommentList();
  720.         JSONObject response = root.optJSONObject("response");
  721.         JSONArray array = response.optJSONArray("items");
  722.         commnets.count = response.getInt("count");
  723.         int category_count = array.length();
  724.         for (int i = 0; i < category_count; ++i) {
  725.             JSONObject o = (JSONObject) array.get(i);
  726.             VKComment comment = VKComment.parse(o);
  727.             commnets.comments.add(comment);
  728.         }
  729.         return commnets;
  730.     }
  731.  
  732.     //http://vk.com/dev/photos.getAllComments
  733.     public ArrayList<VKComment> getAllPhotoComments(Long owner_id, Long album_id, int offset, int count) throws IOException, JSONException, KException {
  734.         VKParams params = new VKParams("photos.getAllComments");
  735.         params.put("owner_id", owner_id);
  736.         params.put("album_id", album_id);
  737.         if (count > 0)
  738.             params.put("count", count);
  739.         if (offset > 0)
  740.             params.put("offset", offset);
  741.         params.put("need_likes", "1");
  742.         ArrayList<VKComment> commnets = new ArrayList<VKComment>();
  743.         @SuppressWarnings("unused")
  744.         JSONObject root = sendRequest(params);
  745.         //здесь ещё приходит pid - photo_id
  746.         //вынести парсящий код чтобы не было дублирования
  747.         //JSONArray array = root.getJSONArray("response");
  748.         //int category_count = array.length();
  749.         //for(int i = 0; i<category_count; ++i) {
  750.         //    JSONObject o = (JSONObject)array.get(i);
  751.         //    Comment comment = new Comment();
  752.         //    comment.cid = Long.parseLong(o.getString("comment_id"));
  753.         //    comment.from_id = Long.parseLong(o.getString("from_id"));
  754.         //    comment.date = Long.parseLong(o.getString("date"));
  755.         //    comment.message = unescape(o.getString("message"));
  756.         //    commnets.add(comment);
  757.         //}
  758.         return commnets;
  759.     }
  760.  
  761.     //http://vk.com/dev/photos.createComment
  762.     public long createPhotoComment(Long pid, Long owner_id, String message, Long reply_to_cid, Collection<String> attachments, boolean from_group, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  763.         VKParams params = new VKParams("photos.createComment");
  764.         params.put("photo_id", pid);
  765.         params.put("owner_id", owner_id);
  766.         addCaptchaParams(captcha_key, captcha_sid, params);
  767.         params.put("message", message);
  768.         params.put("reply_to_comment", reply_to_cid);
  769.         params.put("attachments", arrayToString(attachments));
  770.         if (from_group)
  771.             params.put("from_group", "1");
  772.         JSONObject root = sendRequest(params, true);
  773.         long message_id = root.optLong("response");
  774.         return message_id;
  775.     }
  776.  
  777.     //http://vk.com/dev/photos.editComment
  778.     public boolean editPhotoComment(long cid, long pid, Long owner_id, String message, Collection<String> attachments, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  779.         VKParams params = new VKParams("photos.editComment");
  780.         params.put("comment_id", cid);
  781.         params.put("photo_id", pid);//probably not reqiured - missing in docs
  782.         params.put("owner_id", owner_id);
  783.         addCaptchaParams(captcha_key, captcha_sid, params);
  784.         params.put("message", message);
  785.         params.put("attachments", arrayToString(attachments));
  786.         JSONObject root = sendRequest(params, true);
  787.         int response = root.optInt("response");
  788.         return response == 1;
  789.     }
  790.  
  791.     //http://vk.com/dev/notes.createComment
  792.     public long createNoteComment(Long nid, Long owner_id, String message, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  793.         VKParams params = new VKParams("notes.createComment");
  794.         params.put("note_id", nid);
  795.         params.put("owner_id", owner_id);
  796.         addCaptchaParams(captcha_key, captcha_sid, params);
  797.         params.put("message", message);
  798.         //if (reply_to != null && !reply_to.equals(""))
  799.         //    params.put("reply_to", reply_to);
  800.         JSONObject root = sendRequest(params, true);
  801.         long message_id = root.optLong("response");
  802.         return message_id;
  803.     }
  804.  
  805.     //http://vk.com/dev/notes.editComment
  806.     public boolean editNoteComment(long cid, Long owner_id, String message, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  807.         VKParams params = new VKParams("notes.editComment");
  808.         params.put("comment_id", cid);
  809.         params.put("owner_id", owner_id);
  810.         addCaptchaParams(captcha_key, captcha_sid, params);
  811.         params.put("message", message);
  812.         JSONObject root = sendRequest(params, true);
  813.         int response = root.optInt("response");
  814.         return response == 1;
  815.     }
  816.  
  817.     //http://vk.com/dev/video.createComment
  818.     public long createVideoComment(Long video_id, Long owner_id, String message, Collection<String> attachments, boolean from_group, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  819.         VKParams params = new VKParams("video.createComment");
  820.         params.put("video_id", video_id);
  821.         params.put("owner_id", owner_id);
  822.         addCaptchaParams(captcha_key, captcha_sid, params);
  823.         params.put("message", message);
  824.         params.put("attachments", arrayToString(attachments));
  825.         if (from_group)
  826.             params.put("from_group", "1");
  827.         JSONObject root = sendRequest(params, true);
  828.         long message_id = root.optLong("response");
  829.         return message_id;
  830.     }
  831.  
  832.     //http://vk.com/dev/video.editComment
  833.     public boolean editVideoComment(long cid, Long owner_id, String message, Collection<String> attachments, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  834.         VKParams params = new VKParams("video.editComment");
  835.         params.put("comment_id", cid);
  836.         params.put("owner_id", owner_id);
  837.         addCaptchaParams(captcha_key, captcha_sid, params);
  838.         params.put("message", message);
  839.         params.put("attachments", arrayToString(attachments));
  840.         JSONObject root = sendRequest(params, true);
  841.         int response = root.optInt("response");
  842.         return response == 1;
  843.     }
  844.  
  845.     private void addCaptchaParams(String captcha_key, String captcha_sid, VKParams params) {
  846.         params.put("captcha_sid", captcha_sid);
  847.         params.put("captcha_key", captcha_key);
  848.     }
  849.  
  850.     /**
  851.      * methods for messages
  852.      *
  853.      * @throws KException **
  854.      */
  855.     //http://vk.com/dev/messages.get
  856.     public ArrayList<VKMessage> getMessages(long time_offset, int filters, boolean is_out, int count) throws IOException, JSONException, KException {
  857.         VKParams params = new VKParams("messages.get");
  858.         if (is_out)
  859.             params.put("out", "1");
  860.         if (time_offset != 0)
  861.             params.put("time_offset", time_offset);
  862.         if (count != 0)
  863.             params.put("count", count);
  864.         if (filters != 0)
  865.             params.put("filters", filters);
  866.  
  867.         params.put("preview_length", "0");
  868.         JSONObject root = sendRequest(params);
  869.         JSONObject response = root.optJSONObject("response");
  870.         JSONArray array = response.optJSONArray("items");
  871.         ArrayList<VKMessage> messages = parseMessages(array);
  872.         return messages;
  873.     }
  874.  
  875.     //http://vk.com/dev/messages.getHistory
  876.     public ArrayList<VKMessage> getMessagesHistory(long uid, long chat_id, long offset, int count, boolean rev) throws IOException, JSONException, KException {
  877.         VKParams params = new VKParams("messages.getHistory");
  878.         if (chat_id <= 0)
  879.             params.put("user_id", uid);
  880.         else
  881.             params.put("chat_id", chat_id);
  882.         params.put("offset", offset);
  883.         if (count != 0)
  884.             params.put("count", count);
  885.         params.put("rev", rev);
  886.         JSONObject root = sendRequest(params);
  887.         JSONObject response = root.optJSONObject("response");
  888.         JSONArray array = response.optJSONArray("items");
  889.         return VKMessage.parseArray(array);
  890.     }
  891.  
  892.     //http://vk.com/dev/messages.getDialogs
  893.     public ArrayList<VKMessage> getMessagesDialogs(long offset, int count, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  894.         VKParams params = new VKParams("messages.getDialogs");
  895.         if (offset != 0)
  896.             params.put("offset", offset);
  897.         if (count != 0)
  898.             params.put("count", count);
  899.         params.put("preview_length", "50");
  900.         addCaptchaParams(captcha_key, captcha_sid, params);
  901.         JSONObject root = sendRequest(params);
  902.         JSONObject response = root.optJSONObject("response");
  903.         JSONArray array = response.optJSONArray("items");
  904.         return parseMessages(array);
  905.     }
  906.  
  907.     // http://vk.com/dev/messages.getHistoryAttachments
  908.     public ArrayList<VKMessageAttachment> getHistoryAttachments(int peer_id, String media_type, Integer offset, Integer count, Boolean photo_sizes) throws IOException, JSONException, KException {
  909.         VKParams params = new VKParams("messages.getHistoryAttachments");
  910.         params.put("peer_id", peer_id);
  911.         params.put("media_type", media_type);
  912.         params.put("offset", offset);
  913.         params.put("count", count);
  914.         params.put("count", count);
  915.         params.put("photo_sizes", photo_sizes);
  916.  
  917.         JSONObject request = sendRequest(params);
  918.         JSONObject response = request.optJSONObject("response");
  919.         return VKMessageAttachment.parseArray(response.optJSONArray("items"), media_type, response.optInt("next_from"));
  920.     }
  921.  
  922.  
  923.     //http://vk.com/dev/messages.getLongPollServer
  924.     public VKLongPollServer getLongPollServer(String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  925.         VKParams params = new VKParams("messages.getLongPollServer");
  926.         params.put("need_pts", true);
  927.         addCaptchaParams(captcha_key, captcha_sid, params);
  928.         JSONObject root = sendRequest(params);
  929.         JSONObject response = root.getJSONObject("response");
  930.  
  931.         return VKLongPollServer.parse(response);
  932.     }
  933.  
  934.     // https://vk.com/dev/messages.getLongPollHistory
  935.     public ArrayList<VKMessage> getLongPollHistory(long ts, Long pts, long preview_length, boolean onlines, Long events_limit, Long msgs_limit, Long max_msg_id) throws JSONException, IOException, KException {
  936.         VKParams params = new VKParams("messages.getLongPollHistory");
  937.         params.put("ts", ts);
  938.         params.put("pts", pts);
  939.         params.put("preview_length", preview_length);
  940.         params.put("onlines", onlines);
  941.         params.put("events_limit", events_limit);
  942.         params.put("msgs_limit", msgs_limit);
  943.         params.put("max_msg_id", max_msg_id);
  944.  
  945.         JSONObject root = sendRequest(params);
  946.         JSONObject response = root.optJSONObject("response");
  947.         JSONObject messages = response.optJSONObject("messages");
  948.  
  949.         return VKMessage.parseArray(messages.optJSONArray("items"));
  950.     }
  951.  
  952.     //http://vk.com/dev/messages.setActivity
  953.     public Integer setMessageActivity(Integer uid, Integer chat_id, boolean typing) throws IOException, JSONException, KException {
  954.         VKParams params = new VKParams("messages.setActivity");
  955.         params.put("user_id", uid);
  956.         params.put("chat_id", chat_id);
  957.         if (typing)
  958.             params.put("type", "typing");
  959.         JSONObject root = sendRequest(params);
  960.         return root.optInt("response");
  961.     }
  962.  
  963.     private ArrayList<VKMessage> parseMessages(JSONArray array) throws JSONException {
  964. //        ArrayList<VKMessage> messages = new ArrayList<VKMessage>();
  965. //        if (array != null) {
  966. //            int category_count = array.length();
  967. //            for (int i = 0; i < category_count; ++i) {
  968. //                JSONObject o = array.getJSONObject(i);
  969. ////                VKMessage m = VKMessage.parseArray(o, from_history, history_uid, from_chat, me);
  970. //                VKMessage m = VKMessage.parseArray(o);
  971. //                messages.add(m);
  972. //            }
  973. //        }
  974.         return VKMessage.parseArray(array);
  975.     }
  976.  
  977.     //http://vk.com/dev/messages.send
  978.     public Integer sendMessage(int uid, long chat_id, String message, String title, String type, Collection<String> attachments, ArrayList<Long> forward_messages, String lat, String lon, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  979.         VKParams params = new VKParams("messages.send");
  980.         if (chat_id <= 0)
  981.             params.put("user_id", uid);
  982.         else
  983.             params.put("chat_id", chat_id);
  984.         params.put("message", message);
  985.         params.put("title", title);
  986.         params.put("type", type);
  987.         params.put("attachment", arrayToString(attachments));
  988.         params.put("forward_messages", arrayToString(forward_messages));
  989.         params.put("lat", lat);
  990.         params.put("long", lon);
  991.         addCaptchaParams(captcha_key, captcha_sid, params);
  992.         JSONObject root = sendRequest(params, true);
  993.         return root.optInt("response");
  994.     }
  995.  
  996.     //http://vk.com/dev/messages.markAsNew
  997.     //http://vk.com/dev/messages.markAsRead
  998.     @Deprecated
  999.     public String markAsNewOrAsRead(ArrayList<Long> mids, boolean as_read) throws IOException, JSONException, KException {
  1000.         if (mids == null || mids.size() == 0)
  1001.             return null;
  1002.         VKParams params;
  1003.         if (as_read)
  1004.             params = new VKParams("messages.markAsRead");
  1005.         else
  1006.             params = new VKParams("messages.markAsNew");
  1007.         params.put("message_ids", arrayToString(mids));
  1008.         JSONObject root = sendRequest(params);
  1009.         Object response_code = root.opt("response");
  1010.         if (response_code != null)
  1011.             return String.valueOf(response_code);
  1012.         return null;
  1013.     }
  1014.  
  1015.     //http://vk.com/dev/messages.markAsRead
  1016.     public Long markAsRead(ArrayList<Long> mids, Long peer_id) throws JSONException, IOException, KException {
  1017.         VKParams params = new VKParams("messages.markAsRead");
  1018.         params.put("message_ids", arrayToString(mids));
  1019.         params.put("peer_id", peer_id);
  1020.         JSONObject root = sendRequest(params);
  1021.         Long response = root.optLong("response");
  1022.         return response;
  1023.     }
  1024.  
  1025.     //http://vk.com/dev/messages.markAsNew
  1026.     @Deprecated
  1027.     public Long markAsNew(ArrayList<Long> mids) throws JSONException, IOException, KException {
  1028.         VKParams params = new VKParams("messages.markAsNew");
  1029.         params.put("message_ids", arrayToString(mids));
  1030.         JSONObject root = sendRequest(params);
  1031.         Long response = root.optLong("response");
  1032.         return response;
  1033.     }
  1034.  
  1035.     // https://vk.com/dev/messages.markAsImportant
  1036.     public ArrayList<Long> markAsImportant(ArrayList<Long> mids, boolean important) throws JSONException, IOException, KException {
  1037.         VKParams params = new VKParams("messages.markAsImportant");
  1038.         params.put("message_ids", arrayToString(mids));
  1039.         params.put("important", important);
  1040.         JSONObject root = sendRequest(params);
  1041.         JSONArray array = root.optJSONArray("response");
  1042.         ArrayList<Long> message_ids = new ArrayList<>();
  1043.         for (int i = 0; i < array.length(); i++) {
  1044.             message_ids.add(array.optLong(i));
  1045.         }
  1046.         return message_ids;
  1047.     }
  1048.  
  1049.     //http://vk.com/dev/messages.delete
  1050.     public Long deleteMessage(Collection<Integer> message_ids) throws IOException, JSONException, KException {
  1051.         if (message_ids.isEmpty()) return null;
  1052.  
  1053.         VKParams params = new VKParams("messages.delete");
  1054.         params.put("message_ids", arrayToString(message_ids));
  1055.         JSONObject root = sendRequest(params);
  1056.         //не парсим ответ - там приходят отдельные флаги для каждого удалённого сообщения
  1057.         // TODO: Может быть, это так, но у меня возвращает 1
  1058.         return root.optLong("response", -1);
  1059.     }
  1060.  
  1061.     // Получение 1.5к сообщений в истории перписки
  1062.     public ArrayList<VKMessage> getMessagesHistoryWithExecute(Integer user_id, Integer chat_id, long offset) throws JSONException, IOException, KException {
  1063.         String var;
  1064.         if (chat_id != 0) {
  1065.             var = "\"chat_id\":" + chat_id + ",\n" +
  1066.                     "\"rev\":" + "1" + ",\n" +
  1067.                     "\"user_id\":" + 0 + ",\n";
  1068.         } else var = "\"user_id\":" + user_id + ",\n" + "\"rev\":" + "1" + ",\n";
  1069.  
  1070.         String code =
  1071.                 "var offset = " + offset + ";\n" +
  1072.                         "var v = " + API_VERSION + ";\n" +
  1073.                         "var a = API.messages.getHistory({\n" +
  1074.                         var +
  1075.                         "\"offset\":offset, \n" +
  1076.                         "\"count\":200, \n" +
  1077.                         "\"v\":v\n" +
  1078.                         "});\n" +
  1079.                         "\n" +
  1080.                         "var b =  API.messages.getHistory({\n" +
  1081.                         var +
  1082.                         "\"offset\":offset + 200, \n" +
  1083.                         "\"count\":200,\n" +
  1084.                         "\"v\":v\n" +
  1085.                         "});\n" +
  1086.                         "\n" +
  1087.                         "var c =  API.messages.getHistory({\n" +
  1088.                         var +
  1089.                         "\"offset\":offset + 400, \n" +
  1090.                         "\"count\":200,\n" +
  1091.                         "\"v\":v\n" +
  1092.                         "});\n" +
  1093.                         "\n" +
  1094.                         "var d =  API.messages.getHistory({\n" +
  1095.                         var +
  1096.                         "\"offset\":offset + 600, \n" +
  1097.                         "\"count\":200,\n" +
  1098.                         "\"v\":v\n" +
  1099.                         "});\n" +
  1100.                         "\n" +
  1101.                         "var e =  API.messages.getHistory({\n" +
  1102.                         var +
  1103.                         "\"offset\":offset + 800, \n" +
  1104.                         "\"count\":200,\n" +
  1105.                         "\"v\":v\n" +
  1106.                         "});\n" +
  1107.                         "\n" +
  1108.                         "var k =  API.messages.getHistory({\n" +
  1109.                         var +
  1110.                         "\"offset\":offset + 1000, \n" +
  1111.                         "\"count\":200,\n" +
  1112.                         "\"v\":v\n" +
  1113.                         "});\n" +
  1114.                         "\n" +
  1115.                         "var j =  API.messages.getHistory({\n" +
  1116.                         var +
  1117.                         "\"offset\":offset + 1200, \n" +
  1118.                         "\"count\":100,\n" +
  1119.                         "\"v\":v\n" +
  1120.                         "});\n" +
  1121.                         "\n" +
  1122.                         "var i =  API.messages.getHistory({\n" +
  1123.                         var +
  1124.                         "\"offset\":offset + 1300, \n" +
  1125.                         "\"count\":200,\n" +
  1126.                         "\"v\":v\n" +
  1127.                         "});\n" +
  1128.                         "\n" +
  1129.                         "return a.items + b.items + c.items + d.items + e.items + k.items + j.items + i.items;";
  1130.  
  1131.         VKParams params = new VKParams("execute");
  1132.         params.put("code", code);
  1133.         JSONObject root = sendRequest(params);
  1134.         JSONArray response = root.optJSONArray("response");
  1135.         return parseMessages(response);
  1136.     }
  1137.  
  1138.     /**
  1139.      * for status**
  1140.      */
  1141.     //http://vk.com/dev/status.get
  1142.     public VKStatus getStatus(Long uid) throws IOException, JSONException, KException {
  1143.         VKParams params = new VKParams("status.get");
  1144.         params.put("user_id", uid);
  1145.         JSONObject root = sendRequest(params);
  1146.         JSONObject obj = root.optJSONObject("response");
  1147.         VKStatus status = new VKStatus();
  1148.         if (obj != null) {
  1149.             status.text = unescape(obj.getString("text"));
  1150.             JSONObject jaudio = obj.optJSONObject("audio");
  1151.             if (jaudio != null)
  1152.                 status.audio = VKAudio.parse(jaudio);
  1153.         }
  1154.         return status;
  1155.     }
  1156.  
  1157.     //http://vk.com/dev/status.set
  1158.     public String setStatus(String status_text) throws IOException, JSONException, KException {
  1159.         VKParams params = new VKParams("status.set");
  1160.         params.put("text", status_text);
  1161.         JSONObject root = sendRequest(params);
  1162.         Object response_id = root.opt("response");
  1163.         if (response_id != null)
  1164.             return String.valueOf(response_id);
  1165.         return null;
  1166.     }
  1167.  
  1168.     /**
  1169.      * methods for wall
  1170.      *
  1171.      * @throws KException **
  1172.      */
  1173.     //http://vk.com/dev/wall.get
  1174.     public ArrayList<VKWallMessage> getWall(long owner_id, int count, int offset, String filter) throws IOException, JSONException, KException {
  1175.         VKParams params = new VKParams("wall.get");
  1176.         params.put("owner_id", owner_id);
  1177.         if (count > 0)
  1178.             params.put("count", count);
  1179.         params.put("offset", offset);
  1180.         params.put("filter", filter); //owner, others, all - default
  1181.         JSONObject root = sendRequest(params);
  1182.         JSONObject response = root.optJSONObject("response");
  1183.         JSONArray array = response.optJSONArray("items");
  1184.         ArrayList<VKWallMessage> wmessages = new ArrayList<VKWallMessage>();
  1185.         int category_count = array.length();
  1186.         for (int i = 0; i < category_count; ++i) {
  1187.             JSONObject o = (JSONObject) array.get(i);
  1188.             VKWallMessage wm = VKWallMessage.parse(o);
  1189.             wmessages.add(wm);
  1190.         }
  1191.         return wmessages;
  1192.     }
  1193.  
  1194.     /**
  1195.      * methods for news
  1196.      *
  1197.      * @throws KException **
  1198.      */
  1199.     //http://vk.com/dev/newsfeed.get
  1200.     //always returns about 33-35 items
  1201.     public Newsfeed getNews(Long start_time, long count, Long end_time, Integer offset, String from, String source_ids, String filters, Integer max_photos, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  1202.         VKParams params = new VKParams("newsfeed.get");
  1203.         params.put("filters", filters);
  1204.         params.put("start_time", start_time);
  1205.         params.put("end_time", end_time);
  1206.         if (count != 0)
  1207.             params.put("count", count);
  1208.         params.put("offset", offset);
  1209.         params.put("from", from);
  1210.         params.put("source_ids", source_ids);
  1211.         params.put("max_photos", max_photos);
  1212.         addCaptchaParams(captcha_key, captcha_sid, params);
  1213.         JSONObject root = sendRequest(params);
  1214.         return Newsfeed.parse(root, false);
  1215.     }
  1216.  
  1217.  
  1218.     //http://vk.com/dev/newsfeed.getRecommended
  1219.     public Newsfeed getRecommendedNews(Long start_time, long count, Long end_time, Integer offset, String from, Integer max_photos, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  1220.         VKParams params = new VKParams("newsfeed.getRecommended");
  1221.         params.put("start_time", start_time);
  1222.         params.put("end_time", end_time);
  1223.         if (count != 0)
  1224.             params.put("count", count);
  1225.         params.put("offset", offset);
  1226.         params.put("from", from);
  1227.         params.put("max_photos", max_photos);
  1228.         addCaptchaParams(captcha_key, captcha_sid, params);
  1229.         JSONObject root = sendRequest(params);
  1230.         Newsfeed parse = Newsfeed.parse(root, false);
  1231.         return parse;
  1232.     }
  1233.  
  1234.     //http://vk.com/dev/newsfeed.getComments
  1235.     public Newsfeed getNewsComments() throws IOException, JSONException, KException {
  1236.         VKParams params = new VKParams("newsfeed.getComments");
  1237.         params.put("last_comments", "1");
  1238.         params.put("count", "50");
  1239.         JSONObject root = sendRequest(params);
  1240.         return Newsfeed.parse(root, true);
  1241.     }
  1242.  
  1243.     /**
  1244.      * for audio **
  1245.      */
  1246.     //http://vk.com/dev/audio.get
  1247.     public ArrayList<VKAudio> getAudio(Integer owner_id, Integer album_id, long count, long offset, Collection<Long> aids, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  1248.         VKParams params = new VKParams("audio.get");
  1249.         if (owner_id != null)
  1250.             params.put("owner_id", owner_id);
  1251.         params.put("audio_ids", arrayToString(aids));//не документировано - возможно уже не работает - возможно нужно использовать audio.getById
  1252.         params.put("album_id", album_id);
  1253.         params.put("count", count);
  1254.         params.put("offset", offset);
  1255.         addCaptchaParams(captcha_key, captcha_sid, params);
  1256.         JSONObject root = sendRequest(params);
  1257.         JSONObject response = root.optJSONObject("response");
  1258.         JSONArray array = response.optJSONArray("items");
  1259.         return parseAudioList(array);
  1260.     }
  1261.  
  1262.     // http://vk.com/dev/audio.getCount
  1263.     public Long getAudioCount(Long owner_id) throws JSONException, IOException, KException {
  1264.         VKParams params = new VKParams("audio.getCount");
  1265.         params.put("owner_id", owner_id);
  1266.  
  1267.         JSONObject root = sendRequest(params);
  1268.         return root.optLong("response");
  1269.     }
  1270.  
  1271.     //http://vk.com/dev/audio.getById
  1272.     public ArrayList<VKAudio> getAudioById(String audios, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  1273.         VKParams params = new VKParams("audio.getById");
  1274.         params.put("audios", audios);
  1275.         addCaptchaParams(captcha_key, captcha_sid, params);
  1276.         JSONObject root = sendRequest(params);
  1277.         JSONArray array = root.optJSONArray("response");
  1278.         return parseAudioList(array);
  1279.     }
  1280.  
  1281.     //http://vk.com/dev/audio.getLyrics
  1282.     public String getLyricsAudio(Long id) throws IOException, JSONException, KException {
  1283.         VKParams params = new VKParams("audio.getLyrics");
  1284.         params.put("lyrics_id", id);
  1285.         JSONObject root = sendRequest(params);
  1286.         JSONObject response = root.optJSONObject("response");
  1287.         return response.optString("text");
  1288.     }
  1289.  
  1290.     //http://vk.com/dev/audio.search
  1291.     public ArrayList<VKAudio> searchAudio(String q, String sort, String lyrics, Long count, Long offset, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  1292.         VKParams params = new VKParams("audio.search");
  1293.         params.put("q", q);
  1294.         params.put("sort", sort);
  1295.         params.put("lyrics", lyrics);
  1296.         params.put("count", count);
  1297.         params.put("offset", offset);
  1298.         params.put("auto_complete", "1");
  1299.         addCaptchaParams(captcha_key, captcha_sid, params);
  1300.         JSONObject root = sendRequest(params);
  1301.         JSONObject response = root.optJSONObject("response");
  1302.         JSONArray array = response.optJSONArray("items");
  1303.         return parseAudioList(array);
  1304.     }
  1305.  
  1306.     //http://vk.com/dev/audio.delete
  1307.     public String deleteAudio(Long aid, Long oid) throws IOException, JSONException, KException {
  1308.         VKParams params = new VKParams("audio.delete");
  1309.         params.put("audio_id", aid);
  1310.         params.put("album_id", aid);//Баг в Api - это лишний параметр
  1311.         params.put("owner_id", oid);
  1312.         JSONObject root = sendRequest(params);
  1313.         Object response_code = root.opt("response");
  1314.         if (response_code != null)
  1315.             return String.valueOf(response_code);
  1316.         return null;
  1317.     }
  1318.  
  1319.     //http://vk.com/dev/audio.add
  1320.     public String addAudio(Long aid, Long oid, Long gid, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  1321.         VKParams params = new VKParams("audio.add");
  1322.         params.put("audio_id", aid);
  1323.         params.put("owner_id", oid);
  1324.         params.put("group_id", gid);
  1325.         addCaptchaParams(captcha_key, captcha_sid, params);
  1326.         JSONObject root = sendRequest(params);
  1327.         Object response_code = root.opt("response");
  1328.         if (response_code != null)
  1329.             return String.valueOf(response_code);
  1330.         return null;
  1331.     }
  1332.  
  1333.     // http://vk.com/dev/audio.restore
  1334.     // TODO: Если время хранения удаленной аудиозаписи истекло (обычно это 20 минут), сервер вернет ошибку 202 (Cache expired).
  1335.     public VKAudio restoreAudio(Long audio_id, Long owner_id) throws JSONException, IOException, KException {
  1336.         VKParams params = new VKParams("audio.restore");
  1337.         params.put("audio_id", audio_id);
  1338.         params.put("owner_id", owner_id);
  1339.  
  1340.         JSONObject root = sendRequest(params);
  1341.         JSONObject response = root.optJSONObject("response");
  1342.         return VKAudio.parse(response);
  1343.     }
  1344.  
  1345.  
  1346.     private ArrayList<VKAudio> parseAudioList(JSONArray array) throws JSONException {
  1347.         ArrayList<VKAudio> audios = new ArrayList<VKAudio>(array.length());
  1348.         if (array != null) {
  1349.             for (int i = 0; i < array.length(); ++i) { //get(0) is integer, it is audio count
  1350.                 JSONObject o = (JSONObject) array.get(i);
  1351.                 audios.add(VKAudio.parse(o));
  1352.             }
  1353.         }
  1354.         return audios;
  1355.     }
  1356.  
  1357.     /**
  1358.      * for video **
  1359.      */
  1360.     //http://vk.com/dev/video.get
  1361.     public ArrayList<VKVideo> getVideo(String videos, int owner_id, Long album_id, String width, Long count, Long offset, String access_key) throws IOException, JSONException, KException {
  1362.         VKParams params = new VKParams("video.get");
  1363.         params.put("videos", videos);
  1364.         params.put("owner_id", owner_id);
  1365.         params.put("width", width);
  1366.         params.put("count", count);
  1367.         params.put("offset", offset);
  1368.         params.put("album_id", album_id);
  1369.         params.put("access_key", access_key);
  1370.         JSONObject root = sendRequest(params);
  1371.         JSONObject response = root.optJSONObject("response");
  1372.         ArrayList<VKVideo> videoss = new ArrayList<VKVideo>();
  1373.         if (response == null)
  1374.             return videoss;
  1375.         JSONArray array = response.optJSONArray("items");
  1376.         if (array != null) {
  1377.             for (int i = 0; i < array.length(); ++i) {
  1378.                 JSONObject o = (JSONObject) array.get(i);
  1379.                 VKVideo video = VKVideo.parse(o);
  1380.                 videoss.add(video);
  1381.             }
  1382.         }
  1383.         return videoss;
  1384.     }
  1385.  
  1386.     //http://vk.com/dev/video.getUserVideos
  1387.     public ArrayList<VKVideo> getUserVideo(Long user_id) throws IOException, JSONException, KException {
  1388.         VKParams params = new VKParams("video.getUserVideos");
  1389.         params.put("user_id", user_id);
  1390.         params.put("count", "50");
  1391.         JSONObject root = sendRequest(params);
  1392.         JSONObject response = root.optJSONObject("response");
  1393.         JSONArray array = response.optJSONArray("items");
  1394.         ArrayList<VKVideo> videos = new ArrayList<VKVideo>();
  1395.         if (array != null) {
  1396.             for (int i = 0; i < array.length(); ++i) {
  1397.                 JSONObject o = (JSONObject) array.get(i);
  1398.                 videos.add(VKVideo.parse(o));
  1399.             }
  1400.         }
  1401.         return videos;
  1402.     }
  1403.  
  1404.     /**
  1405.      * for crate album **
  1406.      */
  1407.     //http://vk.com/dev/photos.createAlbum
  1408.     public VKAlbum createAlbum(String title, Long gid, String privacy, String comment_privacy, String description) throws IOException, JSONException, KException {
  1409.         VKParams params = new VKParams("photos.createAlbum");
  1410.         params.put("title", title);
  1411.         params.put("group_id", gid);
  1412.         params.put("privacy", privacy);
  1413.         params.put("comment_privacy", comment_privacy);
  1414.         params.put("description", description);
  1415.         JSONObject root = sendRequest(params);
  1416.         JSONObject o = root.optJSONObject("response");
  1417.         if (o == null)
  1418.             return null;
  1419.         return VKAlbum.parse(o);
  1420.     }
  1421.  
  1422.     //http://vk.com/dev/photos.editAlbum
  1423.     public String editAlbum(long aid, Long oid, String title, String privacy, String comment_privacy, String description) throws IOException, JSONException, KException {
  1424.         VKParams params = new VKParams("photos.editAlbum");
  1425.         params.put("album_id", String.valueOf(aid));
  1426.         params.put("owner_id", oid);
  1427.         params.put("title", title);
  1428.         params.put("privacy", privacy);
  1429.         params.put("comment_privacy", comment_privacy);
  1430.         params.put("description", description);
  1431.         JSONObject root = sendRequest(params);
  1432.         Object response_code = root.opt("response");
  1433.         return response_code != null ? String.valueOf(response_code) : null;
  1434.     }
  1435.  
  1436.     /**
  1437.      * for notes **
  1438.      */
  1439.     //http://vk.com/dev/notes.get
  1440.     public ArrayList<VKNote> getNotes(Integer uid, Collection<Long> nids, String sort, Long count, Long offset) throws IOException, JSONException, KException {
  1441.         VKParams params = new VKParams("notes.get");
  1442.         params.put("user_id", uid);
  1443.         params.put("note_ids", arrayToString(nids));
  1444.         params.put("sort", sort);
  1445.         params.put("count", count);
  1446.         params.put("offset", offset);
  1447.         JSONObject root = sendRequest(params);
  1448.         JSONObject response = root.optJSONObject("response");
  1449.         JSONArray array = response.optJSONArray("items");
  1450.  
  1451.         return VKNote.parseNotes(array);
  1452.     }
  1453.  
  1454.     //http://vk.com/dev/notes.delete
  1455.     public String deleteNote(Long nid) throws IOException, JSONException, KException {
  1456.         VKParams params = new VKParams("notes.delete");
  1457.         params.put("note_id", nid);
  1458.         JSONObject root = sendRequest(params);
  1459.         Object response_code = root.opt("response");
  1460.         if (response_code != null)
  1461.             return String.valueOf(response_code);
  1462.         return null;
  1463.     }
  1464.  
  1465.     //http://vk.com/dev/photos.getUploadServer
  1466.     public String getPhotoUploadServer(long album_id, Long group_id) throws IOException, JSONException, KException {
  1467.         VKParams params = new VKParams("photos.getUploadServer");
  1468.         params.put("album_id", album_id);
  1469.         params.put("group_id", group_id);
  1470.         JSONObject root = sendRequest(params);
  1471.         JSONObject response = root.getJSONObject("response");
  1472.         return response.getString("upload_url");
  1473.     }
  1474.  
  1475.     //http://vk.com/dev/photos.getWallUploadServer
  1476.     public String getWallUploadServer(Long user_id, Long group_id) throws IOException, JSONException, KException {
  1477.         VKParams params = new VKParams("photos.getWallUploadServer");
  1478.         params.put("user_id", user_id);
  1479.         params.put("group_id", group_id);
  1480.         JSONObject root = sendRequest(params);
  1481.         JSONObject response = root.getJSONObject("response");
  1482.         return response.getString("upload_url");
  1483.     }
  1484.  
  1485.     //http://vk.com/dev/audio.getUploadServer
  1486.     public String getAudioUploadServer() throws IOException, JSONException, KException {
  1487.         VKParams params = new VKParams("audio.getUploadServer");
  1488.         JSONObject root = sendRequest(params);
  1489.         JSONObject response = root.getJSONObject("response");
  1490.         return response.getString("upload_url");
  1491.     }
  1492.  
  1493.     //http://vk.com/dev/photos.getMessagesUploadServer
  1494.     public String getPhotoMessageUploadServer() throws IOException, JSONException, KException {
  1495.         VKParams params = new VKParams("photos.getMessagesUploadServer");
  1496.         JSONObject root = sendRequest(params);
  1497.         JSONObject response = root.getJSONObject("response");
  1498.         return response.getString("upload_url");
  1499.     }
  1500.  
  1501.     //http://vk.com/dev/photos.getProfileUploadServer
  1502.     public String getPhotoProfileUploadServer() throws IOException, JSONException, KException {
  1503.         VKParams params = new VKParams("photos.getProfileUploadServer");
  1504.         JSONObject root = sendRequest(params);
  1505.         JSONObject response = root.getJSONObject("response");
  1506.         return response.getString("upload_url");
  1507.     }
  1508.  
  1509.     //http://vk.com/dev/photos.save
  1510.     public ArrayList<VKPhoto> savePhoto(String server, String photos_list, Long aid, Long group_id, String hash, String caption) throws IOException, JSONException, KException {
  1511.         VKParams params = new VKParams("photos.save");
  1512.         params.put("server", server);
  1513.         params.put("photos_list", photos_list);
  1514.         params.put("album_id", aid);
  1515.         params.put("group_id", group_id);
  1516.         params.put("hash", hash);
  1517.         params.put("caption", caption);
  1518.         JSONObject root = sendRequest(params);
  1519.         JSONArray array = root.getJSONArray("response");
  1520.         ArrayList<VKPhoto> photos = parsePhotos(array);
  1521.         return photos;
  1522.     }
  1523.  
  1524.     //http://vk.com/dev/photos.saveWallPhoto
  1525.     public ArrayList<VKPhoto> saveWallPhoto(String server, String photo, String hash, Long user_id, Long group_id) throws IOException, JSONException, KException {
  1526.         VKParams params = new VKParams("photos.saveWallPhoto");
  1527.         params.put("server", server);
  1528.         params.put("photo", photo);
  1529.         params.put("hash", hash);
  1530.         params.put("user_id", user_id);
  1531.         params.put("group_id", group_id);
  1532.         JSONObject root = sendRequest(params);
  1533.         JSONArray array = root.getJSONArray("response");
  1534.         ArrayList<VKPhoto> photos = parsePhotos(array);
  1535.         return photos;
  1536.     }
  1537.  
  1538.     //http://vk.com/dev/audio.save
  1539.     public VKAudio saveAudio(String server, String audio, String hash, String artist, String title) throws IOException, JSONException, KException {
  1540.         VKParams params = new VKParams("audio.save");
  1541.         params.put("server", server);
  1542.         params.put("audio", audio);
  1543.         params.put("hash", hash);
  1544.         params.put("artist", artist);
  1545.         params.put("title", title);
  1546.         JSONObject root = sendRequest(params);
  1547.         JSONObject response = root.getJSONObject("response");
  1548.         return VKAudio.parse(response);
  1549.     }
  1550.  
  1551.     //http://vk.com/dev/photos.saveMessagesPhoto
  1552.     public ArrayList<VKPhoto> saveMessagesPhoto(String server, String photo, String hash) throws IOException, JSONException, KException {
  1553.         VKParams params = new VKParams("photos.saveMessagesPhoto");
  1554.         params.put("server", server);
  1555.         params.put("photo", photo);
  1556.         params.put("hash", hash);
  1557.         JSONObject root = sendRequest(params);
  1558.         JSONArray array = root.getJSONArray("response");
  1559.         ArrayList<VKPhoto> photos = parsePhotos(array);
  1560.         return photos;
  1561.     }
  1562.  
  1563.     //http://vk.com/dev/photos.saveProfilePhoto
  1564.     public String[] saveProfilePhoto(String server, String photo, String hash) throws IOException, JSONException, KException {
  1565.         VKParams params = new VKParams("photos.saveProfilePhoto");
  1566.         params.put("server", server);
  1567.         params.put("photo", photo);
  1568.         params.put("hash", hash);
  1569.         JSONObject root = sendRequest(params);
  1570.         JSONObject response = root.getJSONObject("response");
  1571.         String src = response.optString("photo_src");
  1572.         String hash1 = response.optString("photo_hash");
  1573.         String[] res = new String[]{src, hash1};
  1574.         return res;
  1575.     }
  1576.  
  1577.     private ArrayList<VKPhoto> parsePhotos(JSONArray array) throws JSONException {
  1578.         return VKPhoto.parsePhotos(array);
  1579.     }
  1580.  
  1581.     //http://vk.com/dev/wall.addComment
  1582.     public long createWallComment(Long owner_id, Long post_id, String text, Long reply_to_cid, Collection<String> attachments, boolean from_group, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  1583.         VKParams params = new VKParams("wall.addComment");
  1584.         params.put("owner_id", owner_id);
  1585.         params.put("post_id", post_id);
  1586.         params.put("text", text);
  1587.         params.put("reply_to_comment", reply_to_cid);
  1588.         params.put("attachments", arrayToString(attachments));
  1589.         if (from_group)
  1590.             params.put("from_group", "1");
  1591.         addCaptchaParams(captcha_key, captcha_sid, params);
  1592.         JSONObject root = sendRequest(params, true);
  1593.         JSONObject response = root.getJSONObject("response");
  1594.         long cid = response.optLong("comment_id");
  1595.         return cid;
  1596.     }
  1597.  
  1598.     //http://vk.com/dev/wall.editComment
  1599.     public boolean editWallComment(long cid, Long owner_id, String text, Collection<String> attachments, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  1600.         VKParams params = new VKParams("wall.editComment");
  1601.         params.put("comment_id", cid);
  1602.         params.put("owner_id", owner_id);
  1603.         params.put("message", text);
  1604.         params.put("attachments", arrayToString(attachments));
  1605.         addCaptchaParams(captcha_key, captcha_sid, params);
  1606.         JSONObject root = sendRequest(params, true);
  1607.         int response = root.optInt("response");
  1608.         return response == 1;
  1609.     }
  1610.  
  1611.     //http://vk.com/dev/wall.post
  1612.     public long createWallPost(long owner_id, String text, Collection<String> attachments, String export, boolean only_friends, boolean from_group, boolean signed, String lat, String lon, Long publish_date, Long post_id, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  1613.         VKParams params = new VKParams("wall.post");
  1614.         params.put("owner_id", owner_id);
  1615.         params.put("attachments", arrayToString(attachments));
  1616.         params.put("lat", lat);
  1617.         params.put("long", lon);
  1618.         params.put("message", text);
  1619.         if (export != null && export.length() != 0)
  1620.             params.put("services", export);
  1621.         if (from_group)
  1622.             params.put("from_group", "1");
  1623.         if (only_friends)
  1624.             params.put("friends_only", "1");
  1625.         if (signed)
  1626.             params.put("signed", "1");
  1627.         params.put("publish_date", publish_date);
  1628.         if (post_id > 0)
  1629.             params.put("post_id", post_id);
  1630.         addCaptchaParams(captcha_key, captcha_sid, params);
  1631.         JSONObject root = sendRequest(params, true);
  1632.         JSONObject response = root.getJSONObject("response");
  1633.         long res_post_id = response.optLong("post_id");
  1634.         return res_post_id;
  1635.     }
  1636.  
  1637.     //http://vk.com/dev/wall.repost
  1638.     public VKWallMessage repostWallPost(String object, String message, Long gid, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  1639.         VKParams params = new VKParams("wall.repost");
  1640.         params.put("group_id", gid);
  1641.         params.put("message", message);
  1642.         params.put("object", object);
  1643.         addCaptchaParams(captcha_key, captcha_sid, params);
  1644.         JSONObject root = sendRequest(params);
  1645.         JSONObject response = root.getJSONObject("response");
  1646.         VKWallMessage wall = new VKWallMessage();
  1647.         wall.id = response.optLong("post_id");
  1648.         wall.like_count = response.optInt("likes_count");
  1649.         wall.reposts_count = response.optInt("reposts_count");
  1650.         return wall;
  1651.     }
  1652.  
  1653.     //http://vk.com/dev/wall.getComments
  1654.     public CommentList getWallComments(Long owner_id, Long post_id, int offset, int count, boolean reverse_order) throws IOException, JSONException, KException {
  1655.         VKParams params = new VKParams("wall.getComments");
  1656.         params.put("post_id", post_id);
  1657.         params.put("owner_id", owner_id);
  1658.         /*
  1659.         if (sort != null)
  1660.             params.put("sort", sort);
  1661.             //asc - хронологический
  1662.             //desc - антихронологический
  1663.         */
  1664.         if (offset > 0)
  1665.             params.put("offset", offset);
  1666.         if (count > 0)
  1667.             params.put("count", count);
  1668.         params.put("preview_length", "0");
  1669.         params.put("need_likes", "1");
  1670.         if (reverse_order)
  1671.             params.put("sort", "desc");
  1672.         JSONObject root = sendRequest(params);
  1673.         JSONObject response = root.optJSONObject("response");
  1674.         JSONArray array = response.optJSONArray("items");
  1675.         CommentList commnets = new CommentList();
  1676.         commnets.count = response.getInt("count");
  1677.         int category_count = array.length();
  1678.         for (int i = 0; i < category_count; ++i)
  1679.             commnets.comments.add(VKComment.parse((JSONObject) array.get(i)));
  1680.         return commnets;
  1681.     }
  1682.  
  1683.  
  1684.     //deprecated, use http://vk.com/dev/likes.add instead
  1685.     @Deprecated
  1686.     public Long wallAddLike(Long owner_id, Long post_id, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  1687.         return addLike(owner_id, post_id, "post", null, captcha_key, captcha_sid);
  1688.     }
  1689.  
  1690.     //deprecated, use http://vk.com/dev/likes.delete instead
  1691.     @Deprecated
  1692.     public Long wallDeleteLike(Long owner_id, Long post_id) throws IOException, JSONException, KException {
  1693.         return deleteLike(owner_id, "post", post_id, null, null);
  1694.     }
  1695.  
  1696.     //http://vk.com/dev/likes.add
  1697.     public Long addLike(Long owner_id, Long item_id, String type, String access_key, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  1698.         VKParams params = new VKParams("likes.add");
  1699.         params.put("owner_id", owner_id);
  1700.         params.put("item_id", item_id);
  1701.         params.put("type", type);
  1702.         params.put("access_key", access_key);
  1703.         addCaptchaParams(captcha_key, captcha_sid, params);
  1704.         JSONObject root = sendRequest(params);
  1705.         JSONObject response = root.optJSONObject("response");
  1706.         long likes = response.optLong("likes", -1);
  1707.         return likes;
  1708.     }
  1709.  
  1710.     //http://vk.com/dev/likes.delete
  1711.     public Long deleteLike(Long owner_id, String type, Long item_id, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  1712.         VKParams params = new VKParams("likes.delete");
  1713.         params.put("owner_id", owner_id);
  1714.         params.put("type", type);
  1715.         params.put("item_id", item_id);
  1716.         addCaptchaParams(captcha_key, captcha_sid, params);
  1717.         JSONObject root = sendRequest(params);
  1718.         JSONObject response = root.optJSONObject("response");
  1719.         return response.optLong("likes", -1);
  1720.     }
  1721.  
  1722.     //http://vk.com/dev/photos.getById
  1723.     public ArrayList<VKPhoto> getPhotosById(String photos, Integer extended, Integer photo_sizes) throws IOException, JSONException, KException {
  1724.         VKParams params = new VKParams("photos.getById");
  1725.         params.put("photos", photos);
  1726.         params.put("extended", extended);
  1727.         params.put("photo_sizes", photo_sizes);
  1728.         JSONObject root = sendRequest(params);
  1729.         JSONArray response = root.optJSONArray("response");
  1730.         if (response == null)
  1731.             return new ArrayList<VKPhoto>();
  1732.         ArrayList<VKPhoto> photos1 = parsePhotos(response);
  1733.         return photos1;
  1734.     }
  1735.  
  1736.     public VKPhoto getPhotoCountsByIdWithExecute(String photo, boolean get_user_id) throws IOException, JSONException, KException {
  1737.         String b = (get_user_id) ? ",\"user_id\":p@.user_id" : "";
  1738.         String code = "var p=API.photos.getById({\"photos\":\"" + photo + "\",\"extended\":1}); return {\"pid\":p@.id,\"likes\":p@.likes,\"comments\":p@.comments,\"can_comment\":p@.can_comment,\"tags\":p@.tags" + b + "};";
  1739.         VKParams params = new VKParams("execute");
  1740.         params.put("code", code);
  1741.         JSONObject root = sendRequest(params);
  1742.         JSONObject array = root.optJSONObject("response");
  1743.         if (array == null)
  1744.             return null;
  1745.         return VKPhoto.parseCounts(array);
  1746.     }
  1747.  
  1748.     //http://vk.com/dev/groups.get
  1749.     public ArrayList<VKGroup> getGroups(Integer user_id) throws IOException, JSONException, KException {
  1750.         VKParams params = new VKParams("groups.get");
  1751.         params.put("extended", "1");
  1752.         params.put("fields", "members_count");
  1753.         params.put("user_id", user_id);
  1754.         JSONObject root = sendRequest(params);
  1755.         ArrayList<VKGroup> groups = new ArrayList<VKGroup>();
  1756.         JSONObject response = root.optJSONObject("response");
  1757.         JSONArray array = response.optJSONArray("items");
  1758.         //if there are no groups "response" will not be array
  1759.         if (array == null)
  1760.             return groups;
  1761.         groups = VKGroup.parseGroups(array);
  1762.         return groups;
  1763.     }
  1764.  
  1765.     //http://vk.com/dev/wall.delete
  1766.     public Boolean deleteWallPost(Long post_id, long wall_owner_id) throws IOException, JSONException, KException {
  1767.         VKParams params = new VKParams("wall.delete");
  1768.         params.put("owner_id", wall_owner_id);
  1769.         params.put("post_id", post_id);
  1770.         JSONObject root = sendRequest(params);
  1771.         int response = root.optInt("response");
  1772.         return response == 1;
  1773.     }
  1774.  
  1775.     //http://vk.com/dev/wall.deleteComment
  1776.     public Boolean deleteWallComment(Long wall_owner_id, long comment_id) throws IOException, JSONException, KException {
  1777.         VKParams params = new VKParams("wall.deleteComment");
  1778.         params.put("owner_id", wall_owner_id);
  1779.         params.put("comment_id", comment_id);
  1780.         JSONObject root = sendRequest(params);
  1781.         int response = root.optInt("response");
  1782.         return response == 1;
  1783.     }
  1784.  
  1785.     //http://vk.com/dev/notes.deleteComment
  1786.     public Boolean deleteNoteComment(Long note_owner_id, long comment_id) throws IOException, JSONException, KException {
  1787.         VKParams params = new VKParams("notes.deleteComment");
  1788.         params.put("owner_id", note_owner_id);
  1789.         params.put("comment_id", comment_id);
  1790.         JSONObject root = sendRequest(params);
  1791.         int response = root.optInt("response");
  1792.         return response == 1;
  1793.     }
  1794.  
  1795.     //http://vk.com/dev/video.deleteComment
  1796.     public Boolean deleteVideoComment(Long video_owner_id, long comment_id) throws IOException, JSONException, KException {
  1797.         VKParams params = new VKParams("video.deleteComment");
  1798.         params.put("owner_id", video_owner_id);
  1799.         params.put("comment_id", comment_id);
  1800.         JSONObject root = sendRequest(params);
  1801.         int response = root.optInt("response");
  1802.         return response == 1;
  1803.     }
  1804.  
  1805.     //http://vk.com/dev/photos.deleteComment
  1806.     public Boolean deletePhotoComment(long photo_id, Long photo_owner_id, long comment_id) throws IOException, JSONException, KException {
  1807.         VKParams params = new VKParams("photos.deleteComment");
  1808.         params.put("owner_id", photo_owner_id);
  1809.         params.put("comment_id", comment_id);
  1810.         params.put("photo_id", photo_id);
  1811.         JSONObject root = sendRequest(params);
  1812.         int response = root.optInt("response");
  1813.         return response == 1;
  1814.     }
  1815.  
  1816.     //http://vk.com/dev/video.search
  1817.     public ArrayList<VKVideo> searchVideo(String q, String sort, String hd, Long count, Long offset, Integer adult, String filters) throws IOException, JSONException, KException {
  1818.         VKParams params = new VKParams("video.search");
  1819.         params.put("q", q);
  1820.         params.put("sort", sort);
  1821.         params.put("hd", hd);
  1822.         params.put("count", count);
  1823.         params.put("offset", offset);
  1824.         params.put("adult", adult);     //safe search: 1 - disabled, 0 - enabled
  1825.         params.put("filters", filters); //mp4, youtube, vimeo, short, long
  1826.         JSONObject root = sendRequest(params);
  1827.         JSONObject response = root.optJSONObject("response");
  1828.         JSONArray array = response.optJSONArray("items");
  1829.         ArrayList<VKVideo> videoss = new ArrayList<VKVideo>();
  1830.         if (array != null) {
  1831.             for (int i = 0; i < array.length(); ++i) {
  1832.                 JSONObject o = (JSONObject) array.get(i);
  1833.                 VKVideo video = VKVideo.parse(o);
  1834.                 videoss.add(video);
  1835.             }
  1836.         }
  1837.         return videoss;
  1838.     }
  1839.  
  1840.     //http://vk.com/dev/users.search
  1841.     public ArrayList<VKFullUser> searchUser(String q, String fields, Long count, Long offset, Integer sort,
  1842.                                             Integer city, Integer country, String hometown, Integer university_country, Integer university, Integer university_year,
  1843.                                             Integer sex, Integer status, Integer age_from, Integer age_to, Integer birth_day, Integer birth_month, Integer birth_year,
  1844.                                             Integer online, Integer has_photo, Integer school_country, Integer school_city, Integer school, Integer school_year,
  1845.                                             String religion, String interests, String company, String position, Long gid) throws IOException, JSONException, KException {
  1846.         VKParams params = new VKParams("users.search");
  1847.         params.put("q", q);
  1848.         params.put("count", count);
  1849.         params.put("offset", offset);
  1850.         params.put("fields", fields);
  1851.         if (sort != null && sort > 0)
  1852.             params.put("sort", sort);
  1853.         if (city != null && city > 0)
  1854.             params.put("city", city);
  1855.         if (country != null && country > 0)
  1856.             params.put("country", country);
  1857.         if (hometown != null && hometown.length() > 0)
  1858.             params.put("hometown", hometown);
  1859.         if (university_country != null && university_country > 0)
  1860.             params.put("university_country", university_country);
  1861.         if (university != null && university > 0)
  1862.             params.put("university", university);
  1863.         if (university_year != null && university_year > 0)
  1864.             params.put("university_year", university_year);
  1865.         if (sex != null && sex > 0)
  1866.             params.put("sex", sex);
  1867.         if (status != null && status > 0)
  1868.             params.put("status", status);
  1869.         if (age_from != null && age_from > 0)
  1870.             params.put("age_from", age_from);
  1871.         if (age_to != null && age_to > 0)
  1872.             params.put("age_to", age_to);
  1873.         if (birth_day != null && birth_day > 0)
  1874.             params.put("birth_day", birth_day);
  1875.         if (birth_month != null && birth_month > 0)
  1876.             params.put("birth_month", birth_month);
  1877.         if (birth_year != null && birth_year > 0)
  1878.             params.put("birth_year", birth_year);
  1879.         if (online != null && online > 0)
  1880.             params.put("online", online);
  1881.         if (has_photo != null && has_photo > 0)
  1882.             params.put("has_photo", has_photo);
  1883.         if (school_country != null && school_country > 0)
  1884.             params.put("school_country", school_country);
  1885.         if (school_city != null && school_city > 0)
  1886.             params.put("school_city", school_city);
  1887.         if (school != null && school > 0)
  1888.             params.put("school", school);
  1889.         if (school_year != null && school_year > 0)
  1890.             params.put("school_year", school_year);
  1891.         if (religion != null && religion.length() > 0)
  1892.             params.put("religion", religion);
  1893.         if (interests != null && interests.length() > 0)
  1894.             params.put("interests", interests);
  1895.         if (company != null && company.length() > 0)
  1896.             params.put("company", company);
  1897.         if (position != null && position.length() > 0)
  1898.             params.put("position", position);
  1899.         if (gid != null && gid > 0)
  1900.             params.put("group_id", gid);
  1901.         JSONObject root = sendRequest(params);
  1902.         JSONObject response = root.optJSONObject("response");
  1903.         JSONArray array = response.optJSONArray("items");
  1904.         return VKFullUser.parseUsers(array);
  1905.     }
  1906.  
  1907.     public ArrayList<VKFullUser> searchUserExtended(String q, String fields, Long count, Long offset, Integer sort,
  1908.                                                     Integer city, Integer country, String hometown, Integer university_country, Integer university, Integer university_year,
  1909.                                                     Integer sex, Integer status, Integer age_from, Integer age_to, Integer birth_day, Integer birth_month, Integer birth_year,
  1910.                                                     Integer online, Integer has_photo, Integer school_country, Integer school_city, Integer school, Integer school_year,
  1911.                                                     String religion, String interests, String company, String position, Long gid) throws IOException, JSONException, KException {
  1912.         String uids = Utils.parseProfileId(q);
  1913.         if (uids != null && uids.length() > 0 && (offset == null || offset == 0)) {
  1914.             Log.i(TAG, "Search with uid = " + uids);
  1915.             String code = "var a=API.users.search({\"q\":\"" + q + "\",\"count\":" + count + ",\"count\":" + offset + ",\"fields\":\"" + fields + "\"});";
  1916.             code += "var b=API.users.get({\"user_ids\":" + uids + ",\"fields\":\"" + fields + "\"});";
  1917.             code += "return b+a.items;";
  1918.             VKParams params = new VKParams("execute");
  1919.             params.put("code", code);
  1920.             JSONObject root = sendRequest(params);
  1921.             JSONArray array = root.optJSONArray("response");
  1922.             return VKFullUser.parseUsers(array);
  1923.         } else
  1924.             return searchUser(q, fields, count, offset, sort,
  1925.                     city, country, hometown, university_country, university, university_year,
  1926.                     sex, status, age_from, age_to, birth_day, birth_month, birth_year,
  1927.                     online, has_photo, school_country, school_city, school, school_year,
  1928.                     religion, interests, company, position, gid);
  1929.     }
  1930.  
  1931.     //http://vk.com/dev/video.delete
  1932.     public String deleteVideo(Long vid, Long oid) throws IOException, JSONException, KException {
  1933.         VKParams params = new VKParams("video.delete");
  1934.         params.put("video_id", vid);
  1935.         params.put("owner_id", oid);
  1936.         JSONObject root = sendRequest(params);
  1937.         Object response_code = root.opt("response");
  1938.         if (response_code != null)
  1939.             return String.valueOf(response_code);
  1940.         return null;
  1941.     }
  1942.  
  1943.     //http://vk.com/dev/video.add
  1944.     public String addVideo(Long vid, Long oid) throws IOException, JSONException, KException {
  1945.         VKParams params = new VKParams("video.add");
  1946.         params.put("video_id", vid);
  1947.         params.put("owner_id", oid);
  1948.         JSONObject root = sendRequest(params);
  1949.         Object response_code = root.opt("response");
  1950.         if (response_code != null)
  1951.             return String.valueOf(response_code);
  1952.         return null;
  1953.     }
  1954.  
  1955.     //http://vk.com/dev/notes.add
  1956.     public long createNote(String title, String text) throws IOException, JSONException, KException {
  1957.         VKParams params = new VKParams("notes.add");
  1958.         params.put("title", title);
  1959.         params.put("text", text);
  1960.         params.put("privacy", "0");
  1961.         params.put("comment_privacy", "0");
  1962.         JSONObject root = sendRequest(params, true);
  1963.         long note_id = root.getLong("response");
  1964.         return note_id;
  1965.     }
  1966.  
  1967.  
  1968.     //http://vk.com/dev/account.setOnline
  1969.     public void setOnline(String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  1970.         VKParams params = new VKParams("account.setOnline");
  1971.         addCaptchaParams(captcha_key, captcha_sid, params);
  1972.         sendRequest(params);
  1973.     }
  1974.  
  1975.     //http://vk.com/dev/friends.add
  1976.     public long addFriend(Long uid, String text, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  1977.         VKParams params = new VKParams("friends.add");
  1978.         params.put("user_id", uid);
  1979.         params.put("text", text);
  1980.         addCaptchaParams(captcha_key, captcha_sid, params);
  1981.         JSONObject root = sendRequest(params);
  1982.         return root.optLong("response");
  1983.     }
  1984.  
  1985.     //http://vk.com/dev/friends.delete
  1986.     public long deleteFriend(Long uid) throws IOException, JSONException, KException {
  1987.         VKParams params = new VKParams("friends.delete");
  1988.         params.put("user_id", uid);
  1989.         JSONObject root = sendRequest(params);
  1990.         return root.optLong("response");
  1991.     }
  1992.  
  1993.     //http://vk.com/dev/friends.getRequests
  1994.     public ArrayList<Object[]> getRequestsFriends(Integer out) throws IOException, JSONException, KException {
  1995.         VKParams params = new VKParams("friends.getRequests");
  1996.         params.put("need_messages", "1");
  1997.         params.put("out", out);
  1998.         JSONObject root = sendRequest(params);
  1999.         JSONObject response = root.optJSONObject("response");
  2000.         JSONArray array = response.optJSONArray("items");
  2001.         ArrayList<Object[]> users = new ArrayList<Object[]>();
  2002.         if (array != null) {
  2003.             int category_count = array.length();
  2004.             for (int i = 0; i < category_count; ++i) {
  2005.                 JSONObject item = array.optJSONObject(i);
  2006.                 if (item != null) {
  2007.                     Long id = item.optLong("user_id", -1);
  2008.                     if (id != -1) {
  2009.                         Object[] u = new Object[2];
  2010.                         u[0] = id;
  2011.                         u[1] = item.optString("message");
  2012.                         users.add(u);
  2013.                     }
  2014.                 }
  2015.             }
  2016.         }
  2017.         return users;
  2018.     }
  2019.  
  2020.     //http://vk.com/dev/users.getSubscriptions
  2021.     public ArrayList<Long> getSubscriptions(Long uid, int offset, int count, Integer extended) throws IOException, JSONException, KException {
  2022.         VKParams params = new VKParams("users.getSubscriptions");
  2023.         params.put("user_id", uid);
  2024.         //params.put("extended", extended); //TODO
  2025.         if (offset > 0)
  2026.             params.put("offset", offset);
  2027.         if (count > 0)
  2028.             params.put("count", count);
  2029.         JSONObject root = sendRequest(params);
  2030.         JSONObject response = root.getJSONObject("response");
  2031.         JSONObject jusers = response.optJSONObject("users");
  2032.         JSONArray array = jusers.optJSONArray("items");
  2033.         ArrayList<Long> users = new ArrayList<Long>();
  2034.         if (array != null) {
  2035.             int category_count = array.length();
  2036.             for (int i = 0; i < category_count; ++i) {
  2037.                 Long id = array.optLong(i, -1);
  2038.                 if (id != -1)
  2039.                     users.add(id);
  2040.             }
  2041.         }
  2042.         return users;
  2043.     }
  2044.  
  2045.     //http://vk.com/dev/users.getFollowers
  2046.     public ArrayList<VKUser> getFollowers(Integer uid, int offset, int count, String fields, String name_case) throws IOException, JSONException, KException {
  2047.         VKParams params = new VKParams("users.getFollowers");
  2048.         params.put("user_id", uid);
  2049.         if (offset > 0)
  2050.             params.put("offset", offset);
  2051.         if (count > 0)
  2052.             params.put("count", count);
  2053.  
  2054.         // if this method is called without fields it will return just ids in wrong format
  2055.         if (fields == null)
  2056.             fields = "first_name,last_name, photo_100, online";
  2057.         params.put("fields", fields);
  2058.         params.put("name_case", name_case);
  2059.         JSONObject root = sendRequest(params);
  2060.         JSONObject response = root.getJSONObject("response");
  2061.         JSONArray array = response.optJSONArray("items");
  2062.         return VKUser.parseUsers(array);
  2063.     }
  2064.  
  2065.     //http://vk.com/dev/messages.deleteDialog
  2066.     // TODO: Обратите внимание, что на метод наложено ограничение, за один вызов нельзя удалить больше 10000 сообщений,
  2067.     // TODO: поэтому если сообщений в переписке больше — метод нужно вызывать несколько раз.
  2068.     public int deleteMessageDialog(Long uid, Long chatId) throws IOException, JSONException, KException {
  2069.         VKParams params = new VKParams("messages.deleteDialog");
  2070.         if (chatId != 0) {
  2071.             params.put("chat_id", chatId);
  2072.         } else {
  2073.             params.put("user_id", uid);
  2074.         }
  2075.         JSONObject root = sendRequest(params);
  2076.         return root.getInt("response");
  2077.     }
  2078.  
  2079.     //http://vk.com/dev/execute
  2080.     public void execute(String code) throws IOException, JSONException, KException {
  2081.         VKParams params = new VKParams("execute");
  2082.         params.put("code", code);
  2083.         sendRequest(params);
  2084.     }
  2085.  
  2086.     //http://vk.com/dev/photos.delete
  2087.     public boolean deletePhoto(Long owner_id, Long photo_id) throws IOException, JSONException, KException {
  2088.         VKParams params = new VKParams("photos.delete");
  2089.         params.put("owner_id", owner_id);
  2090.         params.put("photo_id", photo_id);
  2091.         JSONObject root = sendRequest(params);
  2092.         long response = root.optLong("response", -1);
  2093.         return response == 1;
  2094.     }
  2095.  
  2096.     //http://vk.com/dev/polls.getById
  2097.     public VKPoll getPollById(long poll_id, long owner_id, long topic_id) throws JSONException, IOException, KException {
  2098.         VKParams params = new VKParams("polls.getById");
  2099.         params.put("owner_id", owner_id);
  2100.         params.put("poll_id", poll_id);
  2101.         if (topic_id != 0)
  2102.             params.put("board", topic_id);
  2103.         JSONObject root = sendRequest(params);
  2104.         JSONObject response = root.getJSONObject("response");
  2105.         return VKPoll.parse(response);
  2106.     }
  2107.  
  2108.     //http://vk.com/dev/polls.addVote
  2109.     public int addPollVote(long poll_id, long answer_id, long owner_id, long topic_id, String captcha_key, String captcha_sid) throws JSONException, IOException, KException {
  2110.         VKParams params = new VKParams("polls.addVote");
  2111.         params.put("owner_id", owner_id);
  2112.         params.put("poll_id", poll_id);
  2113.         if (topic_id != 0)
  2114.             params.put("board", topic_id);
  2115.         params.put("answer_id", answer_id);
  2116.         addCaptchaParams(captcha_key, captcha_sid, params);
  2117.         JSONObject root = sendRequest(params);
  2118.         return root.getInt("response");
  2119.     }
  2120.  
  2121.     //http://vk.com/dev/polls.deleteVote
  2122.     public int deletePollVote(long poll_id, long answer_id, long owner_id, long topic_id) throws JSONException, IOException, KException {
  2123.         VKParams params = new VKParams("polls.deleteVote");
  2124.         params.put("owner_id", owner_id);
  2125.         params.put("poll_id", poll_id);
  2126.         if (topic_id != 0)
  2127.             params.put("board", topic_id);
  2128.         params.put("answer_id", answer_id);
  2129.         JSONObject root = sendRequest(params);
  2130.         return root.getInt("response");
  2131.     }
  2132.  
  2133.     //http://vk.com/dev/polls.getVoters
  2134.     public ArrayList<VKFullUser> getPollVoters(long poll_id, long owner_id, Collection<Long> answer_ids, Long count, Long offset, String fields, long topic_id) throws JSONException, IOException, KException {
  2135.         VKParams params = new VKParams("polls.getVoters");
  2136.         params.put("owner_id", owner_id);
  2137.         params.put("poll_id", poll_id);
  2138.         if (topic_id != 0)
  2139.             params.put("board", topic_id);
  2140.         params.put("answer_ids", arrayToString(answer_ids));
  2141.         params.put("count", count);
  2142.         params.put("offset", offset);
  2143.         params.put("fields", fields);
  2144.         JSONObject root = sendRequest(params);
  2145.         JSONArray response = root.optJSONArray("response");//массив ответов
  2146.         JSONObject object = (JSONObject) response.get(0);
  2147.         JSONObject array2 = object.optJSONObject("users");
  2148.         JSONArray array = array2.optJSONArray("items");
  2149.         //TODO for others answer_ids
  2150.         return VKFullUser.parseUsers(array);
  2151.     }
  2152.  
  2153.     //http://vk.com/dev/friends.getLists
  2154.     public ArrayList<FriendsList> getFriendLists() throws JSONException, IOException, KException {
  2155.         VKParams params = new VKParams("friends.getLists");
  2156.         JSONObject root = sendRequest(params);
  2157.         JSONObject response = root.optJSONObject("response");
  2158.         JSONArray array = response.optJSONArray("items");
  2159.  
  2160.         ArrayList<FriendsList> result = new ArrayList<>();
  2161.         for (int i = 0; i < array.length(); i++) {
  2162.             FriendsList list = FriendsList.parse(array.optJSONObject(i));
  2163.             result.add(list);
  2164.         }
  2165.         return result;
  2166.     }
  2167.  
  2168.     //http://vk.com/dev/video.save
  2169.     public String saveVideo(String name, String description, Long gid, int privacy_view, int privacy_comment) throws IOException, JSONException, KException {
  2170.         VKParams params = new VKParams("video.save");
  2171.         params.put("name", name);
  2172.         params.put("description", description);
  2173.         params.put("group_id", gid);
  2174.         if (privacy_view > 0)
  2175.             params.put("privacy_view", privacy_view);
  2176.         if (privacy_comment > 0)
  2177.             params.put("privacy_comment", privacy_comment);
  2178.         JSONObject root = sendRequest(params);
  2179.         JSONObject response = root.getJSONObject("response");
  2180.         return response.getString("upload_url");
  2181.     }
  2182.  
  2183.     //http://vk.com/dev/photos.deleteAlbum
  2184.     public String deleteAlbum(Long aid, Long gid) throws IOException, JSONException, KException {
  2185.         VKParams params = new VKParams("photos.deleteAlbum");
  2186.         params.put("album_id", aid);
  2187.         params.put("group_id", gid);
  2188.         JSONObject root = sendRequest(params);
  2189.         Object response_code = root.opt("response");
  2190.         if (response_code != null)
  2191.             return String.valueOf(response_code);
  2192.         return null;
  2193.     }
  2194.  
  2195.     //http://vk.com/dev/photos.getTags
  2196.     public ArrayList<PhotoTag> getPhotoTagsById(Long pid, Long owner_id) throws IOException, JSONException, KException {
  2197.         VKParams params = new VKParams("photos.getTags");
  2198.         params.put("owner_id", owner_id);
  2199.         params.put("photo_id", pid);
  2200.         JSONObject root = sendRequest(params);
  2201.         JSONArray array = root.optJSONArray("response");
  2202.         if (array == null)
  2203.             return new ArrayList<PhotoTag>();
  2204.         ArrayList<PhotoTag> photo_tags = parsePhotoTags(array, pid, owner_id);
  2205.         return photo_tags;
  2206.     }
  2207.  
  2208.     private ArrayList<PhotoTag> parsePhotoTags(JSONArray array, Long pid, Long owner_id) throws JSONException {
  2209.         ArrayList<PhotoTag> photo_tags = new ArrayList<PhotoTag>();
  2210.         int category_count = array.length();
  2211.         for (int i = 0; i < category_count; ++i) {
  2212.             JSONObject o = (JSONObject) array.get(i);
  2213.             PhotoTag p = PhotoTag.parse(o);
  2214.             photo_tags.add(p);
  2215.             if (pid != null)
  2216.                 p.pid = pid;
  2217.             if (owner_id != null)
  2218.                 p.owner_id = owner_id;
  2219.         }
  2220.         return photo_tags;
  2221.     }
  2222.  
  2223.     //http://vk.com/dev/photos.putTag
  2224.     public String putPhotoTag(PhotoTag ptag, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  2225.         if (ptag == null)
  2226.             return null;
  2227.         VKParams params = new VKParams("photos.putTag");
  2228.         params.put("owner_id", ptag.owner_id);
  2229.         params.put("photo_id", ptag.pid);
  2230.         params.put("user_id", ptag.uid);
  2231.         params.putDouble("x", ptag.x);
  2232.         params.putDouble("x2", ptag.x2);
  2233.         params.putDouble("y", ptag.y);
  2234.         params.putDouble("y2", ptag.y2);
  2235.         addCaptchaParams(captcha_key, captcha_sid, params);
  2236.         JSONObject root = sendRequest(params);
  2237.         Object response_code = root.opt("response");
  2238.         if (response_code != null)
  2239.             return String.valueOf(response_code);
  2240.         return null;
  2241.     }
  2242.  
  2243.     /**
  2244.      * topics region **
  2245.      */
  2246.     //http://vk.com/dev/board.getTopics
  2247.     public ArrayList<GroupTopic> getGroupTopics(long gid, Integer order, int extended, int count, int offset) throws IOException, JSONException, KException {
  2248.         VKParams params = new VKParams("board.getTopics");
  2249.         params.put("group_id", gid);
  2250.         params.put("order", order);
  2251.         if (extended == 1)
  2252.             params.put("extended", "1"); //for profiles
  2253.         if (count > 0)
  2254.             params.put("count", count);
  2255.         if (offset > 0)
  2256.             params.put("offset", offset);
  2257.         JSONObject root = sendRequest(params);
  2258.         ArrayList<GroupTopic> result = new ArrayList<GroupTopic>();
  2259.         JSONObject response = root.optJSONObject("response");
  2260.         if (response != null) {
  2261.             JSONArray topics = response.optJSONArray("items");
  2262.             if (topics != null) {
  2263.                 for (int i = 0; i < topics.length(); ++i) {
  2264.                     JSONObject o = topics.getJSONObject(i);
  2265.                     GroupTopic gt = GroupTopic.parse(o);
  2266.                     gt.gid = gid;
  2267.                     result.add(gt);
  2268.                 }
  2269.             }
  2270.         }
  2271.         return result;
  2272.     }
  2273.  
  2274.     //http://vk.com/dev/board.getComments
  2275.     public CommentList getGroupTopicComments(long gid, long tid, int photo_sizes, int extended, int count, int offset, boolean reverse_order) throws IOException, JSONException, KException {
  2276.         VKParams params = new VKParams("board.getComments");
  2277.         params.put("group_id", gid);
  2278.         params.put("topic_id", tid);
  2279.         if (photo_sizes == 1)
  2280.             params.put("photo_sizes", "1");
  2281.         if (extended == 1)
  2282.             params.put("extended", "1");
  2283.         if (count > 0)
  2284.             params.put("count", count);
  2285.         if (offset > 0)
  2286.             params.put("offset", offset);
  2287.         if (reverse_order)
  2288.             params.put("sort", "desc");
  2289.         params.put("need_likes", "1");
  2290.         JSONObject root = sendRequest(params);
  2291.         JSONObject response = root.optJSONObject("response");
  2292.         CommentList result = new CommentList();
  2293.         if (response != null) {
  2294.             JSONArray comments = response.optJSONArray("items");
  2295.             int category_count = comments.length();
  2296.             result.count = response.getInt("count");
  2297.             for (int i = 0; i < category_count; ++i) {
  2298.                 JSONObject o = comments.getJSONObject(i);
  2299.                 VKComment c = VKComment.parseTopicComment(o);
  2300.                 result.comments.add(c);
  2301.             }
  2302.  
  2303.             //topic poll parsed separately
  2304.             if (offset == 0) {
  2305.                 JSONObject poll_json = response.optJSONObject("poll");
  2306.                 if (poll_json != null) {
  2307.                     VKPoll poll = VKPoll.parse(poll_json);
  2308.                     poll.topic_id = tid;
  2309.                     VKAttachment attachment = new VKAttachment();
  2310.                     attachment.poll = poll;
  2311.                     attachment.type = "poll";
  2312.                     if (result.comments.size() > 0)
  2313.                         result.comments.get(0).attachments.add(attachment);
  2314.                 }
  2315.             }
  2316.         }
  2317.         return result;
  2318.     }
  2319.  
  2320.     //http://vk.com/dev/board.addComment
  2321.     public long createGroupTopicComment(long gid, long tid, String text, Collection<String> attachments, boolean from_group, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  2322.         VKParams params = new VKParams("board.addComment");
  2323.         params.put("group_id", gid);
  2324.         params.put("topic_id", tid);
  2325.         params.put("text", text);
  2326.         params.put("attachments", arrayToString(attachments));
  2327.         if (from_group)
  2328.             params.put("from_group", "1");
  2329.         addCaptchaParams(captcha_key, captcha_sid, params);
  2330.         JSONObject root = sendRequest(params, true);
  2331.         long message_id = root.optLong("response");
  2332.         return message_id;
  2333.     }
  2334.  
  2335.     //http://vk.com/dev/board.editComment
  2336.     public boolean editGroupTopicComment(long cid, long gid, long tid, String text, Collection<String> attachments, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  2337.         VKParams params = new VKParams("board.editComment");
  2338.         params.put("comment_id", cid);
  2339.         params.put("group_id", gid);
  2340.         params.put("topic_id", tid);
  2341.         params.put("text", text);
  2342.         params.put("attachments", arrayToString(attachments));
  2343.         addCaptchaParams(captcha_key, captcha_sid, params);
  2344.         JSONObject root = sendRequest(params, true);
  2345.         int response = root.optInt("response");
  2346.         return response == 1;
  2347.     }
  2348.  
  2349.     //http://vk.com/dev/board.deleteComment
  2350.     public Boolean deleteGroupTopicComment(long gid, long tid, long cid) throws IOException, JSONException, KException {
  2351.         VKParams params = new VKParams("board.deleteComment");
  2352.         params.put("group_id", gid);
  2353.         params.put("topic_id", tid);
  2354.         params.put("comment_id", cid);
  2355.         JSONObject root = sendRequest(params);
  2356.         int response = root.optInt("response");
  2357.         return response == 1;
  2358.     }
  2359.  
  2360.     //http://vk.com/dev/board.addTopic
  2361.     public long createGroupTopic(long gid, String title, String text, boolean from_group, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  2362.         VKParams params = new VKParams("board.addTopic");
  2363.         params.put("group_id", gid);
  2364.         params.put("title", title);
  2365.         params.put("text", text);
  2366.         if (from_group)
  2367.             params.put("from_group", "1");
  2368.         addCaptchaParams(captcha_key, captcha_sid, params);
  2369.         JSONObject root = sendRequest(params);
  2370.         long topic_id = root.optLong("response");
  2371.         return topic_id;
  2372.     }
  2373.  
  2374.     //http://vk.com/dev/board.deleteTopic
  2375.     public Boolean deleteGroupTopic(long gid, long tid) throws IOException, JSONException, KException {
  2376.         VKParams params = new VKParams("board.deleteTopic");
  2377.         params.put("group_id", gid);
  2378.         params.put("topic_id", tid);
  2379.         JSONObject root = sendRequest(params);
  2380.         int response = root.optInt("response");
  2381.         return response == 1;
  2382.     }
  2383.  
  2384.     /**
  2385.      * end topics region **
  2386.      */
  2387.  
  2388.     //http://vk.com/dev/groups.getById
  2389.     public ArrayList<VKGroup> getGroupsById(Collection<Long> uids, String domain, String fields) throws IOException, JSONException, KException {
  2390.         if (uids == null && domain == null)
  2391.             return null;
  2392.         if (uids.isEmpty() && domain == null)
  2393.             return null;
  2394.         VKParams params = new VKParams("groups.getById");
  2395.         String str_uids;
  2396.         if (uids != null && uids.size() > 0)
  2397.             str_uids = arrayToString(uids);
  2398.         else
  2399.             str_uids = domain;
  2400.         params.put("group_ids", str_uids);
  2401.         params.put("fields", fields);
  2402.         JSONObject root = sendRequest(params);
  2403.         JSONArray array = root.optJSONArray("response");
  2404.         return VKGroup.parseGroups(array);
  2405.     }
  2406.  
  2407.     //http://vk.com/dev/groups.join
  2408.     public String joinGroup(long gid, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  2409.         VKParams params = new VKParams("groups.join");
  2410.         params.put("group_id", gid);
  2411.         addCaptchaParams(captcha_key, captcha_sid, params);
  2412.         JSONObject root = sendRequest(params);
  2413.         Object response_code = root.opt("response");
  2414.         if (response_code != null)
  2415.             return String.valueOf(response_code);
  2416.         return null;
  2417.     }
  2418.  
  2419.     //http://vk.com/dev/groups.leave
  2420.     public String leaveGroup(long gid) throws IOException, JSONException, KException {
  2421.         VKParams params = new VKParams("groups.leave");
  2422.         params.put("group_id", gid);
  2423.         JSONObject root = sendRequest(params);
  2424.         Object response_code = root.opt("response");
  2425.         if (response_code != null)
  2426.             return String.valueOf(response_code);
  2427.         return null;
  2428.     }
  2429.  
  2430.     // http://vk.com/dev/groups.isMember
  2431.     public Boolean isGroupMember(long group_id, long user_id) throws JSONException, IOException, KException {
  2432.         VKParams params = new VKParams("groups.isMember");
  2433.         params.put("group_id", group_id);
  2434.         params.put("user_id", user_id);
  2435.         JSONObject root = sendRequest(params);
  2436.         Long response = root.optLong("response");
  2437.  
  2438.         return response == 1;
  2439.     }
  2440.  
  2441.     //http://vk.com/dev/groups.search
  2442.     public ArrayList<VKGroup> searchGroup(String q, Long count, Long offset) throws IOException, JSONException, KException {
  2443.         VKParams params = new VKParams("groups.search");
  2444.         params.put("q", q);
  2445.         params.put("count", count);
  2446.         params.put("offset", offset);
  2447.         JSONObject root = sendRequest(params);
  2448.         JSONObject response = root.optJSONObject("response");
  2449.         JSONArray array = response.optJSONArray("items");
  2450.         ArrayList<VKGroup> groups = new ArrayList<VKGroup>();
  2451.         //if there are no groups "response" will not be array
  2452.         if (array == null)
  2453.             return groups;
  2454.         groups = VKGroup.parseGroups(array);
  2455.         return groups;
  2456.     }
  2457.  
  2458.     //http://vk.com/dev/account.registerDevice
  2459.     public String registerDevice(String token, String device_model, String system_version, Integer no_text, String subscribe)
  2460.             throws IOException, JSONException, KException {
  2461.         VKParams params = new VKParams("account.registerDevice");
  2462.         params.put("token", token);
  2463.         params.put("device_model", device_model);
  2464.         params.put("system_version", system_version);
  2465.         params.put("no_text", no_text);
  2466.         params.put("subscribe", subscribe);
  2467.         //params.put("gcm", 1);
  2468.         JSONObject root = sendRequest(params);
  2469.         return root.getString("response");
  2470.     }
  2471.  
  2472.     //http://vk.com/dev/account.unregisterDevice
  2473.     public String unregisterDevice(String token) throws IOException, JSONException, KException {
  2474.         VKParams params = new VKParams("account.unregisterDevice");
  2475.         params.put("token", token);
  2476.         JSONObject root = sendRequest(params);
  2477.         return root.getString("response");
  2478.     }
  2479.  
  2480.     //http://vk.com/dev/notifications.get
  2481.     public VKNotifications getNotifications(String filters, Long start_time, Long end_time, Integer offset, Integer count) throws IOException, JSONException, KException {
  2482.         VKParams params = new VKParams("notifications.get");
  2483.         params.put("filters", filters);
  2484.         params.put("start_time", start_time);
  2485.         params.put("end_time", end_time);
  2486.         params.put("offset", offset);
  2487.         params.put("count", count);
  2488.         JSONObject root = sendRequest(params);
  2489.         JSONObject response = root.optJSONObject("response");
  2490.         return VKNotifications.parse(response);
  2491.     }
  2492.  
  2493.     //http://vk.com/dev/messages.getById
  2494.     public ArrayList<VKMessage> getMessagesById(ArrayList<Integer> message_ids) throws IOException, JSONException, KException {
  2495.         VKParams params = new VKParams("messages.getById");
  2496.         params.put("message_ids", arrayToString(message_ids));
  2497.         JSONObject root = sendRequest(params);
  2498.         JSONObject response = root.optJSONObject("response");
  2499.         JSONArray array = response.optJSONArray("items");
  2500.         return parseMessages(array);
  2501.     }
  2502.  
  2503.     //http://vk.com/dev/account.getCounters
  2504.     public Counters getCounters(String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  2505.         VKParams params = new VKParams("account.getCounters");
  2506.         addCaptchaParams(captcha_key, captcha_sid, params);
  2507.         JSONObject root = sendRequest(params);
  2508.         JSONObject response = root.optJSONObject("response");
  2509.         return Counters.parse(response);
  2510.     }
  2511.  
  2512.     /**
  2513.      * faves **
  2514.      */
  2515.     //http://vk.com/dev/fave.getUsers
  2516.     public ArrayList<VKFullUser> getFaveUsers(String fields, Integer count, Integer offset) throws IOException, JSONException, KException {
  2517.         VKParams params = new VKParams("fave.getUsers");
  2518.         params.put("fields", fields);
  2519.         params.put("count", count);
  2520.         params.put("offset", offset);
  2521.         JSONObject root = sendRequest(params);
  2522.         ArrayList<VKFullUser> users = new ArrayList<VKFullUser>();
  2523.         JSONObject response = root.optJSONObject("response");
  2524.         JSONArray array = response.optJSONArray("items");
  2525.         if (array == null)
  2526.             return users;
  2527.         int category_count = array.length();
  2528.         for (int i = 0; i < category_count; ++i) {
  2529.             JSONObject o = (JSONObject) array.get(i);
  2530.             VKFullUser u = VKFullUser.parseFromFave(o);
  2531.             users.add(u);
  2532.         }
  2533.         return users;
  2534.     }
  2535.  
  2536.     //http://vk.com/dev/fave.getPhotos
  2537.     public ArrayList<VKPhoto> getFavePhotos(Integer count, Integer offset) throws IOException, JSONException, KException {
  2538.         VKParams params = new VKParams("fave.getPhotos");
  2539.         params.put("count", count);
  2540.         params.put("offset", offset);
  2541.         JSONObject root = sendRequest(params);
  2542.         JSONObject response = root.optJSONObject("response");
  2543.         JSONArray array = response.optJSONArray("items");
  2544.         if (array == null)
  2545.             return new ArrayList<VKPhoto>();
  2546.         ArrayList<VKPhoto> photos = parsePhotos(array);
  2547.         return photos;
  2548.     }
  2549.  
  2550.     //http://vk.com/dev/fave.getVideos
  2551.     public ArrayList<VKVideo> getFaveVideos(Integer count, Integer offset) throws IOException, JSONException, KException {
  2552.         VKParams params = new VKParams("fave.getVideos");
  2553.         params.put("count", count);
  2554.         params.put("offset", offset);
  2555.         JSONObject root = sendRequest(params);
  2556.         JSONObject response = root.optJSONObject("response");
  2557.         JSONArray array = response.optJSONArray("items");
  2558.         ArrayList<VKVideo> videos = new ArrayList<VKVideo>();
  2559.         if (array != null) {
  2560.             for (int i = 0; i < array.length(); ++i) {
  2561.                 JSONObject o = (JSONObject) array.get(i);
  2562.                 VKVideo video = VKVideo.parse(o);
  2563.                 videos.add(video);
  2564.             }
  2565.         }
  2566.         return videos;
  2567.     }
  2568.  
  2569.     //http://vk.com/dev/fave.getPosts
  2570.     public ArrayList<VKWallMessage> getFavePosts(Integer count, Integer offset) throws IOException, JSONException, KException {
  2571.         VKParams params = new VKParams("fave.getPosts");
  2572.         //params.put("extended", extended);
  2573.         params.put("count", count);
  2574.         params.put("offset", offset);
  2575.         JSONObject root = sendRequest(params);
  2576.         JSONObject response = root.optJSONObject("response");
  2577.         JSONArray array = response.optJSONArray("items");
  2578.         //JSONArray array = response.optJSONArray("wall");
  2579.         //JSONArray profiles_array = response.optJSONArray("profiles");
  2580.         //JSONArray groups_array = response.optJSONArray("groups");
  2581.         ArrayList<VKWallMessage> wmessages = new ArrayList<VKWallMessage>();
  2582.         if (array == null)
  2583.             return wmessages;
  2584.         int category_count = array.length();
  2585.         for (int i = 0; i < category_count; ++i) {
  2586.             JSONObject o = (JSONObject) array.get(i);
  2587.             VKWallMessage wm = VKWallMessage.parse(o);
  2588.             wmessages.add(wm);
  2589.         }
  2590.         return wmessages;
  2591.     }
  2592.  
  2593.     //http://vk.com/dev/fave.getLinks
  2594.     public ArrayList<VKLink> getFaveLinks(Integer count, Integer offset) throws IOException, JSONException, KException {
  2595.         VKParams params = new VKParams("fave.getLinks");
  2596.         params.put("count", count);
  2597.         params.put("offset", offset);
  2598.         JSONObject root = sendRequest(params);
  2599.         ArrayList<VKLink> groups = new ArrayList<VKLink>();
  2600.         JSONObject response = root.optJSONObject("response");
  2601.         JSONArray array = response.optJSONArray("items");
  2602.         if (array == null)
  2603.             return groups;
  2604.         for (int i = 0; i < array.length(); i++) {
  2605.             JSONObject jlink = (JSONObject) array.get(i);
  2606.             VKLink link = VKLink.parse(jlink);
  2607.             groups.add(link);
  2608.         }
  2609.  
  2610.         return groups;
  2611.     }
  2612.     /*** end faves  ***/
  2613.  
  2614.     /**
  2615.      * chat methods **
  2616.      */
  2617.     //http://vk.com/dev/messages.createChat
  2618.     public Long сreateChat(ArrayList<Integer> uids, String title) throws IOException, JSONException, KException {
  2619.         if (uids == null || uids.size() == 0)
  2620.             return null;
  2621.         VKParams params = new VKParams("messages.createChat");
  2622.         String str_uids = String.valueOf(uids.get(0));
  2623.         for (int i = 1; i < uids.size(); i++)
  2624.             str_uids += "," + String.valueOf(uids.get(i));
  2625.         params.put("user_ids", str_uids);
  2626.         params.put("title", title);
  2627.         JSONObject root = sendRequest(params);
  2628.         return root.optLong("response");
  2629.     }
  2630.  
  2631.     //http://vk.com/dev/messages.editChat
  2632.     public Integer editChat(long chat_id, String title) throws IOException, JSONException, KException {
  2633.         VKParams params = new VKParams("messages.editChat");
  2634.         params.put("chat_id", chat_id);
  2635.         params.put("title", title);
  2636.         JSONObject root = sendRequest(params);
  2637.         return root.optInt("response");
  2638.     }
  2639.  
  2640.     //http://vk.com/dev/messages.getChatUsers
  2641.     public ArrayList<VKFullUser> getChatUsers(long chat_id, String fields) throws IOException, JSONException, KException {
  2642.         VKParams params = new VKParams("messages.getChatUsers");
  2643.         params.put("chat_id", chat_id);
  2644.         params.put("fields", fields);
  2645.         JSONObject root = sendRequest(params);
  2646.         JSONArray array = root.optJSONArray("response");
  2647.         return VKFullUser.parseUsers(array);
  2648.     }
  2649.  
  2650.     // https://vk.com/dev/messages.getChat
  2651.     public VKChat getChat(long chat_id) throws JSONException, IOException, KException {
  2652.         VKParams params = new VKParams("messages.getChat");
  2653.         params.put("chat_id", chat_id);
  2654.         JSONObject root = sendRequest(params);
  2655.         JSONObject response = root.optJSONObject("response");
  2656.         return VKChat.parse(response);
  2657.     }
  2658.  
  2659.     //http://vk.com/dev/messages.addChatUser
  2660.     public Integer addUserToChat(long chat_id, long uid) throws IOException, JSONException, KException {
  2661.         VKParams params = new VKParams("messages.addChatUser");
  2662.         params.put("chat_id", chat_id);
  2663.         params.put("user_id", uid);
  2664.         JSONObject root = sendRequest(params);
  2665.         return root.optInt("response");
  2666.     }
  2667.  
  2668.     //http://vk.com/dev/messages.removeChatUser
  2669.     public Integer removeUserFromChat(long chat_id, long uid) throws IOException, JSONException, KException {
  2670.         VKParams params = new VKParams("messages.removeChatUser");
  2671.         params.put("chat_id", chat_id);
  2672.         params.put("user_id", uid);
  2673.         JSONObject root = sendRequest(params);
  2674.         return root.optInt("response");
  2675.     }
  2676.  
  2677.     /**
  2678.      * end chat methods **
  2679.      */
  2680.  
  2681.     //http://vk.com/dev/friends.getSuggestions
  2682.     public ArrayList<VKUser> getSuggestions(String filter, String fields) throws IOException, JSONException, KException {
  2683.         VKParams params = new VKParams("friends.getSuggestions");
  2684.         params.put("filter", filter);   //mutual, contacts, mutual_contacts
  2685.         params.put("fields", fields);
  2686.         JSONObject root = sendRequest(params);
  2687.         JSONObject response = root.optJSONObject("response");
  2688.         JSONArray array = response.optJSONArray("items");
  2689.         return VKUser.parseUsers(array);
  2690.     }
  2691.  
  2692.     //http://vk.com/dev/account.importContacts
  2693.     @Deprecated
  2694.     public Integer importContacts(Collection<String> contacts) throws IOException, JSONException, KException {
  2695.         VKParams params = new VKParams("account.importContacts");
  2696.         params.put("contacts", arrayToString(contacts));
  2697.         JSONObject root = sendRequest(params);
  2698.         return root.optInt("response");
  2699.     }
  2700.  
  2701.     //http://vk.com/dev/friends.getByPhones
  2702.     public ArrayList<VKFullUser> getFriendsByPhones(ArrayList<String> phones, String fields) throws IOException, JSONException, KException {
  2703.         VKParams params = new VKParams("friends.getByPhones");
  2704.         params.put("phones", arrayToString(phones));
  2705.         params.put("fields", fields);
  2706.         JSONObject root = sendRequest(params);
  2707.         JSONArray array = root.optJSONArray("response");
  2708.         return VKFullUser.parseUsersForGetByPhones(array);
  2709.     }
  2710.  
  2711.     /**
  2712.      * methods for messages search **
  2713.      */
  2714.     //http://vk.com/dev/messages.search
  2715.     public ArrayList<VKMessage> searchMessages(String q, int offset, int count, Integer preview_length) throws IOException, JSONException, KException {
  2716.         VKParams params = new VKParams("messages.search");
  2717.         params.put("q", q);
  2718.         params.put("count", count);
  2719.         params.put("offset", offset);
  2720.         params.put("preview_length", preview_length);
  2721.         JSONObject root = sendRequest(params);
  2722.         JSONObject response = root.optJSONObject("response");
  2723.         JSONArray array = response.optJSONArray("items");
  2724.         return VKMessage.parseArray(array);
  2725.     }
  2726.  
  2727.     //http://vk.com/dev/messages.searchDialogs
  2728.     public ArrayList<SearchDialogItem> searchDialogs(String q, String fields, Integer limit) throws IOException, JSONException, KException {
  2729.         VKParams params = new VKParams("messages.searchDialogs");
  2730.         params.put("q", q);
  2731.         params.put("fields", fields);
  2732.         params.put("limit", limit);
  2733.         JSONObject root = sendRequest(params);
  2734.         JSONArray array = root.optJSONArray("response");
  2735.         return VKMessage.parseSearchedDialogs(array);
  2736.     }
  2737.  
  2738.     //http://vk.com/dev/messages.getLastActivity
  2739.     public VKFullUser.LastActivity getLastActivity(long user_id) throws JSONException, IOException, KException {
  2740.         VKParams params = new VKParams("messages.getLastActivity");
  2741.         params.put("user_id", user_id);
  2742.         JSONObject root = sendRequest(params);
  2743.         JSONObject response = root.optJSONObject("response");
  2744.         return VKFullUser.LastActivity.parse(response);
  2745.     }
  2746.  
  2747.     //http://vk.com/dev/groups.getMembers
  2748.     public ArrayList<Long> getGroupsMembers(long gid, Integer count, Integer offset, String sort) throws IOException, JSONException, KException {
  2749.         VKParams params = new VKParams("groups.getMembers");
  2750.         params.put("group_id", gid);
  2751.         params.put("count", count);
  2752.         params.put("offset", offset);
  2753.         params.put("sort", sort); //id_asc, id_desc, time_asc, time_desc
  2754.         JSONObject root = sendRequest(params);
  2755.         JSONObject response = root.getJSONObject("response");
  2756.         JSONArray array = response.optJSONArray("items");
  2757.         ArrayList<Long> users = new ArrayList<Long>();
  2758.         if (array != null) {
  2759.             int category_count = array.length();
  2760.             for (int i = 0; i < category_count; i++) {
  2761.                 Long id = array.optLong(i, -1);
  2762.                 if (id != -1)
  2763.                     users.add(id);
  2764.             }
  2765.         }
  2766.         return users;
  2767.     }
  2768.  
  2769.     public ArrayList<VKFullUser> getGroupsMembersWithExecute(long gid, Integer count, Integer offset, String sort, String fields) throws IOException, JSONException, KException {
  2770.         //String code = "return API.users.get({\"user_ids\":API.groups.getMembers({\"gid\":" + String.valueOf(gid) + ",\"count\":" + String.valueOf(count) + ",\"offset\":" + String.valueOf(offset) + ",\"sort\":\"id_asc\"}),\"fields\":\"" + fields + "\"});";
  2771.         String code = "var members=API.groups.getMembers({\"gid\":" + gid + "}); var u=members[1]; return API.users.get({\"user_ids\":u,\"fields\":\"" + fields + "\"});";
  2772.         VKParams params = new VKParams("execute");
  2773.         params.put("code", code);
  2774.         JSONObject root = sendRequest(params);
  2775.         JSONArray array = root.optJSONArray("response");
  2776.         return VKFullUser.parseUsers(array);
  2777.     }
  2778.  
  2779.     //http://vk.com/dev/utils.getServerTime
  2780.     public long getServerTime() throws IOException, JSONException, KException {
  2781.         VKParams params = new VKParams("utils.getServerTime");
  2782.         JSONObject root = sendRequest(params);
  2783.         return root.getLong("response");
  2784.     }
  2785.  
  2786.     //http://vk.com/dev/audio.getAlbums
  2787.     public ArrayList<AudioAlbum> getAudioAlbums(Long owner_id, Integer offset, Integer count) throws IOException, JSONException, KException {
  2788.         VKParams params = new VKParams("audio.getAlbums");
  2789.         params.put("owner_id", owner_id);
  2790.         params.put("count", count);
  2791.         params.put("offset", offset);
  2792.         JSONObject root = sendRequest(params);
  2793.         JSONObject response = root.optJSONObject("response");
  2794.         JSONArray array = response.optJSONArray("items");
  2795.         ArrayList<AudioAlbum> albums = AudioAlbum.parseAlbums(array);
  2796.         return albums;
  2797.     }
  2798.  
  2799.     //http://vk.com/dev/audio.getRecommendations
  2800.     public ArrayList<VKAudio> getAudioRecommendations() throws IOException, JSONException, KException {
  2801.         VKParams params = new VKParams("audio.getRecommendations");
  2802.         JSONObject root = sendRequest(params);
  2803.         JSONObject response = root.optJSONObject("response");
  2804.         JSONArray array = response.optJSONArray("items");
  2805.         return parseAudioList(array);
  2806.     }
  2807.  
  2808.     //http://vk.com/dev/audio.getPopular
  2809.     public ArrayList<VKAudio> getAudioPopular() throws IOException, JSONException, KException {
  2810.         VKParams params = new VKParams("audio.getPopular");
  2811.         //params.put("only_eng", only_eng);
  2812.         //params.put("genre_id", genre_id);
  2813.         //params.put("count", count);
  2814.         //params.put("offset", offset);
  2815.         JSONObject root = sendRequest(params);
  2816.         JSONArray array = root.optJSONArray("response");
  2817.         return parseAudioList(array);
  2818.     }
  2819.  
  2820.     //http://vk.com/dev/video.getAlbums
  2821.     public ArrayList<AudioAlbum> getVideoAlbums(long owner_id, Integer offset, Integer count) throws IOException, JSONException, KException {
  2822.         VKParams params = new VKParams("video.getAlbums");
  2823.         params.put("owner_id", owner_id);
  2824.         params.put("count", count);
  2825.         params.put("offset", offset);
  2826.         JSONObject root = sendRequest(params);
  2827.         JSONObject response = root.optJSONObject("response");
  2828.         JSONArray array = response.optJSONArray("items");
  2829.         ArrayList<AudioAlbum> albums = AudioAlbum.parseAlbums(array);
  2830.         return albums;
  2831.     }
  2832.  
  2833.  
  2834.     //http://vk.com/dev/wall.edit
  2835.     public int editWallPost(long owner_id, long post_id, String text, Collection<String> attachments, String lat, String lon, long place_id, Long publish_date, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  2836.         VKParams params = new VKParams("wall.edit");
  2837.         params.put("owner_id", owner_id);
  2838.         params.put("post_id", post_id);
  2839.         params.put("message", text);
  2840.         params.put("attachments", arrayToString(attachments));
  2841.         params.put("lat", lat);
  2842.         params.put("long", lon);
  2843.         params.put("place_id", place_id);
  2844.         params.put("publish_date", publish_date);
  2845.         addCaptchaParams(captcha_key, captcha_sid, params);
  2846.         JSONObject root = sendRequest(params, true);
  2847.         return root.optInt("response");
  2848.     }
  2849.  
  2850.     //http://vk.com/dev/photos.edit
  2851.     public Integer photoEdit(Long owner_id, long pid, String caption) throws IOException, JSONException, KException {
  2852.         VKParams params = new VKParams("photos.edit");
  2853.         params.put("owner_id", owner_id);
  2854.         params.put("photo_id", pid);
  2855.         params.put("caption", caption);
  2856.         JSONObject root = sendRequest(params);
  2857.         return root.optInt("response");
  2858.     }
  2859.  
  2860.     //http://vk.com/dev/docs.get
  2861.     public ArrayList<VKDocument> getDocs(Integer owner_id, Integer count, Long offset) throws IOException, JSONException, KException {
  2862.         VKParams params = new VKParams("docs.get");
  2863.         params.put("owner_id", owner_id);
  2864.         params.put("count", count);
  2865.         params.put("offset", offset);
  2866.         JSONObject root = sendRequest(params);
  2867.         JSONObject response = root.optJSONObject("response");
  2868.         JSONArray array = response.optJSONArray("items");
  2869.         return VKDocument.parseDocs(array);
  2870.     }
  2871.  
  2872.     //http://vk.com/dev/docs.getUploadServer
  2873.     public String getDocsUploadServer() throws IOException, JSONException, KException {
  2874.         VKParams params = new VKParams("docs.getUploadServer");
  2875.         JSONObject root = sendRequest(params);
  2876.         JSONObject response = root.getJSONObject("response");
  2877.         return response.getString("upload_url");
  2878.     }
  2879.  
  2880.     //http://vk.com/dev/docs.save
  2881.     public VKDocument saveDoc(String file, String title) throws IOException, JSONException, KException {
  2882.         VKParams params = new VKParams("docs.save");
  2883.         params.put("file", file);
  2884.         JSONObject root = sendRequest(params);
  2885.         JSONArray array = root.getJSONArray("response");
  2886.         ArrayList<VKDocument> docs = VKDocument.parseDocs(array);
  2887.         return docs.get(0);
  2888.     }
  2889.  
  2890.     //http://vk.com/dev/docs.delete
  2891.     public Boolean deleteDoc(Long doc_id, long owner_id) throws IOException, JSONException, KException {
  2892.         VKParams params = new VKParams("docs.delete");
  2893.         params.put("owner_id", owner_id);
  2894.         params.put("doc_id", doc_id);
  2895.         JSONObject root = sendRequest(params);
  2896.         int response = root.optInt("response");
  2897.         return response == 1;
  2898.     }
  2899.  
  2900.     //http://vk.com/dev/docs.edit
  2901.     public Boolean editDoc(long doc_id, long owner_id, String title) throws IOException, JSONException, KException {
  2902.         VKParams params = new VKParams("docs.edit");
  2903.         params.put("owner_id", owner_id);
  2904.         params.put("doc_id", doc_id);
  2905.         params.put("title", title);
  2906.         JSONObject root = sendRequest(params);
  2907.         int response = root.optInt("response");
  2908.         return response == 1;
  2909.     }
  2910.  
  2911.     //http://vk.com/dev/notifications.markAsViewed
  2912.     public Boolean markNotificationsAsViewed() throws IOException, JSONException, KException {
  2913.         VKParams params = new VKParams("notifications.markAsViewed");
  2914.         JSONObject root = sendRequest(params);
  2915.         int response = root.optInt("response");
  2916.         return response == 1;
  2917.     }
  2918.  
  2919.     //http://vk.com/dev/newsfeed.getBanned
  2920.     public BannArg getBanned(boolean is_extended, String fields) throws IOException, JSONException, KException {
  2921.         VKParams params = new VKParams("newsfeed.getBanned");
  2922.         if (is_extended)
  2923.             params.put("extended", "1");
  2924.         params.put("fields", fields);
  2925.         JSONObject root = sendRequest(params);
  2926.         JSONObject object = root.optJSONObject("response");
  2927.         return BannArg.parse(object, is_extended);
  2928.     }
  2929.  
  2930.     //http://vk.com/dev/newsfeed.addBan
  2931.     public Boolean addBan(Collection<Long> uids, Collection<Long> gids) throws IOException, JSONException, KException {
  2932.         VKParams params = new VKParams("newsfeed.addBan");
  2933.         if (uids != null && uids.size() > 0)
  2934.             params.put("uids", arrayToString(uids));
  2935.         if (gids != null && gids.size() > 0)
  2936.             params.put("gids", arrayToString(gids));
  2937.         JSONObject root = sendRequest(params);
  2938.         int response = root.optInt("response");
  2939.         return response == 1;
  2940.     }
  2941.  
  2942.     //http://vk.com/dev/newsfeed.deleteBan
  2943.     public Boolean deleteBan(Collection<Long> uids, Collection<Long> gids) throws IOException, JSONException, KException {
  2944.         VKParams params = new VKParams("newsfeed.deleteBan");
  2945.         if (uids != null && uids.size() > 0)
  2946.             params.put("uids", arrayToString(uids));
  2947.         if (gids != null && gids.size() > 0)
  2948.             params.put("gids", arrayToString(gids));
  2949.         JSONObject root = sendRequest(params);
  2950.         int response = root.optInt("response");
  2951.         return response == 1;
  2952.     }
  2953.  
  2954.     //http://vk.com/dev/audio.getBroadcast
  2955.     //gets status of broadcasting user current audio to his page
  2956.     @Deprecated
  2957.     public boolean audioGetBroadcast() throws IOException, JSONException, KException {
  2958.         VKParams params = new VKParams("audio.getBroadcast");
  2959.         // метод устаревший, можно передавать старую версию 4.98 если перестанет работать. Замены ему пока нет http://vk.com/bugs?act=show&id=4717174_23
  2960.         // TODO: Теперь вместо него следует использовать status.get
  2961.         JSONObject root = sendRequest(params);
  2962.         JSONObject response = root.optJSONObject("response");
  2963.         return response.optInt("enabled") == 1;
  2964.     }
  2965.  
  2966.     //http://vk.com/dev/audio.setBroadcast
  2967.     public boolean audioSetBroadcast(String audio, String target_ids) throws IOException, JSONException, KException {
  2968.         VKParams params = new VKParams("audio.setBroadcast");
  2969.         params.put("audio", audio);
  2970.         params.put("target_ids", target_ids);
  2971.         sendRequest(params);
  2972.         //В случае успешного выполнения возвращает массив идентификаторов сообществ и пользователя, которым был установлен или удален аудиостатус.
  2973.         //response: [1661530]
  2974.         //нет необходимости парсить пока
  2975.         return true;
  2976.     }
  2977.  
  2978.     //http://vk.com/dev/audio.addAlbum
  2979.     public Long addAudioAlbum(String title, Long gid) throws IOException, JSONException, KException {
  2980.         VKParams params = new VKParams("audio.addAlbum");
  2981.         params.put("title", title);
  2982.         params.put("group_id", gid);
  2983.         JSONObject root = sendRequest(params);
  2984.         JSONObject obj = root.getJSONObject("response");
  2985.         return obj.optLong("album_id");
  2986.     }
  2987.  
  2988.     //http://vk.com/dev/audio.editAlbum
  2989.     public Integer editAudioAlbum(String title, long album_id, Long gid) throws IOException, JSONException, KException {
  2990.         VKParams params = new VKParams("audio.editAlbum");
  2991.         params.put("title", title);
  2992.         params.put("album_id", album_id);
  2993.         params.put("group_id", gid);
  2994.         JSONObject root = sendRequest(params);
  2995.         return root.optInt("response");
  2996.     }
  2997.  
  2998.     //http://vk.com/dev/audio.deleteAlbum
  2999.     public Integer deleteAudioAlbum(long album_id, Long gid) throws IOException, JSONException, KException {
  3000.         VKParams params = new VKParams("audio.deleteAlbum");
  3001.         params.put("album_id", album_id);
  3002.         params.put("group_id", gid);
  3003.         JSONObject root = sendRequest(params);
  3004.         return root.optInt("response");
  3005.     }
  3006.  
  3007.     //http://vk.com/dev/audio.moveToAlbum
  3008.     public Integer moveToAudioAlbum(Collection<Long> aids, long album_id, Long gid) throws IOException, JSONException, KException {
  3009.         VKParams params = new VKParams("audio.moveToAlbum");
  3010.         params.put("audio_ids", arrayToString(aids));
  3011.         params.put("album_ids", arrayToString(aids));//album_ids instead audio_ids - Баг в API
  3012.         params.put("album_id", album_id);
  3013.         params.put("group_id", gid);
  3014.         JSONObject root = sendRequest(params);
  3015.         return root.optInt("response");
  3016.     }
  3017.  
  3018.     //http://vk.com/dev/wall.getById
  3019.     public ArrayList<VKWallMessage> getWallMessageById(ArrayList<String> posts) throws IOException, JSONException, KException {
  3020.         VKParams params = new VKParams("wall.getById");
  3021.         params.put("posts", arrayToString(posts));
  3022.         JSONObject root = sendRequest(params);
  3023.         JSONArray array = root.getJSONArray("response");
  3024.         ArrayList<VKWallMessage> wmessages = new ArrayList<VKWallMessage>();
  3025.         int category_count = array.length();
  3026.         for (int i = 0; i < category_count; ++i) {
  3027.             JSONObject o = (JSONObject) array.get(i);
  3028.             VKWallMessage wm = VKWallMessage.parse(o);
  3029.             wmessages.add(wm);
  3030.         }
  3031.         return wmessages;
  3032.     }
  3033.  
  3034.     //http://vk.com/dev/newsfeed.unsubscribe
  3035.     public Integer unsubscribeMewsfeed(String type, Long owner_id, Long item_id) throws IOException, JSONException, KException {
  3036.         VKParams params = new VKParams("newsfeed.unsubscribe");
  3037.         params.put("type", type);
  3038.         params.put("owner_id", owner_id);
  3039.         params.put("item_id", item_id);
  3040.         JSONObject root = sendRequest(params);
  3041.         return root.optInt("response");
  3042.     }
  3043.  
  3044.     //http://vk.com/dev/account.getBanned
  3045.     public ArrayList<VKFullUser> getBlackList(Long offset, Long count) throws IOException, JSONException, KException {
  3046.         VKParams params = new VKParams("account.getBanned");
  3047.         String fields = "first_name,last_name,photo_100,online";
  3048.         params.put("fields", fields);
  3049.         params.put("offset", offset);
  3050.         params.put("count", count);
  3051.         JSONObject root = sendRequest(params);
  3052.         JSONObject response = root.optJSONObject("response");
  3053.         JSONArray array = response.optJSONArray("items");
  3054.         return VKFullUser.parseUsers(array);
  3055.     }
  3056.  
  3057.     //http://vk.com/dev/account.banUser
  3058.     public Boolean addToBlackList(long uid) throws IOException, JSONException, KException {
  3059.         VKParams params = new VKParams("account.banUser");
  3060.         params.put("user_id", uid);
  3061.         JSONObject root = sendRequest(params);
  3062.         int response = root.optInt("response");
  3063.         return response == 1;
  3064.     }
  3065.  
  3066.     //http://vk.com/dev/account.unbanUser
  3067.     public Boolean deleteFromBlackList(long uid) throws IOException, JSONException, KException {
  3068.         VKParams params = new VKParams("account.unbanUser");
  3069.         params.put("user_id", uid);
  3070.         JSONObject root = sendRequest(params);
  3071.         int response = root.optInt("response");
  3072.         return response == 1;
  3073.     }
  3074.  
  3075.     //http://vk.com/dev/docs.add
  3076.     public Long addDoc(long owner_id, long document_id, String access_key) throws IOException, JSONException, KException {
  3077.         VKParams params = new VKParams("docs.add");
  3078.         params.put("doc_id", document_id);
  3079.         params.put("owner_id", owner_id);
  3080.         params.put("access_key", access_key);
  3081.         JSONObject root = sendRequest(params);
  3082.         //returns new document_id
  3083.         return root.optLong("response");
  3084.     }
  3085.  
  3086.     //http://vk.com/dev/newsfeed.search
  3087.     public Newsfeed searchNews(String q, String start_id, int extended, Long start_time, Long end_time, long count, Long offset, double latitude, double longitude, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  3088.         VKParams params = new VKParams("newsfeed.search");
  3089.         params.put("q", q);
  3090.         params.put("start_id", start_id);
  3091.         params.put("extended", extended);
  3092.         params.put("start_time", start_time);
  3093.         params.put("end_time", end_time);
  3094.         if (count != 0)
  3095.             params.put("count", count);
  3096.         params.put("offset", offset);
  3097.         if (latitude != 0)
  3098.             params.putDouble("latitude", latitude);
  3099.         if (longitude != 0)
  3100.             params.putDouble("longitude", longitude);
  3101.         addCaptchaParams(captcha_key, captcha_sid, params);
  3102.         JSONObject root = sendRequest(params);
  3103.         return Newsfeed.parseFromSearch(root);
  3104.     }
  3105.  
  3106.     //http://vk.com/dev/groups.getBanned
  3107.     public ArrayList<GroupBanItem> getGroupBannedUsers(long group_id, Long offset, Long count) throws IOException, JSONException, KException {
  3108.         VKParams params = new VKParams("groups.getBanned");
  3109.         params.put("group_id", group_id);
  3110.         params.put("offset", offset);
  3111.         params.put("count", count);
  3112.         //not documented
  3113.         params.put("fields", "photo_100");
  3114.         //
  3115.         JSONObject root = sendRequest(params);
  3116.         JSONObject response = root.optJSONObject("response");
  3117.         JSONArray array = response.optJSONArray("items");
  3118.         return GroupBanItem.parseAll(array);
  3119.     }
  3120.  
  3121.     //http://vk.com/dev/groups.banUser
  3122.     public Boolean addGroupBanUser(long group_id, long user_id, Long end_date, Integer reason, String comment, boolean comment_visible) throws IOException, JSONException, KException {
  3123.         VKParams params = new VKParams("groups.banUser");
  3124.         params.put("group_id", group_id);
  3125.         params.put("user_id", user_id);
  3126.         params.put("end_date", end_date);
  3127.         params.put("reason", reason);
  3128.         params.put("comment", comment);
  3129.         if (comment_visible)
  3130.             params.put("comment_visible", "1");
  3131.         JSONObject root = sendRequest(params);
  3132.         int response = root.optInt("response");
  3133.         return response == 1;
  3134.     }
  3135.  
  3136.     //http://vk.com/dev/groups.unbanUser
  3137.     public Boolean deleteGroupBanUser(long group_id, long user_id) throws IOException, JSONException, KException {
  3138.         VKParams params = new VKParams("groups.unbanUser");
  3139.         params.put("group_id", group_id);
  3140.         params.put("user_id", user_id);
  3141.         JSONObject root = sendRequest(params);
  3142.         int response = root.optInt("response");
  3143.         return response == 1;
  3144.     }
  3145.  
  3146.     //http://vk.com/dev/photos.copy
  3147.     public Long copyPhoto(long owner_id, long photo_id, String access_key) throws IOException, JSONException, KException {
  3148.         VKParams params = new VKParams("photos.copy");
  3149.         params.put("owner_id", owner_id);
  3150.         params.put("photo_id", photo_id);
  3151.         params.put("access_key", access_key);
  3152.         JSONObject root = sendRequest(params);
  3153.         Long response = root.optLong("response");
  3154.         return response;
  3155.     }
  3156.  
  3157.     //http://vk.com/dev/account.setOffline
  3158.     public Long setOffline() {
  3159.         try {
  3160.             VKParams params = new VKParams("account.setOffline");
  3161.             JSONObject root = sendRequest(params);
  3162.             return root.optLong("response");
  3163.         } catch (Exception e) {
  3164.             e.printStackTrace();
  3165.             return null;
  3166.         }
  3167.  
  3168.     }
  3169.     //http://vk.com/dev/groups.getInvites
  3170.     public ArrayList<VKGroup> getGroupsInvites(Long offset, Long count) throws IOException, JSONException, KException {
  3171.         VKParams params = new VKParams("groups.getInvites");
  3172.         params.put("offset", offset);
  3173.         params.put("count", count);
  3174.         JSONObject root = sendRequest(params);
  3175.         ArrayList<VKGroup> groups = new ArrayList<VKGroup>();
  3176.         JSONObject response = root.optJSONObject("response");
  3177.         JSONArray array = response.optJSONArray("items");
  3178.         //if there are no groups "response" will not be array
  3179.         if (array == null)
  3180.             return groups;
  3181.         groups = VKGroup.parseGroups(array);
  3182.         return groups;
  3183.     }
  3184.  
  3185.     //http://vk.com/dev/audio.edit
  3186.     public Long editAudio(long owner_id, long audio_id, String artist, String title, String text, Integer genre_id, Integer no_search, String captcha_key, String captcha_sid) throws IOException, JSONException, KException {
  3187.         VKParams params = new VKParams("audio.edit");
  3188.         params.put("owner_id", owner_id);
  3189.         params.put("audio_id", audio_id);
  3190.         params.put("artist", artist);
  3191.         params.put("title", title);
  3192.         params.put("text", text);
  3193.         params.put("genre_id", genre_id);
  3194.         params.put("no_search", no_search);
  3195.         addCaptchaParams(captcha_key, captcha_sid, params);
  3196.         JSONObject root = sendRequest(params, true);
  3197.         Long lyrics_id = root.optLong("response");
  3198.         return lyrics_id;
  3199.     }
  3200.  
  3201.     // http://vk.com/dev/stats.trackVisitor
  3202.     public Long trackStatsVisitor() throws JSONException, IOException, KException {
  3203.         VKParams params = new VKParams("stats.trackVisitor");
  3204.         JSONObject root = sendRequest(params);
  3205.         return root.optLong("response");
  3206.     }
  3207.  
  3208.     // https://vk.com/dev/storage.set
  3209.     public Long setStorage(String key, String value) throws JSONException, IOException, KException {
  3210.         VKParams params = new VKParams("storage.set");
  3211.         params.put("key", key);
  3212.         params.put("value", value);
  3213.         params.put("global", "1");
  3214.  
  3215.         JSONObject root = sendRequest(params);
  3216.         return root.optLong("response");
  3217.     }
  3218.  
  3219.     // http://vk.com/dev/storage.get
  3220.     public String getStorage(String key) throws JSONException, IOException, KException {
  3221.         VKParams params = new VKParams("storage.get");
  3222.         params.put("key", key);
  3223.         params.put("global", "1");
  3224.  
  3225.         JSONObject root = sendRequest(params); // отправляем запрос, и парсим полученный обьект
  3226.         return root.optString("response");
  3227.     }
  3228.  
  3229.     // http://vk.com/dev/gifts.get
  3230.     public ArrayList<VKGift> getGifts(long user_id, int count, int offset) throws JSONException, IOException, KException {
  3231.         VKParams params = new VKParams("gifts.get");
  3232.         params.put("user_id", user_id);
  3233.         params.put("count", count);
  3234.         params.put("offset", offset);
  3235.  
  3236.         JSONObject root = sendRequest(params);
  3237.         JSONObject response = root.optJSONObject("response");
  3238.         return VKGift.parseGifts(response.optJSONArray("items"));
  3239.     }
  3240.  
  3241.     /**
  3242.      * Отправка подарка
  3243.      * TODO: Данный метод был удален из документации, однако он работает
  3244.      * UPD: Не работает,  Access denied: method allowed only for official app
  3245.      *
  3246.      * @param user_ids  IDs пользователей, которым отправляются подарки
  3247.      * @param message   текст сообщения
  3248.      * @param isPrivate - флаг, отвечающий за тип подарка: публичный\приватный
  3249.      * @param gift_id   подарка, можно его достать из m.vk.com версии.  ID подарка будет написан в адресной строке
  3250.      * @param guid      уникальный индентификатор, чтобы предотвратить повторную отправку. - любое число
  3251.      *                  <p/>
  3252.      *                  В случае недостатка голосов - вернет ошибку "Not enough money on owner's balance"
  3253.      */
  3254.     // https://vk.com/dev/gifts.send
  3255.     public void sendGift(Collection<Integer> user_ids, String message, boolean isPrivate, int gift_id, int guid) throws IOException, JSONException, KException {
  3256.         VKParams params = new VKParams("gifts.send");
  3257.         params.put("user_ids", arrayToString(user_ids));
  3258.         params.put("gift_id", gift_id);
  3259.         params.put("private", isPrivate);
  3260.         params.put("message", message);
  3261.         params.put("guid", guid);
  3262.         sendRequest(params);
  3263.     }
  3264.  
  3265.     // http://vk.com/dev/utils.resolveScreenName
  3266.     public VKResolveScreenName utilsResolveScreenName(String screen_name) throws IOException, JSONException, KException {
  3267.         VKParams params = new VKParams("utils.resolveScreenName");
  3268.         params.put("screen_name", screen_name);
  3269.  
  3270.         JSONObject root = sendRequest(params);
  3271.         JSONObject response = root.optJSONObject("response");
  3272.  
  3273.         return VKResolveScreenName.parse(response);
  3274.     }
  3275.  
  3276.     /**
  3277.      * Прямая авторизация с помощью Офф клиента вк для Андроид
  3278.      *
  3279.      * @param login
  3280.      * @param password
  3281.      * @return JSONObject
  3282.      */
  3283.     // http://vk.com/dev/auth_direct
  3284.     public JSONObject directauthorizationWithOff(String login, String password) throws IOException, JSONException {
  3285.         String client_secret = "hHbZxrka2uZ6jB1inYsH";
  3286.         String client_id = "2274003";
  3287.  
  3288.         String url = "https://oauth.vk.com/token?grant_type=password" +
  3289.                 "&client_id=" + client_id +
  3290.                 "&client_secret=" + client_secret +
  3291.                 "&username=" + login +
  3292.                 "&password=" + password +
  3293.                 "&scope=" + Auth.getSettings() +
  3294.                 "&v=" + API_VERSION;
  3295.  
  3296.         return new JSONObject(Api.sendRequestInternal(url, "", true));
  3297.     }
  3298.  
  3299.  
  3300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement