繁体   English   中英

发送int和String作为参数在凌空

[英]Send int and String as param in volley

我试图发送String和int作为凌空的参数。 但是如果我使用Map<String,Integer> params = new HashMap<String, String>();它会在String上显示错误Map<String,Integer> params = new HashMap<String, String>(); 如果我使用Map<String,String> params = new HashMap<String, String>();并在int上显示错误 因此,如何在请求后发送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

你可以这样做创建一个

地图参数= new HashMap();

然后

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

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

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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