如何从Java接口中获取值

发布于 2025-01-24 01:43:32 字数 704 浏览 3 评论 0原文

我在接口方法中有一个变量,并且我希望它在接口之外:

String responseString;
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                // i want this value out!
                responseString = response.toString();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.println(error.toString());
            }
        });

当然,我不能只分配wenderseString = wendesp.tostring(),因为它们具有不同的范围。那我该怎么做呢?

大概这个问题已经回答,但我在网上找不到类似的东西。

I have a variable inside an interface method and I want it outside the interface:

String responseString;
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                // i want this value out!
                responseString = response.toString();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.println(error.toString());
            }
        });

Of course I can't just assign responseString = response.toString() because they have different scopes. So how can I do this?

Probably this question has already been answered but I couldn't find anything similar online.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

停顿的约定 2025-01-31 01:43:32

根据JSON结构或JSONOBJECT尝试一次。

//JSON Array
    private JSONArray result;

  //Creating a string request
        StringRequest stringRequest = new StringRequest(URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        JSONObject j = null;
                        try {
                            //Parsing the fetched Json String to JSON Object
                            j = new JSONObject(response);
 
                            //Storing the Array of JSON String to our JSON Array
                            result = j.getJSONArray("your key");
 
                //OPTIONAL function
                            functionName(result);//If using function
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
 
                    }
                });

Try this once as per your JSON structure Either JSONArray or JSONObject.

//JSON Array
    private JSONArray result;

  //Creating a string request
        StringRequest stringRequest = new StringRequest(URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        JSONObject j = null;
                        try {
                            //Parsing the fetched Json String to JSON Object
                            j = new JSONObject(response);
 
                            //Storing the Array of JSON String to our JSON Array
                            result = j.getJSONArray("your key");
 
                //OPTIONAL function
                            functionName(result);//If using function
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
 
                    }
                });
江南烟雨〆相思醉 2025-01-31 01:43:32

新响应。listener&lt; string&gt;(){...}所使用的方法称为“匿名”实现。确实,您正在实现接口,但是您在代码中没有对其进行任何静态引用。

您应该做的是,要实现正常实现界面,哪些界面当然会实现所需的方法,还可以容纳一个领域和特殊的领域:

public class YourStringListener implements Response.Listener<String> {

    private String responseString; //<-- declare a field

    @Override
    public void onResponse(String response) {
        this.responseString = response.toString(); //<-- set the field
    }

    public String getResponseString() {
        return this.responseString; //<-- get the field from outside
    }

}

现在,您应该能够做到这一点:

String responseString;
YourStringListener listener = new YourStringListener(); //<-- don't implement anonimously, use the concrete implementation
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, listener, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.println(error.toString());
        }
    });
System.out.println(listener.getResponseString()); //<-- access the value of response string from the concrete implementation instance

当然,请小心你是一个听众。该值将为null,直到方法OnResponse将其同步回电为止。

The new Response.Listener<String>() {...} that you're doing in that method is called "anonymous" implementation. Indeed you are implementing the interface, but you don't hold any static reference to it in the code.

What you should do, is to have a normal implementation of the interface which of course implements the method required, but also holds a field and a special getter for your field:

public class YourStringListener implements Response.Listener<String> {

    private String responseString; //<-- declare a field

    @Override
    public void onResponse(String response) {
        this.responseString = response.toString(); //<-- set the field
    }

    public String getResponseString() {
        return this.responseString; //<-- get the field from outside
    }

}

Now, you should be able to do this:

String responseString;
YourStringListener listener = new YourStringListener(); //<-- don't implement anonimously, use the concrete implementation
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, listener, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.println(error.toString());
        }
    });
System.out.println(listener.getResponseString()); //<-- access the value of response string from the concrete implementation instance

Of course, be careful you're into a listener. The value will be null until when the method onResponse will have asynchronously been called back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文