Пример #1
0
  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);
          }
        });
  }
  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;
            }
          }
        });
  }
  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();
    }
  }
  /**
   * Method used for response processing
   *
   * @param jsonResponse response from API
   * @param parsedModel model parsed from json
   */
  private void provideResponse(final JSONObject jsonResponse, Object parsedModel) {
    final VKResponse response = new VKResponse();
    response.request = this;
    response.json = jsonResponse;
    response.parsedModel = parsedModel;
    if (mLoadingOperation instanceof VKHttpOperation) {
      response.responseString = ((VKHttpOperation) mLoadingOperation).getResponseString();
    }

    if (mPostRequestsQueue != null && mPostRequestsQueue.size() > 0) {
      for (VKRequest request : mPostRequestsQueue) {
        request.start();
      }
    }

    if (requestListener != null) {
      requestListener.onComplete(response);
    }
  }
  @Override
  protected Void doInBackground(Void... voids) {
    final VKRequest vkRequest = Requests.vkFriendsRequest(Requests.LOCALE_RUS);

    vkRequest.executeWithListener(
        new VKRequest.VKRequestListener() {
          @Override
          public void onComplete(VKResponse response) {
            super.onComplete(response);
            vkTask = new ParseVkResponseTask(vkFriends, listener, response);
            vkTask.setShowResults(false);
            vkTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
          }
        });

    while (vkTask == null) {}

    try {
      vkTask.get();
      Request fbRequest = Requests.fbFriendsRequest();
      Response response = fbRequest.executeAndWait();
      fbTask = new ParseFbResponseTask(fbFriends, listener, response);
      fbTask.setShowResults(false);
      Handler mainHandler = new Handler(Looper.getMainLooper());
      mainHandler.post(
          new Runnable() {
            @Override
            public void run() {
              fbTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            }
          });
      fbTask.get();
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }

    fillSyncContactsList();
    storeSyncContacts();

    return null;
  }
 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);
         }
       });
 }
Пример #7
0
    @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());
            }
          });
    }
Пример #8
0
  void start() {
    logging.Write("Service started");

    // Загрузка настроек
    Properties properties = new Properties(context);
    properties.LoadData();

    // Установка языка
    Configuration config = new Configuration();
    config.locale = new Locale(properties.locale);
    Locale.setDefault(config.locale);
    context.getResources().updateConfiguration(config, null);

    // Отправка запроса на выявления пользователя
    VKRequest request = VKApi.users().get();
    request.executeWithListener(
        new VKRequest.VKRequestListener() {
          @Override
          public void onComplete(VKResponse response) {
            // Заполнение данных пользователя
            JSON json = new JSON();
            String info;
            info = json.getInfo(response, "first_name");
            info += " ";
            info += json.getInfo(response, "last_name");
            Toast.makeText(
                    context,
                    context.getResources().getString(R.string.Connected) + info,
                    Toast.LENGTH_SHORT)
                .show();
          }
        });

    Intent intent = new Intent(context, Receiver.class);
    pIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    aManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
    aManager.setRepeating(
        AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 600000, pIntent);
  }
  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");
            }
          }
        });
  }
Пример #10
0
 /**
  * Register current request for execute after passed request, if passed request is successful. If
  * it's not, errorBlock will be called.
  *
  * @param request after which request must be called that request
  * @param listener listener for request events
  */
 public void executeAfterRequest(VKRequest request, VKRequestListener listener) {
   this.requestListener = listener;
   request.addPostRequest(this);
 }
Пример #11
0
    @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());
              }
            });
      }
    }