简体   繁体   中英

switch case in getparams in volley

I am trying to achieve single function that can be used to do server calls for different URL request.To do this I need to send different parameters for different URL.

I am doing this and it is showing "Invalid Request". Can anyone suggest me how to achieve this.

@Override
        public Map<String, String> getParams()
        {
            Map<String, String> params = new HashMap<>();
            switch (URL)
            {
                case "REFERRALS_LIST":
                    params.put("customer_id", data.GetData(SharedDataHandler.CUSTOMER_ID));
                    params.put("first_name", argslist.get(0));
                    params.put("last_name", argslist.get(1));
                    params.put("address", argslist.get(2));
                    params.put("mobile", argslist.get(3));
                    params.put("email", argslist.get(4));
                    params.put("contact_home", argslist.get(5));
                    Log.d("test", "getParams: "+argslist.get(0)+argslist.get(1)+argslist.get(2)+argslist.get(3)+argslist.get(4)+argslist.get(5));
                    break;
            }
            return params;
        }

Hello Try this if it helps

1) performVolleyRequest is method for making volley request
2) I maintained Switch Case for Handling the various requests

3) You Have to use requestType variable according to your requirement as I left it blank I didn't know when your required it

public class MainActivity extends AppCompatActivity {



    public static final String KEY_USERNAME = "username";
    public static final String KEY_PASSWORD = "password";
    public static final String KEY_EMAIL = "email";
    String requestType="";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        switch (requestType){
            case "Registration":
                Map<String, String> dynamicParams = new HashMap<String, String>();
                dynamicParams.put(KEY_USERNAME, "userName");
                dynamicParams.put(KEY_PASSWORD, "password");
                dynamicParams.put(KEY_EMAIL, "email");
                final String REGISTER_URL_REG = "http://foo.com/volleyRegister.php";
                performVolleyRequest(dynamicParams,REGISTER_URL_REG);
                break;
            case "Login":
                Map<String, String> dynamicParamsLogin = new HashMap<String, String>();
                dynamicParamsLogin.put(KEY_USERNAME, "userName");
                dynamicParamsLogin.put(KEY_PASSWORD, "password");
                final String REGISTER_URL_LOGIN = "http://foo.com/volleyLogin.php";
                performVolleyRequest(dynamicParamsLogin,REGISTER_URL_LOGIN);
                break;
        }

    }



    private void performVolleyRequest(final Map<String, String> paramDynamic,final String REGISTER_URL) {
        StringRequest stringRequest = new StringRequest(DownloadManager.Request.Method.POST, REGISTER_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params=paramDynamic;
                return params;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM