A
A
astral10042019-12-17 22:41:44
Java
astral1004, 2019-12-17 22:41:44

How to make a cycle. vk api request on java?

Good afternoon. There is such a code that sends an API request to get photos in a certain area, and saves the resulting json file.

String key = "7075966f7075966f7075966f577018909e770757075966f2dfd2579655817dfd28d3bfd";
    String Vers = "5.103";
    System.out.println("Широта ");
    String LATITUDE  = inscaner.next();
    System.out.println("Долгота ");
    String LONGITUDE = inscaner.next();
    System.out.println("Укажите радиус поиска (например, 100 метров) = ");
    String radius = inscaner.next();
    String url = "https://api.vk.com/method/photos.search?"
    + "&lat=" + LATITUDE 
    + "&long=" + LONGITUDE
    + "&count=" + count
    + "&radius=" +radius 
    + "&access_token=" + key
    + "&v=" + Vers;
try {
      URL obj = new URL(url);
      HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
      connection.setRequestMethod("GET");

      BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      String inputLine;
      StringBuffer response = new StringBuffer();

      while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
      }
      in.close();
      /*
      System.out.println(response.toString());
      */
      try (FileWriter file = new FileWriter("response-vk.json")) {
          file.write(response.toString());
      }    
}catch(Exception e){}
   
  }

When answering, I get the following json structure
{"response":{"count":5365,"items":[{"id":457239398,"album_id":-7,"owner_id":515520034,"sizes":[{"type":"s","url":"https:\/\/sun9-37.userapi.com\/c858024\/v858024733\/12881f\/YmnQybQVwQw.jpg","width":42,"height":75},{"type":"m","url":"https:\/\/sun9-44.userapi.com\/c858024\/v858024733\/128820\/pP86RyrkS_M.jpg","width":73,"height":130},{"type":"x","url":"https:\/\/sun9-

The line count:5356 shows the number of photos in the area I specified. Since with one request, VK allows you to get 1000 photos. I need to get all 5356 photos.
But in VK there is such a parameter as offsetc (offset relative to the first user found to select a specific subset.)
Help me to implement getting all photos (not just the first thousand that I get with my request). I can’t figure out how to do this do

Answer the question

In order to leave comments, you need to log in

1 answer(s)
3
3aiats, 2019-12-18
@3aiats

loop with integer modulo (count by 1000) +1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question