Java Can';t将值赋给全局变量,使用截取请求(Android Studio) JsonObjectRequest requestM=newjsonobjectrequest(Request.Method.GET,moveUrl,null,new Response.Listener()){ @凌驾 公共void onResponse(JSONObject响应){ 试一试{ typeVariable=response.getJSONObject(“类型”).getString(“名称”); }捕获(JSONException e){ System.out.println(“捕捉错误”); } } },new Response.ErrorListener(){ @凌驾 公共无效onErrorResponse(截击错误){ System.out.println(“侦听器错误”); } }); requestQueue.add(requestM); 返回类型变量; }

Java Can';t将值赋给全局变量,使用截取请求(Android Studio) JsonObjectRequest requestM=newjsonobjectrequest(Request.Method.GET,moveUrl,null,new Response.Listener()){ @凌驾 公共void onResponse(JSONObject响应){ 试一试{ typeVariable=response.getJSONObject(“类型”).getString(“名称”); }捕获(JSONException e){ System.out.println(“捕捉错误”); } } },new Response.ErrorListener(){ @凌驾 公共无效onErrorResponse(截击错误){ System.out.println(“侦听器错误”); } }); requestQueue.add(requestM); 返回类型变量; },java,android-studio,Java,Android Studio,typeVariable在类字段中定义,typeVariable在try(have checked)中被正确分配,但此方法仍然返回null 我仍然是一个乞丐,所以我可能在做一些愚蠢的事情 谢谢你的帮助 您可以将其设置为全局变量,但它是异步的;您无法知道值何时实际被响应填充。在请求完成之前,请将其返回。感谢您的快速响应!我如何知道请求何时结束?当onResponse或运行错误侦听器时。更大的图景:编程中有很多异步的东西;你需要把你的头绕在这上面。 JsonObjectRequest

typeVariable在类字段中定义,typeVariable在try(have checked)中被正确分配,但此方法仍然返回null

我仍然是一个乞丐,所以我可能在做一些愚蠢的事情


谢谢你的帮助

您可以将其设置为全局变量,但它是异步的;您无法知道值何时实际被响应填充。在请求完成之前,请将其返回。感谢您的快速响应!我如何知道请求何时结束?当
onResponse
或运行错误侦听器时。更大的图景:编程中有很多异步的东西;你需要把你的头绕在这上面。
        JsonObjectRequest requestM = new JsonObjectRequest(Request.Method.GET, moveUrl, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    typeVariable = response.getJSONObject("type").getString("name");
                } catch (JSONException e) {
                    System.out.println("catch ERROR");
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.println("Listener ERROR");
            }
        });
        requestQueue.add(requestM);
        return typeVariable;
    }