Issue
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);
}
Solution
You can send second request in first request's onResponse() method so you will be sure that data is not null
Answered By - EtherPaul
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.