为什么Volley会返回null值?

时间:2017-02-08 11:30:03

标签: java android android-volley

排球正在返回null值。

我收到来自 Volley 的回复,但是当我尝试返回 imageurl 时,它会显示null值。不知道为什么请帮忙。

源代码如下:

 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文件代码

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();


    }
}

2 个答案:

答案 0 :(得分:2)

你无法使用这样的函数获得imageUrl。因为请求运行Async,之后它会在一段时间后收到onResponse方法。你必须将你的方法改为void,向用户显示一些加载,当onReponse被调用时,从onResponse方法继续你的proccess。您需要一个用于回调的接口并将其返回到片段或活动。从onResponse你可以从服务器获取imageURl,然后将其设置为imageView。

这是回调接口类:

public interface ResponseCallback {
void onImageResponse(String imageURL);
}

媒体形象类:

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);
    }
   }

这是图像加载器类:

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();


    }
}

答案 1 :(得分:2)

因为你试图在后台线程(ImageLoader线程本身)中检索imageUrl所以你可以让线程等待你的响应返回

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;
}

实际上,您正在等待将来的对象返回响应。这样,您就可以使调用者线程等到返回响应。

注意:只有在您尝试从后台线程制作排球请求时才应该这样做。不要在主线上做这个