A
A
astral10042019-12-19 22:50:25
css
astral1004, 2019-12-19 22:50:25

How to add a stream?

Good afternoon. Help me add a sleep stop flow to my code.
Here is the example I want to add
Here is my code

package test;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

import org.json.JSONObject;

public class MAin {
  public static void main(String[] args) {
    String urlVK = "https://api.vk.com/method/photos.search?&radius=10000&lat=48.24523&long=58.2523&access_token=7075966f7075966f7075966f577018909e770757075966f2dfd2579655817dfd28d3bfd&v=5.103";
    try {	
    URL url = new URL(urlVK);
      // read from the URL
      Scanner scan = new Scanner(url.openStream());
      String str = new String();
      while (scan.hasNext())
          str += scan.nextLine();
      scan.close();
   
      // build a JSON object
      JSONObject obj = new JSONObject(str);
      int pageName = obj.getJSONObject("response").getInt("count");
      System.out.print(pageName);
       try {
      	 for(int i=0;i < pageName; i=i+1000) {
      		 String urlVKoffset = "https://api.vk.com/method/photos.search?&radius=10000&lat=48.24523&long=58.2523&access_token=7075966f7075966f7075966f577018909e770757075966f2dfd2579655817dfd28d3bfd&v=5.103"
      		 + "&offset=" + i;
      		 URL objVK = new URL(urlVKoffset);
      		    HttpURLConnection connection = (HttpURLConnection) objVK.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" + i + ".json")) {
      		        file.write(response.toString());
      		    }    
   		}
       }catch(Exception e){}
    }
    catch(Exception e){}
  }		
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2020-01-13
@Terran37

And where do you want to add the stream? And in which case do you want to do sleep?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question