Skip to content

Commit

Permalink
Merge pull request #670 from jlhonora/master
Browse files Browse the repository at this point in the history
Update image search sample
  • Loading branch information
koush authored Sep 23, 2016
2 parents 7ddf485 + 32da7e9 commit 0162b5c
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The included documented [ion-sample](https://github.com/koush/ion/tree/master/io
* Populate a ListView Adapter and fetch more data as you scroll to the end
* Put images from a URLs into ImageViews (twitter profile pictures)
* File Download with [Progress Bar Sample](https://github.com/koush/ion/blob/master/ion-sample/src/com/koushikdutta/ion/sample/ProgressBarDownload.java)
* Get JSON and show images with the [Google Image Search Sample](https://github.com/koush/ion/blob/master/ion-sample/src/com/koushikdutta/ion/sample/GoogleImageSearch.java)
* Get JSON and show images with the [Image Search Sample](https://github.com/koush/ion/blob/master/ion-sample/src/com/koushikdutta/ion/sample/ImageSearch.java)

#### More Examples

Expand Down
8 changes: 2 additions & 6 deletions ion-sample/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
android:versionCode="1"
android:versionName="1.0">

<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17"/>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Expand All @@ -30,8 +26,8 @@
android:name=".ProgressBarUpload"
android:label="Progress Bar Upload"/>
<activity
android:name=".GoogleImageSearch"
android:label="Google Image Search"/>
android:name=".ImageSearch"
android:label="Image Search"/>
<activity
android:name=".GallerySample"
android:label="Gallery Sample"/>
Expand Down
24 changes: 20 additions & 4 deletions ion-sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-alpha3'
}
}

allprojects {
repositories {
jcenter()
}
}

apply plugin: 'com.android.application'

dependencies {
compile 'com.makeramen:roundedimageview:+'
compile 'com.github.chrisbanes.photoview:library:+'
compile 'com.flaviofaria:kenburnsview:1.0.6'
compile project(':ion:ion')
// compile project(':ion:ion')
compile 'com.koushikdutta.ion:ion:2.+'
}

android {
Expand All @@ -18,11 +34,11 @@ android {
}

defaultConfig {
targetSdkVersion 21
targetSdkVersion 23
minSdkVersion 9
}

compileSdkVersion 21
buildToolsVersion '21.1.2'
compileSdkVersion 23
buildToolsVersion '23.0.2'
}

File renamed without changes.
4 changes: 2 additions & 2 deletions ion-sample/res/layout/samples.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
android:layout_height="match_parent">

<Button
android:id="@+id/google_image_search"
android:id="@+id/image_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Google Image Search" />
android:text="Image Search" />

<Button
android:id="@+id/gallery"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
Expand All @@ -13,6 +12,7 @@
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
Expand All @@ -23,7 +23,7 @@
/**
* Created by koush on 6/4/13.
*/
public class GoogleImageSearch extends Activity {
public class ImageSearch extends Activity {
private MyAdapter mAdapter;

// Adapter to populate and imageview from an url contained in the array adapter
Expand Down Expand Up @@ -60,9 +60,11 @@ void loadMore() {
if (loading != null && !loading.isDone() && !loading.isCancelled())
return;

String url = getApiUrl(searchText.getText().toString(), mAdapter.getCount());

// query googles image search api
loading = Ion.with(GoogleImageSearch.this)
.load(String.format("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=%s&start=%d&imgsz=medium", Uri.encode(searchText.getText().toString()), mAdapter.getCount()))
loading = Ion.with(ImageSearch.this)
.load(url)
// get the results as json
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
Expand All @@ -71,21 +73,51 @@ public void onCompleted(Exception e, JsonObject result) {
try {
if (e != null)
throw e;
// find the results and populate
JsonArray results = result.getAsJsonObject("responseData").getAsJsonArray("results");
for (int i = 0; i < results.size(); i++) {
mAdapter.add(results.get(i).getAsJsonObject().get("url").getAsString());
}
processApiResult(result);
}
catch (Exception ex) {
// toast any error we encounter (google image search has an API throttling limit that sometimes gets hit)
// Toast.makeText(GoogleImageSearch.this, ex.toString(), Toast.LENGTH_LONG).show();
// toast any error we encounter (most image search APIs have a throttling limit that sometimes gets hit)
Toast.makeText(ImageSearch.this, ex.toString(), Toast.LENGTH_LONG).show();
}

}
});
}

/**
* Build the url for the next request. Uses Flickr's API
*
* https://www.flickr.com/services/feeds/docs/photos_public/
*
* @param text Search text
* @param page Current page we're in
* @return Url for the next request
*/
String getApiUrl(String text, int page) {
String base = "https://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=?";
if (text != null && !text.isEmpty()) {
base += "&tags=" + text;
}

return base;
}

/**
* Process a successfull API result
* @param result the API's response
*/
void processApiResult(JsonObject result) {
processFlickrApiResult(result);
}

void processFlickrApiResult(JsonObject result) {
// find the results and populate
JsonArray results = result.getAsJsonArray("items");
for (int i = 0; i < results.size(); i++) {
mAdapter.add(results.get(i).getAsJsonObject().getAsJsonObject("media").get("m").getAsString());
}
}

EditText searchText;
/**
* Called when the activity is first created.
Expand All @@ -96,7 +128,7 @@ public void onCreate(Bundle savedInstanceState) {

Ion.getDefault(this).configure().setLogging("ion-sample", Log.DEBUG);

setContentView(R.layout.google_image_search);
setContentView(R.layout.image_search);

final Button search = (Button) findViewById(R.id.search);
searchText = (EditText) findViewById(R.id.search_text);
Expand Down
6 changes: 3 additions & 3 deletions ion-sample/src/com/koushikdutta/ion/sample/Samples.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public void onClick(View v) {
}
});

Button googleImageSearch = (Button)findViewById(R.id.google_image_search);
googleImageSearch.setOnClickListener(new View.OnClickListener() {
Button imageSearch = (Button)findViewById(R.id.image_search);
imageSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Samples.this, GoogleImageSearch.class));
startActivity(new Intent(Samples.this, ImageSearch.class));
}
});

Expand Down

0 comments on commit 0162b5c

Please sign in to comment.