gpt4 book ai didi

java - 如何显示谷歌自定义搜索结果的下一页?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:13:29 25 4
gpt4 key购买 nike

我知道 SOF 有很多类似的问题,但我没有找到对我有用的东西。根据 Google 自定义搜索文档,&start 参数:

The start parameter uses a zero-based index, meaning the first result is 0, the second result is 1 and so forth.

The start parameter works in conjunction with the num parameter to determine which search results to return.

我尝试从零开始发送,但应用程序崩溃了。在这次尝试中,我尝试发送:

对于 1 页:&num=10&start=1

对于 2 页:&num=10&start=11

但它仍然请求 1 页。 &start 参数总是被忽略

我正在编写一个执行图像搜索的 android 应用程序,向下滚动时它应该显示下一页(接下来的 10 张图像),在 android 中 它总是返回 1 页 有 10 张图像,我测试了请求浏览器,没有 &num 参数,它可以工作。在 android 上,似乎在第一页加载后,参数 &start&num 被忽略。

我将 robospice 与改造结合使用,但我不知道如何做 get request string 看看,也许我做错了什么

这是我的界面:

 public static final String BASE_URL = "https://www.googleapis.com/customsearch";
public static final String API_KEY = "AIzaSyCCuxxVLzm2sZP-adhRNYKeSck1mMMgsAM";
public static final String CUSTOM_SEARCH_ID = "001734592082236324715:sob9rqk49yg";
public static final String SEARCH_TYPE_IMAGE = "image";
static final String FILTER = "&fields=queries(nextPage(startIndex,count),request(startIndex,count)),searchInformation(totalResults),items(title,link,displayLink,mime," +
"image)";
static final String QUERY = "/v1?key="+API_KEY+
"&cx="+CUSTOM_SEARCH_ID+
"&searchType="+SEARCH_TYPE_IMAGE+FILTER+"&num=10";

@GET(QUERY)
public GoogleSearchResponse search(@Query("q") String query,
@Query("start") long startIndex);

香料请求

public class SampleRetrofitSpiceRequest extends RetrofitSpiceRequest<GoogleSearchResponse,GoogleSearchInterface> {

String query;
long startIndex;

public SampleRetrofitSpiceRequest(String query, long startIndex) {
super(GoogleSearchResponse.class, GoogleSearchInterface.class);
this.query = query;
this.startIndex = startIndex;
}
@Override
public GoogleSearchResponse loadDataFromNetwork() throws Exception {
return getService().search(query,startIndex);
}}

香料服务

public class SampleRetrofitSpiceService extends RetrofitGsonSpiceService {
@Override
public void onCreate() {
super.onCreate();
addRetrofitInterface(GoogleSearchInterface.class);
}

@Override
protected String getServerUrl() {
return GoogleSearchInterface.BASE_URL;
}}

现在我如何发送请求获得第一个结果页面,在页面参数我发送1,在获得第二个页面我发送 11:

 public void sendRequest(String query,int page){
searchQuery = query;
request = new SampleRetrofitSpiceRequest(query, page);

spiceManager.execute(request, query, DurationInMillis.ONE_WEEK, new RequestImageListener());
request=null;
}

这里是响应监听,nextPage是下一页的编号:

 private class RequestImageListener implements RequestListener<GoogleSearchResponse> {
@Override
public void onRequestFailure(SpiceException spiceException) {
Toast.makeText(context, "failure", Toast.LENGTH_SHORT).show();
}
@Override
public void onRequestSuccess(GoogleSearchResponse s) {
Toast.makeText(context,"success",Toast.LENGTH_SHORT).show();
nextPage = s.queries.getNextPage().startIndex;
Log.d("myTag","nextPage "+currentPage);
updateSearchResults(s.items);
}
}

更新 日志显示自定义 SpiceRequest 没有发出新请求,即使我创建了它的新实例也是如此。

最佳答案

似乎可以使用“start=PUT_NUMBER_HERE”URL 选项来获得更多结果。

Number是结果的序号,从1开始

例子: https://www.googleapis.com/customsearch/v1?q=searchtermhere&cx=asdfasdfasdf&key=adfasfdafasfd&start=11

关于java - 如何显示谷歌自定义搜索结果的下一页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31499695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com