首页 > 解决方案 > 带有迭代的 Yelp API 搜索请求 - 如何进行迭代

问题描述

下面我有我的搜索请求代码。由于 yelp 只允许获得 50 个结果,因此我无法使其正常工作。

所以基本上 offset = 0 和 limit = 50 会给我带来结果 0-50 offset = 50 和 limit = 50 会给我带来结果 51-100

因此,就我而言,我需要 1000 个结果。我该如何管理它?我尽我所能尝试了一切,现在我需要一些帮助。

    public class Search2 {
    static HttpURLConnection conn;
        public static void Searche() {

            int offset = 0;
            int limit = 50;

            try {

    for (int i = 0; i < 950; i=+50) {
        URL url = new URL("https://api.yelp.com/v3/businesses/search?location=" + gui.Lead_1.tFOrt.getText()
        + "&categories=" + gui.Lead_1.tFKeyword.getText() + "&offset=" + i + "&limit=50");

     conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Accept", "application/json");
        }

                try {
                    conn.setRequestProperty("Authorization", "Bearer " + dbb.Pfad.getPath() + " ");
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                // throwing the error message if the response is not available.
                if (conn.getResponseCode() != 200) {
                    throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());

                }

                // capturing the response and appending it to the response string.
                BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
                String output;
                StringBuffer response = new StringBuffer();
                while ((output = br.readLine()) != null) {
                    response.append(output);
                }

                // converting the response to the JSONObject.
                JSONObject jsonObj = new JSONObject(response.toString());
                JSONArray list1 = new JSONArray();
                JSONArray arr = jsonObj.getJSONArray("businesses");
                JSONObject details = new JSONObject();
                // capturing the specific values(name, rating, reviewcount) from the response
                for (int i = 0; i < arr.length(); i++) {


                    String name = arr.getJSONObject(i).getString("name");
                    int rating = arr.getJSONObject(i).getInt("rating");
                    int review_count = arr.getJSONObject(i).getInt("review_count");

                    JSONObject address = arr.getJSONObject(i).getJSONObject("location");

                    details.put("Name", name);

                    details.put("Rating", rating);
                    details.put("Review Count", review_count);

                    details.put("Address", address);
                    JSONObject object = new JSONObject();
                    object.put("restaurant " + (i + 1), details);
                    list1.put(object);


                    Lead_2.row[0]=i+1;

                    Lead_2.row[1]=arr.getJSONObject(i).getString("name");

                Lead_2.row[2]=arr.getJSONObject(i).getJSONObject("location").getString("address1");
                    Lead_2.model.addRow(Lead_2.row);
                }

标签: javaloops

解决方案


推荐阅读