Reputation: 11
I was trying to build a memesharing app with an meme Api call but when i launched the app on the emulator, the meme doesn't seem to load. I cant really understand where i'm going wrong. It would be really helpful if you could point out my mistake.
private void loadMeme()
{
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="https://meme-api.herokuapp.com/gimme";
// Request a string response from the provided URL.
JsonObjectRequest JsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
url,null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
ImageView memeImageView = (ImageView) findViewById(R.id.memeImageView);
Glide.with(MainActivity.this).load(url).into(memeImageView);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
// Access the RequestQueue through your singleton class. }
Upvotes: 1
Views: 98
Reputation: 308
You forgot fetch the image url from your API and also forgot to add request to the request queue in Volley. Add this code to your project it will work
private void loadMeme() {
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url = "https://meme-api.herokuapp.com/gimme";
// Request a string response from the provided URL.
JsonObjectRequest JsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
String imageUrl = response.getString("url");
ImageView memeImageView = (ImageView) findViewById(R.id.memeImageView);
Glide.with(MainActivity.this).load(imageUrl).into(memeImageView);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(JsonObjectRequest);
}
Upvotes: 1