public VKRequest getById(VKParameters params) { if (params.containsKey(EXTENDED) && ((Integer) params.get(EXTENDED)) == 1) { return prepareRequest("get", params, VKRequest.HttpMethod.GET, VKPostArray.class); } else { return prepareRequest("get", params); } }
public void SearchFiles(String searchName) { try { VKParameters params = new VKParameters(); params.put(VKApiConst.Q, searchName); params.put(VKApiConst.COUNT, "200"); VKRequest request = VKApi.audio().search(params); request.executeWithListener( new VKRequest.VKRequestListener() { @Override public void onComplete(VKResponse response) { super.onComplete(response); try { searchFiles.clear(); VkAudioArray audioArray = new VkAudioArray(); audioArray.parse(response.json); for (int i = 0; i < audioArray.size(); i++) searchFiles.add( new MusicFile( audioArray.get(i).id, audioArray.get(i).owner_id, audioArray.get(i).artist, audioArray.get(i).title, audioArray.get(i).duration, audioArray.get(i).url, audioArray.get(i).genre)); searchAdapter.notifyDataSetChanged(); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onError(VKError error) { super.onError(error); } }); } catch (NullPointerException e) { Toast.makeText( getApplicationContext(), "Please authrorize app in settings", Toast.LENGTH_LONG) .show(); e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "Error with getting list", Toast.LENGTH_LONG).show(); } }
public void requestFriends() { VKRequest request = VKApi.friends() .get( VKParameters.from( VKApiConst.OFFSET, "1", VKApiConst.FIELDS, "first_name,photo_100,photo_200", VKApiConst.VERSION, "5.37")); request.executeWithListener( new VKRequest.VKRequestListener() { @Override public void onComplete(VKResponse response) { parseFriends(response); for (int i = 0; i < observerList.size(); i++) { observerList.get(i).update(); } } @Override public void attemptFailed(VKRequest request, int attemptNumber, int totalAttempts) { super.attemptFailed(request, attemptNumber, totalAttempts); } @Override public void onError(VKError error) { super.onError(error); } }); }
@OnClick(R.id.post_share_button) void share() { VKRequest request = VKApi.wall() .repost( VKParameters.from( VKApiConst.OBJECT, "wall" + String.valueOf(mPost.source_id) + "_" + String.valueOf(mPost.post_id))); request.useSystemLanguage = false; request = VKRequest.getRegisteredRequest(request.registerObject()); if (request != null) { request.unregisterObject(); } request.executeWithListener( new VKRequestListener() { @Override public void onComplete(VKResponse response) { mPost.reposts_count += 1; postCountOfShare.setText(String.valueOf(mPost.reposts_count)); mPost.user_reposted = true; } @Override public void onError(VKError error) { Log.d("Error", error.toString()); } }); }
private void updateVideoList() { VKRequest videoRequest = VKApi.video() .get( VKParameters.from( VKApiConst.OWNER_ID, ownerId, VKApiConst.ALBUM_ID, albumId)); videoRequest.executeWithListener( new VKRequest.VKRequestListener() { @Override public void onError(VKError error) { super.onError(error); if (error.errorCode == -105) { swipeRefresh.setRefreshing(false); progressBar.setVisibility(View.GONE); if (videoList.isEmpty()) { noConnectionView.setVisibility(View.VISIBLE); } else if (videoList.size() > 0) { Snackbar.make( rootView, getString(R.string.no_connection_snack_message), Snackbar.LENGTH_LONG) .setAction( R.string.no_connection_snack_button, view -> { updateVideoList(); }) .show(); } } else if (error.errorCode == -101) { progressBar.setVisibility(View.GONE); swipeRefresh.setRefreshing(false); noVideosView.setText(R.string.no_added_videos); noVideosView.setVisibility(View.VISIBLE); noVideos = true; } } @Override public void onComplete(VKResponse response) { super.onComplete(response); noConnectionView.setVisibility(View.GONE); progressBar.setVisibility(View.GONE); swipeRefresh.setRefreshing(false); videoList.clear(); videoList = Parser.parseVideos(response); offset = videoList.size(); videoAdapter = new VideoAdapter(getActivity(), videoList, VideosFragment.this); recyclerView.setAdapter(videoAdapter); if (videoList.isEmpty()) { noVideosView.setText(R.string.no_added_videos); noVideosView.setVisibility(View.VISIBLE); noVideos = true; } } }); }
private void loadMore() { offset += offset; VKRequest videoRequest = VKApi.video() .get( VKParameters.from( VKApiConst.OWNER_ID, ownerId, VKApiConst.ALBUM_ID, albumId, VKApiConst.OFFSET, offset)); videoRequest.executeWithListener( new VKRequest.VKRequestListener() { @Override public void onComplete(VKResponse response) { super.onComplete(response); videoList.addAll(Parser.parseVideos(response)); int curSize = videoAdapter.getItemCount(); videoAdapter.notifyItemRangeInserted(curSize, videoList.size() - 1); } }); }
private void makePost(VKAttachments attachments, String message) { final VKRequest post = VKApi.wall() .post( VKParameters.from( VKApiConst.ATTACHMENTS, attachments, VKApiConst.MESSAGE, message)); post.setModelClass(VKWallPostResult.class); post.executeWithListener( new VKRequest.VKRequestListener() { @Override public void onComplete(VKResponse response) { try { super.onComplete(response); Log.d("LOG_TAG", "post ok"); VKWallPostResult result = (VKWallPostResult) response.parsedModel; Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( String.format("https://vk.com/wall" + userID + "_" + result.post_id))); startActivity(intent); } catch (NullPointerException e) { e.printStackTrace(); Log.d(LOG_TAG, "error"); } } }); }
private void getNewPosts() { /*Log.d(TAG, "onComplete: timeForNotificationUpdate = " + Settings.timeForNotificationUpdate); Log.d(TAG, "onComplete: TIME_FOR_NOTIFICATION_UPDATE = " + settings.getInt(Settings.APP_PREFERENCES_TIME_FOR_NOTIFICATION_UPDATE, 0)); Log.d(TAG, "onComplete: LAST_SEEN_POST_ID" + settings.getLong(Settings.APP_PREFERENCES_LAST_SEEN_POST_ID, 0));*/ VKApi.wall() .get(VKParameters.from(VKApiConst.OWNER_ID, Info.GROUP_ID, VKApiConst.COUNT, 100)) .executeSyncWithListener( new VKRequest.VKRequestListener() { @Override public void onComplete(VKResponse response) { try { JSONArray postsNew = response.json.getJSONObject("response").getJSONArray("items"); for (int i = 0; i < postsNew.length(); i++) { VKApiPost vkPost = new VKApiPost(postsNew.getJSONObject(i)); if (!havePinned) { try { havePinned = response .json .getJSONObject("response") .getJSONArray("items") .getJSONObject(i) .getInt("is_pinned") == 1; } catch (JSONException e) { } } if (vkPost.id == settings.getLong(Settings.APP_PREFERENCES_LAST_SEEN_POST_ID, 0)) { if (havePinned) newPostsCount = i - 1; else newPostsCount = i; } // если добавилось newPostsForNotificationCount постов, то выводить уведомление // о новом посте if (newPostsCount >= Settings.newPostsForNotificationCount) { PendingIntent resultPendingIntent = PendingIntent.getActivity( NotificationService.this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); Notification.Builder builder = new Notification.Builder(NotificationService.this) .setDefaults(Notification.DEFAULT_SOUND) .setDefaults(Notification.DEFAULT_VIBRATE) .setTicker("Новые посты!") .setContentTitle( getResources().getString(R.string.notification_title)) .setContentText( getResources() .getQuantityString( R.plurals.numberOfNewPosts, newPostsCount, newPostsCount)) .setSmallIcon(R.mipmap.ic_app) .setContentIntent(resultPendingIntent) .setAutoCancel(true); // выводится верно Notification notification = new Notification.BigTextStyle(builder) .bigText( getResources() .getQuantityString( R.plurals.numberOfNewPosts, newPostsCount, newPostsCount)) .build(); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(1, notification); } /*Log.i(TAG, vkPost.id + " = " + settings.getLong(Settings.APP_PREFERENCES_LAST_SEEN_POST_ID, 0) + "\nhavePinned = " + havePinned + "\ni = " + i + "\nnewPostsCount = " + newPostsCount + "\nnewPostsForNotificationCount = " + settings.getInt(Settings.APP_PREFERENCES_NEW_POSTS_FOR_NOTIFICATION_COUNT, 0));*/ } } catch (JSONException e) { e.printStackTrace(); } } }); }
@OnClick(R.id.post_like_button) void like() { int post_id = mPost.post_id != 0 ? mPost.post_id : mPost.id; int source_id = mPost.source_id != 0 ? mPost.source_id : mPost.from_id; if (mPost.user_likes) { VKRequest request = VKApi.likes() .delete( VKParameters.from( "type", "post", VKApiConst.OWNER_ID, String.valueOf(source_id), "item_id", String.valueOf(post_id))); request.useSystemLanguage = false; request = VKRequest.getRegisteredRequest(request.registerObject()); if (request != null) { request.unregisterObject(); } request.executeWithListener( new VKRequestListener() { @Override public void onComplete(VKResponse response) { mPost.likes_count -= 1; postCountOfLike.setText(String.valueOf(mPost.likes_count)); mPost.user_likes = false; } @Override public void onError(VKError error) { Log.d("Error", error.toString()); } }); } else { VKRequest request = VKApi.likes() .add( VKParameters.from( "type", "post", VKApiConst.OWNER_ID, String.valueOf(source_id), "item_id", String.valueOf(post_id))); request.useSystemLanguage = false; request = VKRequest.getRegisteredRequest(request.registerObject()); if (request != null) { request.unregisterObject(); } request.executeWithListener( new VKRequestListener() { @Override public void onComplete(VKResponse response) { mPost.likes_count += 1; postCountOfLike.setText(String.valueOf(mPost.likes_count)); mPost.user_likes = true; } @Override public void onError(VKError error) { Log.d("Error", error.toString()); } }); } }