java - 带有迭代的 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);
}
解决方案
推荐阅读
- ruby-on-rails - 如何使选择查询在 Rails 中相对更快?
- php - 关于 PHP/HTML 项目文件结构的困惑(带表单)
- python-3.x - Python3没有在类函数调用中添加“自我”
- c# - 如何以编程方式触发 C# DataGridView SelectionChanged 事件?
- javascript - React中变量更改时关闭和重新打开标签的清洁方式
- google-cloud-dataflow - Apache Beam Runner 用于生产中的单台机器
- python - PyQT:如何隐藏对话窗口中的每个对象
- c# - 循环错误,包括字符串和变量
- sqlite - 完整的 SQLite 新手 - 如何为 UWP 应用安装 SQLCipher
- mxgraph - 在 draw.io 内部的 mxgraph 中,我们会弹出一个包含文本区域的对话框。选择时如何控制滚动速度?