在volley中发送int和String作为param

时间:2017-10-03 07:26:22

标签: java android android-volley

我试图将凌空中的String和int作为params发送。但是如果我使用Map<String,Integer> params = new HashMap<String, String>();它会在String上显示错误并且如果我使用Map<String,String> params = new HashMap<String, String>();则在int上显示错误那么我如何在post请求中将int和String作为参数发送。

StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://www.aaa.com/insert/signup" ,
    new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.i("sign_up_res", response);
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("error" , error.toString());
        }
    }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();

            params.put("firstname" , PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString(Local_Preference.FIRSTNAME, "Not Set") );
            params.put("number" , "123");
            return params;
        }
    };
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
    1000,
    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(SignUp2Activity.this);
requestQueue.add(stringRequest);

在Sql表中就像firstname (varchar)number(int)。我不想在sql中将int类型更改为varchar。有没有办法在一个参数中发送intString

2 个答案:

答案 0 :(得分:2)

你可以这样做创建一个

  

Map params = new HashMap();

然后

params.put(key,value); // for string 
params.put(key,String.valueOf(value)); // for int As suggested  

答案 1 :(得分:2)

请使用此代码,我的问题已解决

String tag_json_obj = "json_obj_req";

String url = "https://api.androidhive.info/volley/person_object.json";

ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();     

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
                url, null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG, response.toString());
                        pDialog.hide();
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        pDialog.hide();
                    }
                }) {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("name", "Androidhive");
                params.put("email", "abc@androidhive.info");
                params.put("password", "password123");



                return params;
            }

        };

// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);