and*_*per 2 search android google-image-search google-play-services
我正在试图找出如何从互联网上查找图像,并在Android上显示它们(缩略图和全屏).
不仅谷歌已经弃用了以前的方式,但现在他们有一种称为"自定义搜索"的新文档:
后来,谷歌选择开发者应该使用"谷歌播放服务",但再次,我找不到这个特定任务的正确命令.这是我发现的:https: //developer.android.com/google/auth/api-client.html
我发现了很多链接,但不知道哪些是谷歌建议的最好的,我也不知道为什么几乎没有任何样本(更不用说Java和搜索功能)......
由于所有内容都已合并,并且由于文档(和示例)很少,我想问一下是否有一个如何使用这个新SDK实现图像搜索的示例,也许是否有一个库使其更容易.
我还想问一下没有Google Play服务的设备会发生什么 - 它还能运行吗?
只有部分Google API可通过Google Play服务库获取(例如:地图,广告,广告等).使用Play服务库为您提供了一些优势,如易于使用的客户端库,通用授权,设备上的自动更新等.
Custom Search API目前不是其中之一.因此,我们需要使用其他选项 - 适用于Android的Google API客户端库.使用此功能,您可以访问大多数Google API.另外,这不需要具有播放服务的客户端设备.
对于示例测试,请使用android-async-http并执行以下GET请求(您需要获取API密钥和CX密钥).例如:图片搜索独角兽:
GET https://www.googleapis.com/customsearch/v1?q=unicorn&cx={CX}&searchType=image&key={YOUR_API_KEY}
Run Code Online (Sandbox Code Playgroud)
.
用于图像搜索的示例JSON输出如下所示:
"items": [
{
"kind": "customsearch#result",
"title": "Unicorn - Monster Wiki - a reason to leave the closet closed and ...",
"htmlTitle": "<b>Unicorn</b> - Monster Wiki - a reason to leave the closet closed and <b>...</b>",
"link": "http://img1.wikia.nocookie.net/__cb20090425203919/monster/images/c/c4/Unicorn.jpg",
"displayLink": "monster.wikia.com",
"snippet": "Unicorn",
"htmlSnippet": "<b>Unicorn</b>",
"mime": "image/jpeg",
"image": {
"contextLink": "http://monster.wikia.com/wiki/Unicorn",
"height": 375,
"width": 500,
"byteSize": 235870,
"thumbnailLink": "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRcX2jgPxf-0yzrfV5piHuiKpDzSCGpJNbB0vywH-nqvmeL1-gL22uaZjb5",
"thumbnailHeight": 98,
"thumbnailWidth": 130
}
},
...
...
]
Run Code Online (Sandbox Code Playgroud)拉出图片网址,并使用图片加载器android library like Picasso(与之无关google picasa)下载缩略图和完整图片,以便在您的应用中使用
示例代码:
在android studio中创建一个带有空白活动的hello world应用程序.为"app"模块添加以下两行依赖项到build.gradle
compile 'com.loopj.android:android-async-http:1.4.5'
compile 'com.squareup.picasso:picasso:2.3.4'
Run Code Online (Sandbox Code Playgroud)ImageView在activity_main.xml中添加一个用于显示自定义Google图片搜索结果的内容
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:src="@drawable/ic_launcher" />
Run Code Online (Sandbox Code Playgroud)在onCreate,使用AsyncHttpClient 执行HTTPS GET 自定义Google搜索图像查询,并使用Picasso库显示您的第一个图像结果imageView
String imgQuery = "https://www.googleapis.com/customsearch/v1?q=unicorn&cx={CX_KEY}&searchType=image&key={API_KEY}";
new AsyncHttpClient().get(imgQuery, new AsyncHttpResponseHandler() {
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
String imgJson = new String(response);
String firstImage = imgJson.split("link\": \"")[1].split("\"")[0];
Picasso.with(getApplicationContext()).load(firstImage).into((ImageView) findViewById(R.id.imageView));
}
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {}
});
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
1615 次 |
| 最近记录: |