如何使用Android中的Volley将整数和字符串数组JSON数据发送到服务器

时间:2018-06-04 11:14:25

标签: android json android-volley

这是我的ItemDetailActivity

这里我指定了整数和字符串值。因为我的主要任务是计算当我将价格与数量相乘时可以获得的总价值,为此我必须将字符串值转换为整数,以便我可以执行乘法,当我尝试时,下面的代码对我有效。只传递字符串值,但是当我将它们转换为整数时它会发送值,但除了'Items'值,我指定为字符串只存储在数据库中,剩余的'总数,价格和数量'被指定为整数存储为'0'(零)。如果同时显示字符串和整数值,我能否知道如何发送数据?

 itemname = Itemname.getText().toString();
 price = Integer.parseInt(Price.getText().toString());
 quantity = Integer.parseInt(Integer.toString(count));
 total = price * quantity;

 StringRequest stringRequest = new StringRequest(Request.Method.POST, server_url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    Toast.makeText(ItemDetailActivity.this, " "+itemname+" is added to your cart", Toast.LENGTH_SHORT).show();

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    Toast.makeText(ItemDetailActivity.this, "Some error went wrong!!", Toast.LENGTH_SHORT).show();
                    error.printStackTrace();
                }
            }){
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {

                    Map<String, String> params = new HashMap<>();
                    Map<String, Integer> param2 = new HashMap<String, Integer>();
                    params.put("Items",itemname);
                    param2.put("Price",price);
                    param2.put("Quantity",quantity);
                    param2.put("Total",total);

                    return params;
                }
            };

            MySingleton.getmInstance(ItemDetailActivity.this).addTorequestqueue(stringRequest);
        }

这是我的MySingleton课程

    private MySingleton (Context context){
    ctx = context;
    requestQueue = getRequestQueue();
}

public static synchronized  MySingleton getmInstance(Context context){
    if (mInstance == null){
        mInstance = new MySingleton(context);
    }
    return mInstance;
}

public RequestQueue getRequestQueue() {

    if (requestQueue == null){
        requestQueue = Volley.newRequestQueue(ctx.getApplicationContext());
    }
    return requestQueue;
}
public <T> void addTorequestqueue(Request<T> request ){

    requestQueue.add(request);
}

0 个答案:

没有答案