获得超过8个图像搜索结果

时间:2013-10-22 09:10:17

标签: android google-image-search

我在Android应用中使用Google图片搜索API来搜索图片。它工作正常,但我只得到8个查询结果。

如何获得超过8张图片? (或者有更好的方法来搜索图像吗?)

以下是我的代码:

   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

2 个答案:

答案 0 :(得分:4)

  url = new URL("https://ajax.googleapis.com/ajax/services/search/images?" +
                        "v=1.0&q="+strSearch+"&rsz=8&start=0" );

在这里,您只能使用此API获得64张图片结果。

现在发生了什么,如果你设置了开始variable=9,它会在输出中再添加一个图像,但第一个图像将会消失(因为我们每页只能获得8个图像)。

现在,当您设置开始variable=8时,您将获得接下来的8张图片(第2页,其中包含8张图片)。

同样,如果您设置了开始variable=16,您将获得接下来的8张图片(第3页,包含下8张图片)。

同样,如果您设置了开始variable=24,您将获得接下来的8张图片(第4页包含下8张图片),依此类推。

您将获得此网址前的图片。

 url = new URL("https://ajax.googleapis.com/ajax/services/search/images?" +
                            "v=1.0&q="+strSearch+"&rsz=8&start=56" );
(8th page)

现在从这里开始增加start=57之类的起始变量,就没有输出。

答案 1 :(得分:0)

此服务仅调用8张图片。要拥有超过8张图像,您需要多次调用此服务。 见answer,这就是你想要的。