问题描述
我用我的Android应用程序谷歌图片搜索API来搜索图像。它工作正常,但我只得到8结果查询。
我怎样才能获得超过8个图像? (或者有什么更好的办法来搜索图片吗?)
下面是我的code:
公共类getImagesTask扩展的AsyncTask<虚空,虚空,虚空>
{
的JSONObject JSON;
ProgressDialog对话框;
@覆盖
在preExecute保护无效(){
// TODO自动生成方法存根
super.on preExecute();
对话框= ProgressDialog.show(GoogleSearchAPIExampleActivity.this,,请稍候...);
}
@覆盖
保护无效doInBackground(虚空...... PARAMS){
// TODO自动生成方法存根
URL网址;
尝试 {
URL =新的URL(https://ajax.googleapis.com/ajax/services/search/images?+
V = 1.0&安培; Q =+ strSearch +与& RSZ = 8); //&key=ABQIAAAADxhJjHRvoeM2WF3nxP5rCBRcGWwHZ9XQzXD3SWg04vbBlJ3EWxR0b0NVPhZ4xmhQVm3uUBvvRF-VAA&userip=192.168.0.172");
URLConnection的连接= url.openConnection();
connection.addRequestProperty(Referer的,http://technotalkative.com);
串线;
StringBuilder的建设者=新的StringBuilder();
的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(connection.getInputStream()));
而((行= reader.readLine())!= NULL){
builder.append(线);
}
的System.out.println(生成器串=>中+ builder.toString());
JSON =新的JSONObject(builder.toString());
}赶上(MalformedURLException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(JSONException E){
// TODO自动生成的catch块
e.printStackTrace();
}
返回null;
}
@覆盖
保护无效onPostExecute(无效的结果){
// TODO自动生成方法存根
super.onPostExecute(结果);
如果(dialog.isShowing())
{
dialog.dismiss();
}
尝试 {
JSONObject的responseObject = json.getJSONObject(responseData);
JSONArray resultArray = responseObject.getJSONArray(结果);
listImages = getImageList(resultArray);
SetListViewAdapter(listImages);
的System.out.println(结果数组的长度=>中+ resultArray.length());
}赶上(JSONException E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
}
logcat的:
9月10日至22日:32:08.020:我/的System.out(2614):构建字符串=> {
responseData:{
结果:[{
GsearchResultClass:GimageSearch,
宽:948,
高度:532,
imageId:ANd9GcRjL9QC6U-D2kjCE3NHvmkaDj1U3j46xsqrdsT3xwVkBdcxcV-jakbK2so,
tbWidth:148,
tbHeight:83,
unescapedUrl: "http://media.npr.org/assets/img/2013/05/22/ap401533991888_wide-870363ccd3eb24d332ecc17b6bb44a351a5da196-s6-c30.jpg",
URL: "http://media.npr.org/assets/img/2013/05/22/ap401533991888_wide-870363ccd3eb24d332ecc17b6bb44a351a5da196-s6-c30.jpg",
visibleUrl:www.npr.org,
头衔:脊髓灰质炎暴发在肯尼亚:\ u003cb \ u003eA \ u003c / B \ u003e威胁全球根除:射击\ u003cb \ u003e \ u003c / B \ u003e
titleNoFormatting:脊髓灰质炎暴发在肯尼亚:威胁全球根除:射击......
originalContextUrl: "http://www.npr.org/blogs/health/2013/05/22/185996377/polio-outbreak-in-kenya-a-threat-to-global-eradication",
内容:脊髓灰质炎暴发在肯尼亚:\ u003cb \ u003eA \ u003c / B \ u003e威胁全球根除:镜头,
contentNoFormatting:脊髓灰质炎暴发在肯尼亚:威胁全球根除:镜头,
tbUrl: "http://t1.gstatic.com/images?q\u003dtbn:ANd9GcRjL9QC6U-D2kjCE3NHvmkaDj1U3j46xsqrdsT3xwVkBdcxcV-jakbK2so"
},{
GsearchResultClass:GimageSearch,
宽:600,
高度:424,
imageId:ANd9GcQKbMdb_XZJ1TqEcYBz3TC-PSL-NYRwzMzBRDU1tmB8dpUOWlOp0l2IHII
tbWidth:135,
tbHeight:95,
unescapedUrl: "http://graphics8.nytimes.com/images/2013/05/31/arts/artsspecial/31iht-rfrcballet31A/31iht-rfrcballet31A-articleLarge.jpg",
URL: "http://graphics8.nytimes.com/images/2013/05/31/arts/artsspecial/31iht-rfrcballet31A/31iht-rfrcballet31A-articleLarge.jpg",
visibleUrl:www.nytimes.com,
头衔:机智与创新的芭蕾\ u003cb \ u003eA \ u003c / B \ u003e新鲜的时代 - 纽约时报,
titleNoFormatting:机智与创新的芭蕾一个新的时代 - 纽约时报,
originalContextUrl: "http://www.nytimes.com/2013/05/31/arts/artsspecial/A-Fresh-Era-of-Wit-and-Innovation-in-Ballet.html",
内容:机智与创新的芭蕾\ u003cb \ u003eA \ u003c / B \ u003e新鲜的时代 - 纽约时报,
contentNoFormatting:机智与创新的芭蕾一个新的时代 - 纽约时报,
tbUrl: "http://t0.gstatic.com/images?q\u003dtbn:ANd9GcQKbMdb_XZJ1TqEcYBz3TC-psl-NYRwzMzBRDU1tmB8dpUOWlOp0l2IHII"
},{
GsearchResultClass:GimageSearch,
宽:425,
高度:283,
imageId:ANd9GcSCIN0Dn0f2rfd-GpdhQ9TuhMt3O0i2X7JKAFSARBFm460PQ1Bd-5eNSsk,
tbWidth:126,
tbHeight:84,
unescapedUrl:http://www.usnews.com/pubdbimages/image/45181/FE_DA_0307_Marine425x283.jpg,
URL:http://www.usnews.com/pubdbimages/image/45181/FE_DA_0307_Marine425x283.jpg
visibleUrl:www.usnews.com,
头衔:能古海洋战略扑灭\ u003cb \ u003ea \ u003c / B \ u003e新的太平洋战争 - 美国新闻和\ u003cb \ u003e \ u003c / B \ u003e?,
titleNoFormatting:能古海洋战略打新的太平洋战争 - 美国新闻和......?,
originalContextUrl:http://www.usnews.com/news/articles/2013/03/07/can-old-marine-strategies-fight-a-new-pacific-war,
内容:老海洋战略可以打\ u003cb \ u003ea \ u003c / B \ u003e新的太平洋战争 - 美国新闻和?,
contentNoFormatting:能古海洋战略打新的太平洋战争 - 美国新闻和?,
tbUrl: "http://t2.gstatic.com/images?q\u003dtbn:ANd9GcSCIN0Dn0f2rfd-GpdhQ9TuhMt3O0i2X7JKAFSARBFm460PQ1Bd-5eNSsk"
},{
GsearchResultClass:GimageSearch,
宽:460,
高度:276,
imageId:ANd9GcTBwAl3G4zqkRw1qx4gMs6gfWfR8YAz-X7HhW6GkfJEBADHDIdDUBpxHhA,
tbWidth:128,
tbHeight:77,
unescapedUrl: "http://static.guim.co.uk/sys-images/Arts/Arts_/Pictures/2013/2/12/1360689033304/A-2002-version-of-The-Rit-010.jpg",
URL: "http://static.guim.co.uk/sys-images/Arts/Arts_/Pictures/2013/2/12/1360689033304/A-2002-version-of-The-Rit-010.jpg",
visibleUrl:www.theguardian.com,
头衔:春之祭:\ u0026#39;的工作\ u003cb \ u003ea \ u003c / B \ u003e疯子\ u0026#39; |音乐|卫报,
titleNoFormatting:春之祭:\ u0026#39;的疯子\ u0026#39的工作; |音乐|卫报,
originalContextUrl:http://www.theguardian.com/music/2013/feb/12/rite-of-spring-stravinsky,
内容:春之祭:\ U00
URL =新的URL(https://ajax.googleapis.com/ajax/services/search/images? +
V = 1.0&安培; Q =+ strSearch +与& RSZ = 8&安培;开始= 0);
在这里,你可以得到64图像效果只能使用此API。
现在在这里会发生什么,如果你设置启动变量= 9
,它会增加一个图像的输出,但第一个图像将消失(因为我们只能得到每页8图像)。
现在,当您设置启动变量= 8
,你会得到一个8影像(第数字2与接下来的8个图像)。
另外,如果设置启动变量= 16
,你会得到一个8影像(第number3的与未来的8图像)。
另外,如果设置启动变量= 24
,你会得到一个8影像(第号码4与接下来的8图像)等。
您将获得的图像到这个网址。
URL =新的URL(https://ajax.googleapis.com/ajax/services/search/images?+
V = 1.0&安培; Q =+ strSearch +与& RSZ = 8&安培;开始= 56);
(第8页)
现在从这里,如果你开始增加变量,如开始= 57
,就没有输出。
I'm using Google image search API in my Android app to search images. It is working fine, but I'm getting only 8 results of query.
How can I get more than 8 images? (Or is there any better way to search images?)
Below is my code:
public class getImagesTask extends AsyncTask<Void, Void, Void>
{
JSONObject json;
ProgressDialog dialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = ProgressDialog.show(GoogleSearchAPIExampleActivity.this, "", "Please wait...");
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
URL url;
try {
url = new URL("https://ajax.googleapis.com/ajax/services/search/images?" +
"v=1.0&q="+strSearch+"&rsz=8" ); //&key=ABQIAAAADxhJjHRvoeM2WF3nxP5rCBRcGWwHZ9XQzXD3SWg04vbBlJ3EWxR0b0NVPhZ4xmhQVm3uUBvvRF-VAA&userip=192.168.0.172");
URLConnection connection = url.openConnection();
connection.addRequestProperty("Referer", "http://technotalkative.com");
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
builder.append(line);
}
System.out.println("Builder string => "+builder.toString());
json = new JSONObject(builder.toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(dialog.isShowing())
{
dialog.dismiss();
}
try {
JSONObject responseObject = json.getJSONObject("responseData");
JSONArray resultArray = responseObject.getJSONArray("results");
listImages = getImageList(resultArray);
SetListViewAdapter(listImages);
System.out.println("Result array length => "+resultArray.length());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Logcat:
10 - 22 09: 32: 08.020: I / System.out(2614): Builder string = > {
"responseData": {
"results": [{
"GsearchResultClass": "GimageSearch",
"width": "948",
"height": "532",
"imageId": "ANd9GcRjL9QC6U-D2kjCE3NHvmkaDj1U3j46xsqrdsT3xwVkBdcxcV-jakbK2so",
"tbWidth": "148",
"tbHeight": "83",
"unescapedUrl": "http://media.npr.org/assets/img/2013/05/22/ap401533991888_wide-870363ccd3eb24d332ecc17b6bb44a351a5da196-s6-c30.jpg",
"url": "http://media.npr.org/assets/img/2013/05/22/ap401533991888_wide-870363ccd3eb24d332ecc17b6bb44a351a5da196-s6-c30.jpg",
"visibleUrl": "www.npr.org",
"title": "Polio Outbreak In Kenya: \u003cb\u003eA\u003c/b\u003e Threat To Global Eradication : Shots \u003cb\u003e...\u003c/b\u003e",
"titleNoFormatting": "Polio Outbreak In Kenya: A Threat To Global Eradication : Shots ...",
"originalContextUrl": "http://www.npr.org/blogs/health/2013/05/22/185996377/polio-outbreak-in-kenya-a-threat-to-global-eradication",
"content": "Polio Outbreak In Kenya: \u003cb\u003eA\u003c/b\u003e Threat To Global Eradication : Shots",
"contentNoFormatting": "Polio Outbreak In Kenya: A Threat To Global Eradication : Shots",
"tbUrl": "http://t1.gstatic.com/images?q\u003dtbn:ANd9GcRjL9QC6U-D2kjCE3NHvmkaDj1U3j46xsqrdsT3xwVkBdcxcV-jakbK2so"
}, {
"GsearchResultClass": "GimageSearch",
"width": "600",
"height": "424",
"imageId": "ANd9GcQKbMdb_XZJ1TqEcYBz3TC-psl-NYRwzMzBRDU1tmB8dpUOWlOp0l2IHII",
"tbWidth": "135",
"tbHeight": "95",
"unescapedUrl": "http://graphics8.nytimes.com/images/2013/05/31/arts/artsspecial/31iht-rfrcballet31A/31iht-rfrcballet31A-articleLarge.jpg",
"url": "http://graphics8.nytimes.com/images/2013/05/31/arts/artsspecial/31iht-rfrcballet31A/31iht-rfrcballet31A-articleLarge.jpg",
"visibleUrl": "www.nytimes.com",
"title": "\u003cb\u003eA\u003c/b\u003e Fresh Era of Wit and Innovation in Ballet - NYTimes.",
"titleNoFormatting": "A Fresh Era of Wit and Innovation in Ballet - NYTimes.",
"originalContextUrl": "http://www.nytimes.com/2013/05/31/arts/artsspecial/A-Fresh-Era-of-Wit-and-Innovation-in-Ballet.html",
"content": "\u003cb\u003eA\u003c/b\u003e Fresh Era of Wit and Innovation in Ballet - NYTimes.",
"contentNoFormatting": "A Fresh Era of Wit and Innovation in Ballet - NYTimes.",
"tbUrl": "http://t0.gstatic.com/images?q\u003dtbn:ANd9GcQKbMdb_XZJ1TqEcYBz3TC-psl-NYRwzMzBRDU1tmB8dpUOWlOp0l2IHII"
}, {
"GsearchResultClass": "GimageSearch",
"width": "425",
"height": "283",
"imageId": "ANd9GcSCIN0Dn0f2rfd-GpdhQ9TuhMt3O0i2X7JKAFSARBFm460PQ1Bd-5eNSsk",
"tbWidth": "126",
"tbHeight": "84",
"unescapedUrl": "http://www.usnews.com/pubdbimages/image/45181/FE_DA_0307_Marine425x283.jpg",
"url": "http://www.usnews.com/pubdbimages/image/45181/FE_DA_0307_Marine425x283.jpg",
"visibleUrl": "www.usnews.com",
"title": "Can Old Marine Strategies Fight \u003cb\u003ea\u003c/b\u003e New Pacific War? - US News and \u003cb\u003e...\u003c/b\u003e",
"titleNoFormatting": "Can Old Marine Strategies Fight a New Pacific War? - US News and ...",
"originalContextUrl": "http://www.usnews.com/news/articles/2013/03/07/can-old-marine-strategies-fight-a-new-pacific-war",
"content": "Can Old Marine Strategies Fight \u003cb\u003ea\u003c/b\u003e New Pacific War? - US News and",
"contentNoFormatting": "Can Old Marine Strategies Fight a New Pacific War? - US News and",
"tbUrl": "http://t2.gstatic.com/images?q\u003dtbn:ANd9GcSCIN0Dn0f2rfd-GpdhQ9TuhMt3O0i2X7JKAFSARBFm460PQ1Bd-5eNSsk"
}, {
"GsearchResultClass": "GimageSearch",
"width": "460",
"height": "276",
"imageId": "ANd9GcTBwAl3G4zqkRw1qx4gMs6gfWfR8YAz-X7HhW6GkfJEBADHDIdDUBpxHhA",
"tbWidth": "128",
"tbHeight": "77",
"unescapedUrl": "http://static.guim.co.uk/sys-images/Arts/Arts_/Pictures/2013/2/12/1360689033304/A-2002-version-of-The-Rit-010.jpg",
"url": "http://static.guim.co.uk/sys-images/Arts/Arts_/Pictures/2013/2/12/1360689033304/A-2002-version-of-The-Rit-010.jpg",
"visibleUrl": "www.theguardian.com",
"title": "The Rite of Spring: \u0026#39;The work of \u003cb\u003ea\u003c/b\u003e madman\u0026#39; | Music | The Guardian",
"titleNoFormatting": "The Rite of Spring: \u0026#39;The work of a madman\u0026#39; | Music | The Guardian",
"originalContextUrl": "http://www.theguardian.com/music/2013/feb/12/rite-of-spring-stravinsky",
"content": "The Rite of Spring: \u00
url = new URL("https://ajax.googleapis.com/ajax/services/search/images?" +
"v=1.0&q="+strSearch+"&rsz=8&start=0" );
Here you can get 64 image results only using this API.
Now here what happens, if you set start variable=9
, it will add one more image in output, but the first image will gone (as we can get only 8 images per page).
Now when you set start variable=8
, you will get next 8 images (page number2 with next 8 images).
Again, if you set start variable=16
, you will get next 8 images (page number3 with next 8 images).
Again, if you set start variable=24
, you will get next 8 images (page number4 with next 8 images) and so on.
You will get images till this URL.
url = new URL("https://ajax.googleapis.com/ajax/services/search/images?" +
"v=1.0&q="+strSearch+"&rsz=8&start=56" );
(8th page)
Now from here if you increment start variable such as start=57
, there will be no output.
这篇关于获得超过8图片搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!