【问题标题】:How to send this type of data in post request in volley?如何在 volley 的 post 请求中发送此类数据?
【发布时间】:2016-09-29 05:24:16
【问题描述】:

这要在关键 Json 中发送 JsonObject。所以请告诉如何使用凌空发送它。 json={"product":"magie"}

如何在 Volley 中发送这些数据,我在下面添加了 asyncTask 代码,用于使用该类型数据的命中 api。

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

【问题讨论】:

标签: java android json android-volley


【解决方案1】:

对此的回答就像将数据放入 jsonObject 中,然后将 jsonObject 传递到带有键 json 的 hashmap 中。

在此处输入代码

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

【讨论】:

    猜你喜欢
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 2014-06-06
    • 2013-08-05
    相关资源
    最近更新 更多