VOLLEY / ANDROID:发布请求但未收到参数

时间:2015-08-13 09:58:46

标签: java android json post android-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格式。帮帮我PLZ

&#13;
&#13;
[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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

在排球&#39; source code

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

这意味着您需要传递JsonArray而不是JsonObject

答案 1 :(得分:0)

基本上你不能对&#34; JsonArrayRequest&#34; 使用POST方法。 JsonArrayRequest类可用于检索JSON数组但不能用于检索JSON对象,目前仅支持 HTTP GET 。因为它只支持GET,所以如果你要指定一些查询字符串参数,那么将它们附加到URL本身。构造函数不接受请求参数。

如果您想以发布方式发送数据,可以使用&#34; JsonObjectRequest&#34; &#34; StringRequest&#34;

使用&#34; StringRequest&#34;

在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方法中传递数据。现在,您将在服务器端获取数据。