如何在截击请求中发送此类数据?

时间:2016-09-29 05:24:17

标签: java android json android-volley

这想要在密钥Json中发送JsonObject。所以请告诉我如何用凌空发送它。     JSON = { “产品”: “MAGIE”}

如何在Volley中发送这些数据,我已经为这种类型的数据添加了针对api的asyncTask代码。

enter code here

protected void onPreExecute() {
    if (progress)
        GlobalAlerts.showProgressDialog(context);
}

@Override
protected String doInBackground(String... params) {
    String resp = null;
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("product","magie");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    String data = "json=" + jsonObject.toString(); 
    String url ="http://anc.php";
    resp = new JsonCall().executeHttpPostRequest(url, data);
    return resp;
}

protected void onPostExecute(String resp) {
    if (progress)
        GlobalAlerts.hideProgressDialog();
    if (resp != null) {
        callback.onTaskComplete(resp);
    } else {
        GlobalAlerts.singleAlert((Activity) context, context.getString(R.string.warning), "Error", false);
    }
}

1 个答案:

答案 0 :(得分:0)

答案就像将数据放入jsonObject中,然后使用密钥json在hashmap中传递jsonObject。

在这里输入代码

    String url = "http://anc.php";
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("product", "magie");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    final HashMap<String,String> hashMap2 = new HashMap<>();
    hashMap2.put("json",jsonObject.toString());

    StringRequest strReq = new StringRequest(Request.Method.POST,
            url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.e("order_list", response);
            GlobalAlerts.hideProgressDialog();
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Error", "Error: " + error.getMessage());
            GlobalAlerts.hideProgressDialog();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            /*Map<String,String> params = new HashMap<>();
            params.put("employee_id"," 1");*/
            return hashMap2;
        }
    };
    Application.getInstance().addToRequestQueue(strReq, "order_list");
相关问题