如何缩短有关 Stringrequest 的代码?
How can I shorten my codes about the Stringrequest?
我有一些方法可以从 google 应用程序脚本接收数据,但是为所有方法放置相同的代码有点长。
例如,这是我的 checkIn 方法,但我需要创建 checkOut 方法或 UpdateReservation 以从 google 中的 javascript 接收实际功能。有一种方法可以缩短这个 Volley Stringrequestcode。
或者至少我可以在 firebase 中使用那种编码或函数并且更容易吗?
private void checkIn() {
final RequestQueue requestQueue = Volley.newRequestQueue(Reservations.this);
final StringRequest stringRequest = new StringRequest(Request.Method.POST, "url",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
) {
@Override
protected Map<String, String> getParams() {
Map<String, String> parmas = new HashMap<>();
//here we pass params
parmas.put("action","checkIn");
return parmas;
}
};
int socketTimeOut = 50000;// u can change this .. here it is 50 seconds
RetryPolicy retryPolicy = new DefaultRetryPolicy(socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(retryPolicy);
requestQueue.add(stringRequest);
}
});
}
基本上你需要一种更紧凑的方式来编写那段代码。我说得对吗?
您可以使用 lambda 以更紧凑的方式编写侦听器:
StringRequest request = new StringRequest(Request.Method.POST, "url",
(response) -> {
//Handle response
},
(error) -> {
//Handle error
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> parmas = new HashMap<>();
//here we pass params
parmas.put("action","checkIn");
return parmas;
}
};
如果每个案例的 getParams(...)
方法只需向参数映射添加一个 action
键,您就可以创建一个 class 来扩展 StringRequest
并具有此功能。
public class CustomStringRequest extends StringRequest {
private String action;
public CustomStringRequest(int method, String url, String action, Response.Listener listener, Response.ErrorListener el) {
super(method, url, listener, el);
this.action = action;
}
@Override
protected Map<String, String> getParams() {
Map<String, String> parmas = new HashMap<>();
//here we pass params
parmas.put("action", action);
return parmas;
}
}
然后在每个方法中你可以这样写你的请求:
request = new CustomStringRequest(Request.Method.POST, "url", "checkIn",
(response) -> {
//Handle response
},
(error) -> {
//Handle error
});
请注意,"checkIn" 类型的操作已传递到 CustomStringRequest
构造函数中!
所以你终于得到了这样的东西
private void checkIn() {
final RequestQueue requestQueue = Volley.newRequestQueue(Reservations.this);
final StringRequest stringRequest = new CustomStringRequest(Request.Method.POST, "url", "checkIn",
(response) -> {
//Handle response
},
(error) -> {
//Handle error
});
int socketTimeOut = 50000;// u can change this .. here it is 50 seconds
RetryPolicy retryPolicy = new DefaultRetryPolicy(socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(retryPolicy);
requestQueue.add(stringRequest);
}
我有一些方法可以从 google 应用程序脚本接收数据,但是为所有方法放置相同的代码有点长。
例如,这是我的 checkIn 方法,但我需要创建 checkOut 方法或 UpdateReservation 以从 google 中的 javascript 接收实际功能。有一种方法可以缩短这个 Volley Stringrequestcode。
或者至少我可以在 firebase 中使用那种编码或函数并且更容易吗?
private void checkIn() {
final RequestQueue requestQueue = Volley.newRequestQueue(Reservations.this);
final StringRequest stringRequest = new StringRequest(Request.Method.POST, "url",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
) {
@Override
protected Map<String, String> getParams() {
Map<String, String> parmas = new HashMap<>();
//here we pass params
parmas.put("action","checkIn");
return parmas;
}
};
int socketTimeOut = 50000;// u can change this .. here it is 50 seconds
RetryPolicy retryPolicy = new DefaultRetryPolicy(socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(retryPolicy);
requestQueue.add(stringRequest);
}
});
}
基本上你需要一种更紧凑的方式来编写那段代码。我说得对吗?
您可以使用 lambda 以更紧凑的方式编写侦听器:
StringRequest request = new StringRequest(Request.Method.POST, "url",
(response) -> {
//Handle response
},
(error) -> {
//Handle error
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> parmas = new HashMap<>();
//here we pass params
parmas.put("action","checkIn");
return parmas;
}
};
如果每个案例的 getParams(...)
方法只需向参数映射添加一个 action
键,您就可以创建一个 class 来扩展 StringRequest
并具有此功能。
public class CustomStringRequest extends StringRequest {
private String action;
public CustomStringRequest(int method, String url, String action, Response.Listener listener, Response.ErrorListener el) {
super(method, url, listener, el);
this.action = action;
}
@Override
protected Map<String, String> getParams() {
Map<String, String> parmas = new HashMap<>();
//here we pass params
parmas.put("action", action);
return parmas;
}
}
然后在每个方法中你可以这样写你的请求:
request = new CustomStringRequest(Request.Method.POST, "url", "checkIn",
(response) -> {
//Handle response
},
(error) -> {
//Handle error
});
请注意,"checkIn" 类型的操作已传递到 CustomStringRequest
构造函数中!
所以你终于得到了这样的东西
private void checkIn() {
final RequestQueue requestQueue = Volley.newRequestQueue(Reservations.this);
final StringRequest stringRequest = new CustomStringRequest(Request.Method.POST, "url", "checkIn",
(response) -> {
//Handle response
},
(error) -> {
//Handle error
});
int socketTimeOut = 50000;// u can change this .. here it is 50 seconds
RetryPolicy retryPolicy = new DefaultRetryPolicy(socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(retryPolicy);
requestQueue.add(stringRequest);
}