Java 带迭代的Yelp API搜索请求-如何进行迭代
下面是我的搜索请求代码。由于yelp只允许得到50个结果,我有一个问题,使其工作 所以基本上,偏移量=0和极限值=50会得到0-50的结果 偏移=50和极限=50将为我带来51-100的结果 所以我需要1000个结果。我怎么办?我尽力了,现在我需要一些帮助Java 带迭代的Yelp API搜索请求-如何进行迭代,java,loops,Java,Loops,下面是我的搜索请求代码。由于yelp只允许得到50个结果,我有一个问题,使其工作 所以基本上,偏移量=0和极限值=50会得到0-50的结果 偏移=50和极限=50将为我带来51-100的结果 所以我需要1000个结果。我怎么办?我尽力了,现在我需要一些帮助 public class Search2 { static HttpURLConnection conn; public static void Searche() { int off
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);
}
公共类搜索2{
静态httpurl连接连接;
公共静态无效搜索(){
整数偏移=0;
整数极限=50;
试一试{
对于(int i=0;i<950;i=+50){
URL=新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(“接受”、“应用程序/json”);
}
试一试{
conn.setRequestProperty(“授权”、“承载者”+dbb.Pfad.getPath()+”);
}捕获(异常e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
//如果响应不可用,则引发错误消息。
如果(连接getResponseCode()!=200){
抛出新的RuntimeException(“失败:HTTP错误代码:”+conn.getResponseCode());
}
//捕获响应并将其附加到响应字符串。
BufferedReader br=新的BufferedReader(新的InputStreamReader((conn.getInputStream()));
字符串输出;
StringBuffer响应=新的StringBuffer();
而((output=br.readLine())!=null){
追加(输出);
}
//将响应转换为JSONObject。
JSONObject jsonObj=新的JSONObject(response.toString());
JSONArray list1=新的JSONArray();
JSONArray arr=jsonObj.getJSONArray(“业务”);
JSONObject details=新的JSONObject();
//从响应中捕获特定值(名称、评级、reviewcount)
对于(int i=0;i
欢迎使用Stackoverflow。您可以通过提供帮助来鼓励其他用户帮助您解决问题。欢迎使用Stackoverflow。您可以通过提供帮助来鼓励其他用户帮助您解决问题。