java - 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.
解决方案
你应该能够做这样的事情:
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);
推荐阅读
- java - 如何使用响应格式的改造因参数而异?
- sage - Sage:3D 绘图 - 纵横比
- javascript - 你能做一个允许你输入文本但不允许你删除文本的文本输入吗?
- python - Scrapy:所有请求都出现 403 错误
- java - 如何从arrayList中删除唯一元素
- c# - 如何在从 SQL Server 数据库获取数据的 DataGridView 中显示货币符号?
- php - 与 Google App Engine 上的 Cloud SQL 的 PHP (PDO) 连接不起作用
- c - 使用 C 以编程方式检查 Linux 中的僵尸子进程
- mysql - 如何在一个查询中包含三个不同的表数据
- r - 标题中的空格读取 csv