Volley JsonObjectRequest响应

时间:2018-09-26 02:31:11

标签: android json android-volley jsonobjectrequest request-queueing

我正在使用Volley JSONObjectRequest通过JSON从服务器检索数据以实现此目的。得到JSON响应后,我想将其保存到变量中并在整个活动中使用它。下面是我的代码示例:

private String description;
private int status;
private boolean validateIntegrationCode() {
    if (checkNetwork()) {
        String url = "sample url";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    status = response.getInt("status");// ** STATUS IS 1 HERE **
                    description = response.getString("description");

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        mQueue.add(request);
    } else {
        // No network, check from SQLite
    }
    if (status == 1) { // ** STATUS IS 0 HERE **
        return true;
    } else {
        return false;
    }
}

当我在返回statustrue之前检查false的值时,status的值现在为0,而description也返回{{1 }}。

我的JSON响应:

null

3 个答案:

答案 0 :(得分:2)

我的回答如下更新,

    private int status;
    private String description;

    private void callRequest() {

        // Initialize a new RequestQueue instance
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);

        // Initialize a new JsonObjectRequest instance
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                Request.Method.GET,
                "URL",
                null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            status = response.getInt("status");// ** STATUS IS 1 HERE **
                            description = response.getString("description");

                            checkStatus();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }
        );

        // Add JsonObjectRequest to the RequestQueue
        requestQueue.add(jsonObjectRequest);
    }

    private void checkStatus(){
        if(status == 1){
            // do what do you need to do id status == 1
        }else{
            // do what do you need to do id status == 0
        }
    }

尝试一下,让我知道更新 谢谢!

答案 1 :(得分:1)

截击请求是异步调用。在收到响应之前,它不会阻塞代码。因此,正在发生的情况是在收到截击响应之前您正在检查if(status==1)。因此它是0。

您应该做什么:

1)使函数返回类型为void。

2)在onResponse中,输入代码

if (status == 1) { // ** STATUS IS 0 HERE **
        statusBoolean = true;
    } else {
        statusBoolean = false;
}

答案 2 :(得分:0)

您应将状态声明为 int ,而不是 String

private int status=0;

您可以尝试这种方式

                status = response.getInt("status");
                description = response.getString("description");
                if (status == 1) { 
                 checkingStatus(status);
                } else {
                 checkingStatus(status);
                }

然后

public void checkingStatus(int status){
        if(status == 1){
            // do your code
        }else{
            // do your code
        }
    }