Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show next page of google custom search results?

I know SOF has many similar questions, but I didn't find in them anything useful for me. According to the Google Custom Search documentation, &start parameter:

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. 

I am tried sending from zero, but app crashed. In this attempt I trying send:

for 1 page: &num=10&start=1

for 2 page: &num=10&start=11

but it still requesting 1 page. &start parameter is always ignored

I am writing an android app which does image search, on scroll down it should dislpays next page(next 10 images), in android it always return 1 page with 10 images, I tested request in browser, withount &num parameter and it works. On android it seems that after first page load, parametes &start and &num are ignored.

I am use robospice in conjunction with retrofit and I can't figure out how get request string to see, maybe I doing something wrong

Here is my interface:

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

Now how I seng request to get first result page, in page parameter I send 1, in to get second page I send 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;
}

Here is response listener, nextPage is a number of the next page:

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

UPDATE Logs shows that custom SpiceRequest does not make a new request, even when I make new instance of it.

like image 470
Dennis Zinkovski Avatar asked Nov 09 '22 08:11

Dennis Zinkovski


1 Answers

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

like image 113
re Paul Avatar answered Nov 14 '22 21:11

re Paul