簡體   English   中英

VOLLEY / ANDROID:發布請求但未收到參數

[英]VOLLEY / ANDROID : post request but parameters not received

我使用排球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);
}

但是,當我讀取服務器信息時,可以看到發布數組為空,但是當我驗證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> 

在volley的源代碼中查看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

基本上,您不能對“ JsonArrayRequest”使用POST方法。 JsonArrayRequest類可用於檢索JSON數組,但不能檢索JSON對象,到目前為止,僅支持HTTP GET 由於它僅支持GET,因此,如果要指定一些querystring參數,則將其附加在URL本身中。 構造函數不接受請求參數。

如果要以post方法發送數據,則可以使用“ JsonObjectRequest”“ 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方法中傳遞數據。 現在,您將在服務器端獲取數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM