【问题标题】:VOLLEY / ANDROID : post request but parameters not receivedVOLLEY / ANDROID:发布请求但未收到参数
【发布时间】:2015-08-13 09:58:46
【问题描述】:

我使用 volley Android 库,我创建了发布请求而不是发送到我的服务器:

    JSONArray jsonRequest = new JSONArray();
    for(MyLocation myLocation : ListLocation){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("serial", myLocation.serial);
            jsonObject.put("longitude", myLocation.longitude);
            jsonObject.put("latitude", myLocation.latitude);
            jsonObject.put("altitude", myLocation.altitude);
            jsonObject.put("accuracy", myLocation.accuracy);
            jsonObject.put("date", myLocation.date);
            jsonRequest.put(jsonObject);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    // Request a string response from the provided URL.
    JsonArrayRequest stringRequest = new JsonArrayRequest(url, jsonRequest, 
                new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            dabAcces.dropTable();
            Log.d(TAG, "dropTable");
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            if (row > MAX_REGISTER_GPS_DATA) {
                Log.d(TAG, "deleteOldestRecord");
                dabAcces.deleteOldestRecord();
            }
        }
    });
    // Add the request to the RequestQueue.
    queue.add(stringRequest);
}

但是当我阅读我的服务器信息时,我可以看到 post 数组是空的,但是当我验证 jsonarray 内容时,我看到了 json 格式的变量。请帮帮我

[2015-08-13 09:30:39] local.INFO: POST 
[2015-08-13 09:30:39] local.INFO: array ()  
[2015-08-13 09:30:39] local.INFO: GET ------------------------------   
[2015-08-13 09:30:39] local.INFO: array ()  
[2015-08-13 09:30:39] local.INFO: SERVER ------------------------------   
[2015-08-13 09:30:39] local.INFO: array (
  'REDIRECT_STATUS' => '200',
  'CONTENT_TYPE' => 'application/json; charset=utf-8',
  'HTTP_USER_AGENT' => 'Dalvik/1.6.0 (Linux; U; Android 4.1.2; M17-G903-A Build/JZO54K)',
  'HTTP_HOST' => 'geoloc.com',
  'HTTP_CONNECTION' => 'Keep-Alive',
  'HTTP_ACCEPT_ENCODING' => 'gzip',
  'CONTENT_LENGTH' => '68921',
  'PATH' => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
  'SERVER_SIGNATURE' => '<address>Apache/2.4.10 (Ubuntu) Server at geoloc.com Port 80</address> 'SERVER_SIGNATURE' => '<address>Apache/2.4.10 (Ubuntu) Server at geoloc.com Port 80</address>

【问题讨论】:

  • 基本上你不能对“JsonArrayRequest”使用 POST 方法 JsonArrayRequest 目前支持 GET 方法,arnab.ch/blog/2013/08/…
  • 你可以用post..那篇博文已经过时了..

标签: java android json post android-volley


【解决方案1】:

在凌空的source codeJsonArrayRequest

public JsonArrayRequest(String url, JSONArray jsonRequest, Listener<JSONArray> listener,
                        ErrorListener errorListener) {
    this(jsonRequest == null ? Method.GET : Method.POST, url, jsonRequest,
            listener, errorListener);
}

这意味着你需要传递一个 JsonArray 而不是一个 JsonObject

【讨论】:

  • 我已经用更新的源代码更新了我的答案。请检查一下
【解决方案2】:

"JsonArrayRequest" 基本上不能使用 POST 方法。 JsonArrayRequest 类可用于检索 JSON 数组,但不能检索 JSON 对象,目前仅支持 HTTP GET。由于它仅支持 GET,因此如果您要指定一些查询字符串参数,则将这些参数附加到 URL 本身中。构造函数不接受请求参数。

如果你想用post方法发送数据,你可以使用"JsonObjectRequest" OR "StringRequest"

使用“StringRequest”在POST方法中发送数据的小例子

StringRequest strReq = new StringRequest(Request.Method.POST,
                URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    Log.d(TAG, "Response: " + response.toString());
                } catch (JSONException e) {
                    // JSON error
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Error: " + error.getMessage());
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("param1", "value1");
                params.put("param2","value2");
                return params;
            }

        };

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq);

您应该重写 getParams() 方法以在 POST 方法中传递数据。现在您将在服务器端获取数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多