【问题标题】:android volley for posting jsonobject用于发布 jsonobject 的 android 凌空
【发布时间】:2014-03-25 08:29:10
【问题描述】:

我们可以使用 Android volley 将 JsonObject 或 JsonArray 发布到服务器吗?如果可以,任何人都可以发送工作示例。 如果我发布字符串,那么它工作正常,但我在发布 jsonObject 或 jsonArray 时遇到了一些问题。

【问题讨论】:

    标签: php android android-volley


    【解决方案1】:

    是的,你可以。 JSON 被添加到 POST 请求的正文中,如下所示:

    JSONObject jsonObject; // your JSON here
    requestQueue.add(new JsonObjectRequest(Method.POST, url, jsonObject, listener, errorListener);
    

    【讨论】:

    • 以及如何通过php在服务器上检索它?
    • 我对php不太了解,但是你肯定可以得到一个http请求的正文。
    【解决方案2】:

    是的,只需将数据添加到 JSONObject 并在请求中传递该对象。

    JSONObject jsonObject = new JSONObject();

            jsonObject.put("name", name);
            jsonObject.put("password", password);
    
            RequestQueue queue = Volley.newRequestQueue(context);
            JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, URL, jsonObject, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
     Log.i("volley", "response: " + response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                     Log.i("volley", "error: " + error);
                }
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多