如何以编程方式在Android中搜索图像?

时间:2018-02-14 18:18:21

标签: android android-volley google-image-search

我的EditTexts中有一些Activitytitle的第一个文字,author的第二个文字。现在,用户从第二个edittext {{ 1}}。我想获得与该内容相关的图像(标题和作者)。所以我做了什么,我结束ie author并制作title and author name。然后我打印出那个回复。但是回复是如此不可预测,我无法从中获取图像。

HTTP request using Volley

响应是这样的:

try {
        String googleImageUrl = "http://images.google.com/images?q=";
        String query = URLEncoder.encode(title + " " + author, "utf-8");
        String url = googleImageUrl + query;
        Toast.makeText(context, url, Toast.LENGTH_SHORT).show();
        // Instantiate the RequestQueue.
        RequestQueue queue = Volley.newRequestQueue(this);

  // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        post_des.setText("Response is: " + response);
                        Log.i("Show me something awesome dude", response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                post_des.setText("That didn't work!");
            }
        });
 // Add the request to the RequestQueue.
        queue.add(stringRequest);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

我期待得到一个Html文档。 那么如何使用内容(标题和作者)对图像进行HTTP请求。

修改 外行语言

假设我在images.google.com上,我在搜索栏中键入内容并进行搜索,现在我希望Google返回的数据为Response is: <!doctype html><html itemscope="" itemtype="http://schema.org/SearchResultsPage" lang="en-IN"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><link href="/images/branding/product/ico/googleg_lodp.ico" rel="shortcut icon"><title>something something - Google Search</title><style>#gb{font:13px/27px Arial,sans-serif;height:30px}#gbz,#gbg{position:absolute;white-space:nowrap;top:0;height:30px;z-index:1000}#gbz{left:0;padding-left:4px}#gbg{right:0;padding-right:5px}#gbs{background:transparent;position:absolute;top:-999px;visibility:hidden;z-index:998;right:0}.gbto #gbs{background:#fff}#gbx3,#gbx4{background-color:#2d2d2d;background-image:none;_background-image:none;background-position:0 -138px;background-repeat:repeat-x;border-bottom:1px solid #000;font-size:24px;height:29px;_height:30px;opacity:1;filter:alpha(opacity=100);position:absolute;top:0;width:100%;z-index:990}#gbx3{left:0}#gbx4{right:0}#gbb{position:relative}#gbbw{left:0;position:absolute;top:30px;width:100%}.gbtcb{position:absolute;visibility:hidden}#gbz .gbtcb{right:0}#gbg .gbtcb{left:0}.gbxx{display:none........like wise (我在后端执行此操作)没有向用户显示。) 我认为现在可以理解了:)

2 个答案:

答案 0 :(得分:1)

你有html但整个搜索页面。你可以检索图片&#39;网址为css selectors和[JSOUP库] [2](易于使用)。只需转到Chrome浏览器,然后选择设置 - 更多工具 - 开发人员工具。然后在图片上单击鼠标右键并选择inspect,您将看到哪个容器用于图片,哪个div包含图片的src url,然后右键单击此div并选择{{1 }}。然后使用库。

但要注意,如果他们更改了你的代码会丢失的页面html,那么这不是实际原因。您最好为此目的使用特定的api,例如上面评论中建议的Google Custom Search API。

要将图片放入用户界面,您需要获取其网址,然后您可以使用copy css selectorGlide甚至Picasso

Volley

编辑:

以下是Google搜索页// Retrieves an image with Volley specified by the URL, displays it in the UI. ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap bitmap) { mImageView.setImageBitmap(bitmap); } }, 0, 0, null, new Response.ErrorListener() { public void onErrorResponse(VolleyError error) { mImageView.setImageResource(R.drawable.image_load_error); } }); 上所有图片的CSS选择器。使用img.rg_ic和此选择器,您可以访问页面上的所有图片代码

Jsoup示例:

Jsoup

[![在此处输入图像说明] [3]] [3]

enter image description here

EDIT2:

您的代码包含更改:

Document doc = Jsoup.connect(your link string).get();
Elements imgs = doc.select("img");//the selector
for (Element img : imgs) {
  //add img urls to String array and then use to get imgs with them
  String s = img.attr("src");
  arr.add(s);
}

}

答案 1 :(得分:0)

您可以使用Jsoup。,获取google搜索图像列表。在此处https://jsoup.org/

中查看官方网站
/**
 * Extract images from google as ArrayList.
 *
 * @param searchQuery is the string to search for
 * @return returnedURLS is the List of urls
 */
private List<String> extractImagesFromGoogle(String searchQuery) throws IOException {

    final String encodedSearchUrl = "https://www.google.com/search?q=" + URLEncoder.encode(searchQuery, "UTF-8") + "&source=lnms&tbm=isch&sa=X&ved=0ahUKEwiUpP35yNXiAhU1BGMBHdDeBAgQ_AUIECgB";

    Document document = Jsoup.connect(encodedSearchUrl).get();
    String siteResponse = document.toString();

    List<String> returnedURLS = new ArrayList<String>();
    // parse the object and query the values (the urls) for specific keys ("ou")
    Pattern pattern = Pattern.compile("\"ou\":\"(.*?)\"");
    Matcher matcher = pattern.matcher(siteResponse);

    while (matcher.find()) {
        returnedURLS.add(matcher.group(1));
    }

    return returnedURLS;
}

    // Test it now:
    List<String> retrievedURLS = new ArrayList<String>();

    try {
        retrievedURLS = extractImagesFromGoogle("pyramids");
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println(">> List Size: " + retrievedURLS.size());
    System.out.println(">> List of images urls: " + retrievedURLS);