在第一次从方法调用时,Volley返回null [英] Volley returns null on first call from method

查看:118
本文介绍了在第一次从方法调用时,Volley返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用齐射从服务器检索数据,但是当我第一次调用此方法时,我从服务器获得了响应,但是该方法返回null.如果我第二次打电话给我,则得到最后的答复.

I am trying to retrieve data from server using volley, but when I call this method the first time, I get the response from server, but null is returned by the method. If I call it the second time I get the last response.

 public String retrieveDataFromServer(String url, String param, final String token){


        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try{
                            data =  new JSONObject(response).toString();
                        }catch (Exception e){}
                        //Toast.makeText(getApplicationContext(), "wow" + data, Toast.LENGTH_SHORT).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        try{
                            data = new JSONObject(error.toString()).toString();
                        }catch (Exception e){}
                        //Toast.makeText(getApplicationContext(), "" +data, Toast.LENGTH_SHORT).show();
                    }
                }) {
            /**
             * Passing some request headers
             */
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String bearer = "Bearer ".concat(token);
                Map<String, String> headersSys = super.getHeaders();

                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Content-Type", "application/json; charset=utf-8");
                //headers.put("token", token);

                headersSys.remove("Authorization");
                headers.put("Authorization", bearer);
                headers.putAll(headersSys);
                return headers;
            }
        };
        // Adding request to request queue
        addToRequestQueue(stringRequest);
        //Toast.makeText(getApplicationContext(), "wow" + data, Toast.LENGTH_SHORT).show();

        return data;
}

如何在第一次调用方法时得到响应?

How do I get the response on first call of method?

推荐答案

您可以使用回叫来返回Volley响应:

You can use call back to return Volley response:

public void retrieveDataFromServer(final VolleyCallback callback) {
StringRequest strReq = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        callback.onSuccess(response);
    }
}
}}

创建界面:

public interface VolleyCallback{
    void onSuccess(String response);
}

从活动中获取结果:

String yourString = "";
@override
public void onResume() {
    super.onResume();
    retrieveDataFromServer(new VolleyCallback(){
        @Override
        public void onSuccess(String response){
          //Get result from here
             yourString = response;
        }
    });
}

这篇关于在第一次从方法调用时,Volley返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆