使用不同的参数多次抽取(面向对象)
问题描述:
在我的应用程序中,我使用非常多的次数,每次我需要创建类,因为不同的参数。使用不同的参数多次抽取(面向对象)
有没有办法重复使用不同的params多次?
private void sendReservation(String url) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//Creating parameters
Map<String, String> params = new Hashtable<String, String>();
//Adding parameters
User user = prefManager.getUser();
params.put("id", Id);
return postParams.checkParams(params);
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
int x = 0;// retry count
stringRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 48,
x, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(stringRequest);
}
上面我用类似的请求,它有相同的参数。
我需要的是重复使用相同的方法sendReservation()
与超过1个参数或什么params我通过。
假设我有一个线程后2个PARAMS,如:
params.put("id", Id);
params.put("name", name);
和另一个岗位3个PARAMS,如:
params.put("id", Id);
params.put("name", name);
params.put("type", type);
如何处理这个问题?
随时问任何问题。
答
这已经是Volley提供的简化代码,至少你在调用任何服务时必须设置参数。
但是仍然可以创建一个控制器类,您将发送一个serviceCode(由任何唯一编号创建)并根据serviceCode在控制器类和调用服务中创建您的参数。
在这种情况下,你只需要写两行代码。
例如,
ServiceController controller = new ServiceController(context);
controller.call(SERVICE_CODE, responseListner);
以上只是调用控制器类的示例代码。
编辑
这里是完整的代码,将帮助你。
创建ServiceController
类
import android.content.Context;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import com.cdnsol.interfaces.ConstantsLib;
import com.cdnsol.interfaces.UrlConstants;
import org.json.JSONObject;
import java.util.HashMap;
/**
* Created by vishalchhodwani on 28/8/17.
*/
public class ServiceController {
Context context;
public ServiceController(Context context)
{
this.context = context;
}
public void call(int serviceCode, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener)
{
if (serviceCode == ConstantsLib.LOGIN_REQUEST)
{
HashMap<String, String> stringParams = new HashMap<>();
stringParams.put("password", "123456");
stringParams.put("email", "[email protected]");
createRequest(stringParams, listener, errorListener);
}
if (serviceCode == ConstantsLib.SIGN_UP_REQUEST)
{
HashMap<String, String> stringParams = new HashMap<>();
stringParams.put("password", "123456");
stringParams.put("email", "[email protected]");
stringParams.put("name", "abc");
createRequest(stringParams, listener, errorListener);
}
}
private void createRequest(HashMap<String, String> params, Response.Listener<JSONObject> responseListener, Response.ErrorListener errorListener)
{
try
{
NetworkRequest networkRequest = new NetworkRequest(context, UrlConstants.LOGIN_URL, params,
true, responseListener, errorListener);
RequestQueue requestQueue = Volley.newRequestQueue(context);
int x = 0;// retry count
networkRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 48,
x, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(networkRequest);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
现在打电话从活动或片段您服务
ServiceController controller = new ServiceController(LoginActivity.this);
controller.call(ConstantsLib.LOGIN_REQUEST, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// Handle Response
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle Error
}
});
你的意思是'sendReservation(字符串URL,最后弦乐ID)'?或者'sendReservation(String url,final Map params)' –
@ cricket_007不,我的意思是,如果我有超过1个参数,并且我需要使用同一个类,我必须做什么? –
看看这个:https://stackoverflow.com/questions/35628142/how-to-make-separate-class-for-volley-library-and-call-all-method-of-volley-from我希望这可以帮助您。 –