如何在另一个类或作用域中访问volley响应?

vdzxcuhz  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(244)

嗨,我在努力理解截击请求到底是如何工作的。我在nodejs中也做过类似的事情,过程似乎更直观。

String url = "http://my-json-feed";

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
        (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

    @Override
    public void onResponse(JSONObject response) {
        //All I want is to use the response outside this scope 
    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        // TODO: Handle error

    }
});

MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);

//Example: How can I access the response here or in any other class?

在nodejs中,相同的过程与此类似(在node fetch的帮助下)

const response = await fetch(url);
const json = await response.json();
console.log(json);

我基本上可以从同一个文件中的任何地方访问json,也可以从当前函数返回json,并在调用该函数的任何地方使用它。我想问一下如何在java中做类似的事情?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题