Reputation: 23
Volley is returning null
value.
I am getting the response from Volley, but when I'am trying to return the imageurl, it shows null
value. Don't know why please help.
The source code is the following:
public class Mediaimage {
String imageurl;
Context context;
public Mediaimage(Context context) {
this.context = context;
}
public String getimageurl(String id){
String url="http://www.mytre.com/wp-json/wp/v2/media/"+id;
RequestQueue requestqueue = Volley.newRequestQueue(context);
StringRequest stringrequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
imageurl=response;
JSONObject jsonObject = new JSONObject(response);
String j = jsonObject.getString("guid");
jsonObject = new JSONObject(j);
imageurl =jsonObject.getString("rendered");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestqueue.add(stringrequest);
Log.i("imageurl",""+imageurl);
return imageurl;
}
}
NewsView.java File code
public class Imageloader implements Runnable {
@Override
public void run() {
Mediaimage mediaimages = new Mediaimage(NewsView.this);
contentimage_again = mediaimages.getimageurl(featuredmedia);
// contentimage_again = getimageurl(featuredmedia);
Log.i("jhggggggggggggggggj",""+contentimage_again);
synchronized (this){
try {
wait(4000);
// Log.i("jhggggggggggggggggj",""+contentimage_again);
ImageLoader.getInstance().displayImage(contentimage_again, contentimage);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Toast.makeText(getApplicationContext(), "hehe"+contentimage_again, Toast.LENGTH_SHORT).show();
}
}
Upvotes: 2
Views: 1856
Reputation: 1862
You can't get imageUrl with function like this. Because the request run Async and after that it received with onResponse method after a while. You have to change your method to void show some loading to user and when onReponse get called continue your proccess from onResponse method. You need an interface for call back and return it to fragment or activity. and from onResponse you can get imageURl from server and then set this to your imageView.
Here is callback interface class:
public interface ResponseCallback {
void onImageResponse(String imageURL);
}
media imageclass:
public class MediaImage {
String imageurl;
Context context;
ResponseCallback callback;
public Mediaimage(Context context, ResponseCallback callback) {
this.context = context;
this.callback=callback;
}
public void getimageurl(String id){
String url="http://www.mytre.com/wp-json/wp/v2/media/"+id;
RequestQueue requestqueue = Volley.newRequestQueue(context);
StringRequest stringrequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
imageurl=response;
JSONObject jsonObject = new JSONObject(response);
String j = jsonObject.getString("guid");
jsonObject = new JSONObject(j);
imageurl =jsonObject.getString("rendered");
callback.onImageResponse(imageurl);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestqueue.add(stringrequest);
}
}
And here is image loader class:
public class Imageloader implements Runnable {
@Override
public void run() {
Mediaimage mediaimages = new Mediaimage(NewsView.this);
mediaimages.getimageurl(featuredmedia, new ResponseCallback() {
@Override
public void onImageResponse(String imageUrl) {
ImageLoader.getInstance().displayImage(imageUrl, contentimage);
}
});
// contentimage_again = getimageurl(featuredmedia);
Log.i("jhggggggggggggggggj", "" + contentimage_again);
// Toast.makeText(getApplicationContext(), "hehe"+contentimage_again, Toast.LENGTH_SHORT).show();
}
}
Upvotes: 2
Reputation: 1694
Since you are trying to retrieve the imageUrl inside the background thread(ImageLoader thread itself) so you can make the thread to wait for your response to return
public String getimageurl(String id){
String url="http://www.mytre.com/wp-json/wp/v2/media/"+id;
RequestFuture<String> future = RequestFuture.newFuture();
RequestQueue requestqueue = Volley.newRequestQueue(context);
StringRequest stringrequest = new StringRequest(Request.Method.GET, url, future , future);
requestqueue.add(stringrequest);
try {
String response = future.get(10, TimeUnit.SECONDS); // Blocks for at most 10 seconds.
imageurl=response;
JSONObject jsonObject = new JSONObject(response);
String j = jsonObject.getString("guid");
jsonObject = new JSONObject(j);
imageurl =jsonObject.getString("rendered");
return imageurl;
} catch (InterruptedException e) {
// Exception handling
} catch (ExecutionException e) {
// Exception handling
} catch (TimeoutException e) {
e.printStackTrace();
}
Log.i("imageurl",""+imageurl);
return null;
}
Here actually you are waiting for the future object to return the response. This way you are making the caller thread to wait until the response is returned.
Note: This should be only be done if you are trying to make a Volley Request Already from Background Thread. DO NOT DO THIS IN MAIN THREAD
Upvotes: 2