Den*_*ski 5 java rest android google-custom-search
我知道SOF有很多类似的问题,但我没有找到对我有用的东西.根据Google Custom Search文档,&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.
Run Code Online (Sandbox Code Playgroud)
我尝试从零发送,但应用程序崩溃.在这次尝试中我尝试发送:
为1页: &num=10&start=1
2页: &num=10&start=11
但它仍然要求1页.&start参数始终被忽略
我正在写一个Android应用程序,它进行图像搜索,在向下滚动它应该在下一页(下10个图像),在android 它总是返回1页有10个图像,我在浏览器中测试请求,withount &num参数,它的工作原理.在Android上,似乎在第一页加载后,参数&start和&num被忽略.
我使用robospice与改造相结合,我无法弄清楚如何获取请求字符串,也许我做错了什么
这是我的界面:
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);
Run Code Online (Sandbox Code Playgroud)
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);
}}
Run Code Online (Sandbox Code Playgroud)
SpiceService
public class SampleRetrofitSpiceService extends RetrofitGsonSpiceService {
@Override
public void onCreate() {
super.onCreate();
addRetrofitInterface(GoogleSearchInterface.class);
}
@Override
protected String getServerUrl() {
return GoogleSearchInterface.BASE_URL;
}}
Run Code Online (Sandbox Code Playgroud)
现在我如何获取请求获取第一个结果页面,在页面参数I发送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;
}
Run Code Online (Sandbox Code Playgroud)
这里是响应监听器,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);
}
}
Run Code Online (Sandbox Code Playgroud)
UPDATE 日志显示自定义SpiceRequest不会发出新请求,即使我创建它的新实例也是如此.
小智 1
似乎可以使用“start=PUT_NUMBER_HERE”URL 选项来获取更多结果。
Number 是结果的序号,从 1 开始。
示例: https: //www.googleapis.com/customsearch/v1? q=searchtermhere&cx=asdfasdfasdf&key=adfasfdafasfd&start=11
| 归档时间: |
|
| 查看次数: |
2174 次 |
| 最近记录: |