Reputation: 410
I'm trying to get url of image from json then using that url to download a image. But the problem is url gets fetched so late/slow that imagerequest reports urlFull is null. Even after using DataCallback interface. Any solution for this so that both request happen synchronously?
Edit- One strange thing I noticed is this code works perfectly when I click on button second time. But in first time it says Bad url null
I have set onclicklistener on a button which calls-
public void fetchSave(String photoJson) {
fetchData(new DataCallback() {
@Override
public void onBitmapSuccess(Bitmap result) {
Utils utils = new Utils(getActivity());
utils.saveImageSDCard(result);
}
@Override
public void onJsonSuccess(JSONObject result) {
try {
JSONArray mediaContentArray = result.getJSONObject(TAG_ENTRY).getJSONObject(TAG_MEDIA_GROUP).getJSONArray(TAG_MEDIA_CONTENT);
JSONObject mediaObject = (JSONObject) mediaContentArray.get(0);
urlFull = mediaObject.getString(TAG_IMG_URL);
} catch (JSONException e) {
e.printStackTrace();
}
Log.d(TAG, "Full image url " + urlFull);
}
}, photoJson);
}
fetchData() is as follows-
public void fetchData(final DataCallback callback, String photoJson) {
RequestFuture<JSONObject> future = newFuture();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, photoJson, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "JsonObject response= "+response.toString());
callback.onJsonSuccess(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), getString(R.string.toast_unknown_error), Toast.LENGTH_LONG).show();
}
});
jsonObjectRequest.setShouldCache(false);
AppController.getInstance().addToRequestQueue(jsonObjectRequest);
AppController.getInstance().getRequestQueue().getCache().remove(photoJson);
final ImageRequest imageRequest = new ImageRequest(urlFull, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
Log.d(TAG, "Bitmap response");
callback.onBitmapSuccess(response);
}
}, 0, 0, ImageView.ScaleType.CENTER_CROP, null, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), "Couldn't Download", Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
});
AppController.getInstance().addToRequestQueue(imageRequest);
}
Upvotes: 0
Views: 775
Reputation: 423
You can send second request in first request's onResponse() method so you will be sure that data is not null
Upvotes: 1
Reputation: 1421
Volley supports blocking request via RequestFutures. You create a normal request but set its callbacks as your request future, which is just volley's extension of a standard java futures. The call to future.get() will block.
It looks something like this
RequestFuture<JSONObject> future = RequestFuture.newFuture();
JsonObjectRequest request = new JsonObjectRequest(Method.POST, SIGNUP_URL, reqBody, future, future)
volleyRequestQueue.add(request);
try {
JSONObject response = future.get();
} catch (InterruptedException e) {
} catch (ExecutionException e) {
}
Upvotes: 0