首页 > 解决方案 > How do you separate a nested method in a constructor to a separate method

问题描述

This is more of a style thing, and a self-study thing, but In the volley code are listeners, and all the code I find online involve just nesting a override method inside the constructor. Not necessarily something I'm used to from a C# background. Actually not too great at lambda, anonymous methods neither.

I have no idea where to start, because it doesn't seem intuitive to me right now. But I'd like to separate out nested methods to their own respective methods. Or maybe even just the overridden methods if that is the only part that is needed.

    final EditText textView = (EditText) findViewById(R.id.editText);

    RequestQueue queue = Volley.newRequestQueue(this);
    String url = "http://myURL";

    JSONObject postparams = new JSONObject();
    postparams.put("city", "london");
    postparams.put("name", "bob");

    // Request a string response from the provided URL.
    JsonObjectRequest  postRequest = new JsonObjectRequest(Request.Method.POST, url, postparams,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    textView.setText("Success: "+ response.toString());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    textView.setText(String.valueOf(error.networkResponse.statusCode));
                }
            }
    );
    // Add the request to the RequestQueue.
    queue.add(postRequest);

What I would like is to name a method in the 2 Response arguments within the same class or maybe this itself to do the override. Is it possible? Something like.

...
JsonObjectRequest  postRequest = new JsonObjectRequest(Request.Method.POST, url, postparams, myResponseListener(JSONObject response), myErrorResponseListener(VolleyError error));
// Add the request to the RequestQueue.
queue.add(postRequest);
}

public void myResponseListener(JSONObject response){
     textView.setText("Success: "+ response.toString());
}
public void myErrorResponseListener(VolleyError error){
    textView.setText(String.valueOf(error.networkResponse.statusCode));
}

Is there a simple way to something like this?

Edit: Trying linucksrox answer, to my surprise, the following actually stands alone as its own method... without a public(access modifier) or void(return type)??

    Response.Listener<JSONObject> myListener(JSONObject response)
    {
        return new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                textView.setText("Success: "+ response.toString());
            }
        };
    }

But when I try to plug in myListener as the 4th argument, it complains about the arguments.

myListener() no work, 
myListener(JSONOBject response) no work

The same is for the error section argument.

标签: javaandroidfunctionmethodsandroid-volley

解决方案


你应该能够做这样的事情:

    Response.Listener<JSONObject> myListener = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            textView.setText("Success: "+ response.toString());
        }
    };

    Response.ErrorListener myErrorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            textView.setText(String.valueOf(error.networkResponse.statusCode));
        }
    };

    // Request a string response from the provided URL.
    JsonObjectRequest  postRequest = new JsonObjectRequest(Request.Method.POST, url, postparams,
            myListener, myErrorListener);

推荐阅读