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

时间:2015-07-19 09:17:40

标签: java android rest google-custom-search

我知道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个图像,我测试了请求浏览器,withount &num参数,它的工作原理。在Android上,似乎在第一次页面加载后,参数&start&num被忽略。

我正在使用robospice和改造,我无法弄清楚get request字符串是怎么看的,也许我做错了什么

这是我的界面:

 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);

SpiceRequest

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);
}}

SpiceService

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不会发出新请求,即使我创建了新请求。

1 个答案:

答案 0 :(得分:0)

It seems that the "start=PUT_NUMBER_HERE" URL option can be used to get more results.

Number is ordinal number of a result, starting from 1.

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