【问题标题】:how to POST jsonobject with volley如何用凌空POST jsonobject
【发布时间】:2020-05-31 17:29:00
【问题描述】:

我正在开发一个聊天室,并有一个从我的数据库中获取jsonArray 的请求,我想做一个POST jsonObject 请求在数据库中插入msg

public void getMsg(){
    String url = "http://192.168.1.57/android/leggi.php";
    final TextView chatView =(TextView) findViewById(R.id.chat);
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
            (Request.Method.POST, url, null, new Response.Listener<JSONArray>() {
                String msg = "";
                String mittente = "";
                @Override
                public void onResponse(JSONArray response) {

                    for (int i = 0; i<response.length(); i++) {
                        try {
                            mittente = response.getJSONObject(i).get("mittente").toString();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        try {
                            msg += (response.getJSONObject(i).get("mittente").toString() + ":\n" + response.getJSONObject(i).get("testo").toString() + "\n");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    chatView.setText(msg);
                    //chatView.setGravity(Gravity.RIGHT);
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO: Handle error

                }
            });

    MySingleton.getInstance(this).addToRequestQueue(jsonArrayRequest);

}

我可以使用相同的截击请求吗???或者请求必须有不同的参数???需要帮助:P

【问题讨论】:

    标签: android arrays json post android-volley


    【解决方案1】:

    这取决于您的服务器在 POST JSONObject 时的响应。

    如果服务器使用 JSONArray 响应,您可以使用相同的方式构建响应:

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
                (Request.Method.POST, url, INSERT_HERE, new Response.Listener<JSONArray>()
        ...
    {
    

    并在INSERT_HERE 位置插入您要发布的 JSON 对象。 如果您的回复不同,您需要更改Response.Listener&lt;JSONArray&gt;的类型

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      • 2019-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多