Last active
April 20, 2017 10:32
-
-
Save ahsan/b5ca51aad0d88f2258a6fb542b76fe97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void getGiphy (String searchString) { | |
//URL Format: http://api.giphy.com/v1/gifs/search?q=cute+cat&api_key=dc6zaTOxFJmzC&limit=1&offset=0 | |
//Random Search URL: http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=cute+funny+cat+kitten | |
String apiKey = "dc6zaTOxFJmzC"; //Giphy's Public API Key | |
String giphyUrl = | |
"http://api.giphy.com/v1/gifs/random" + | |
"?tag=" + | |
searchString + | |
"&api_key=" + | |
apiKey; | |
if (isNetworkAvailable()) { | |
Log.d(TAG, giphyUrl); | |
OkHttpClient client = new OkHttpClient(); | |
Request request = new Request.Builder() | |
.url(giphyUrl) | |
.build(); | |
Call call = client.newCall(request); | |
call.enqueue(new Callback() { | |
@Override | |
public void onFailure(Call call, IOException e) { | |
} | |
@Override | |
public void onResponse(Call call, Response response) throws IOException { | |
try { | |
String jsonData = response.body().string(); | |
if (response.isSuccessful()) { | |
final String gif_url = getGifURL(jsonData); | |
Log.v(TAG, "Giphy Gif Data from Response: " + gif_url); | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
//updateDisplay(gif_url); | |
Glide.with(MainActivity.this) | |
.load(gif_url) | |
.thumbnail(0.1f) | |
.diskCacheStrategy(DiskCacheStrategy.SOURCE) | |
.fitCenter() | |
.into(new GlideDrawableImageViewTarget(imageView) { | |
@Override | |
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) { | |
super.onResourceReady(resource, animation); | |
button.setText("Search"); | |
button.setEnabled(true); | |
} | |
}); | |
} | |
}); | |
} else { | |
Log.i(TAG, "Response Unsuccessful"); | |
} | |
} | |
catch (IOException e) { | |
Log.e(TAG, "Exception Caught: ", e); | |
} | |
catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
}); | |
} else { | |
Toast.makeText(this, "Network is not available!", Toast.LENGTH_LONG).show(); | |
} | |
} | |
private String getGifURL(String jsonData) throws JSONException { | |
JSONObject giphy = new JSONObject(jsonData); | |
JSONObject data = giphy.getJSONObject("data"); | |
return data.getString("image_url"); | |
} | |
private boolean isNetworkAvailable() { | |
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo networkInfo = manager.getActiveNetworkInfo(); | |
boolean isAvailable = false; | |
if (networkInfo != null && networkInfo.isConnected()) { | |
isAvailable = true; | |
} | |
return isAvailable; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment