如何建立一个适当的凌空监听器的跨级排球方法调用监听器、建立一个、排球、适当

2023-09-07 14:10:46 作者:如儿戏

我的目标是从另一个类中调用排球,一个非常简洁的,模块化的方式,即:

I aim to call Volley from another class in, a very succinct, modular way ie:

            VolleyListener newListener = new VolleyListener();
            VolleySingleton.getsInstance().somePostRequestReturningString(getApplicationContext(), newListener);
            JSONObject data = newListener.getResponse();

但我有麻烦让听者部分配发工作,以便能够从一个方法来访问所产生的数据,如

But am having allot of trouble getting the listener portion to work so as to be able to access the resulting data from a method such as

newListener.getResponse();

有在这个网站上一般概述了如何建立一个凌空呼叫从另一个类,如几个问题:Android排球 - 如何隔离在另一个类请求。我已经成功获得方法调用工作,但到现在为止得到的数据到present类的使用已经引起了麻烦。

There are a few questions on this site that generally outline how to set up a volley call from another class, such as: Android Volley - How to isolate requests in another class. I have had success getting the method call to work, but to now get that data into the present class for usage has caused trouble.

我是在我的 VolleySingleton 类的动作:

public void somePostRequestReturningString(final Context context,final VolleyListener<String> listener) {

        final String URL = "http://httpbin.org/ip";

        JsonObjectRequest set = new JsonObjectRequest(Request.Method.GET, URL, ((String) null),
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        listener.outPut = response.toString();
                        //Toast.makeText(context, response.toString(), Toast.LENGTH_LONG).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d("Error.Response", error.toString());
                    }
                }
        );

        mRequestQueue.add(set);
}

和监听器类中:

public class VolleyListener {
    public static String outPut;

    private static Response.Listener<String> createSuccessListener() {
        return new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                outPut = response;
            }
        };
    }
}

我如何配置这个工作,让排球从另一个类调用和数据检索,特别是如何正确地构建回调?

How can I configure this to work and allow Volley calls and data retrieval from another class, particularly how to build callbacks correctly?

推荐答案

有关您的要求,我建议你向我如下的解决方案,希望这是明确的和有益的:

For your requirement, I suggest you refer to my following solution, hope it's clear and helpful:

首先是接口的:

public interface VolleyResponseListener {
    void onError(String message);

    void onResponse(Object response);
}

那么你的助手类(我将其命名为 VolleyUtils 类)内:

public static void makeJsonObjectRequest(Context context, String url, final VolleyResponseListener listener) {
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
                (url, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    listener.onResponse(response);
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    listener.onError(error.toString());
                }
            }) {

        @Override
        protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
            try {
                String jsonString = new String(response.data,
                        HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
                return Response.success(new JSONObject(jsonString),
                        HttpHeaderParser.parseCacheHeaders(response));
            } catch (UnsupportedEncodingException e) {
                return Response.error(new ParseError(e));
            } catch (JSONException je) {
                return Response.error(new ParseError(je));
            }
        }
    };

    // Access the RequestQueue through singleton class.
    VolleySingleton.getInstance(context).addToRequestQueue(jsonObjectRequest);
}

那么,你的Activity类里面,你可以调用如下所示:

Then, inside your Activity classes, you can call like the following:

VolleyUtils.makeJsonObjectRequest(mContext, url, new VolleyResponseListener() {
        @Override
        public void onError(String message) {

        }

        @Override
        public void onResponse(Object response) {

        }
    });

您可以参考更多的信息,下面的问题(正如我昨天跟你说):

You can refer to the following questions for more information (as I told you yesterday):

Android:如何从使用方法凌空返回异步的JSONObject?

POST请求JSON文件传递的字符串并等待回应凌空

Android/Java:如何延缓回报的方法