โพสต์ JsonObject ด้วย Volley

ฉันกำลังพยายามส่งคำขอโพสต์ด้วยการวอลเลย์ไม่สำเร็จ

lib ทำงานอย่างถูกต้อง และฉันจัดการเพื่อส่งคำขอสตริงบางส่วนได้ แต่การโพสต์ด้วย JsonObject ไม่ทำงาน

String urlJsonReq = "https://api.parse.com/1/classes/GameScore";
    String tag_json_obj = "tag_json";

    JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.POST,
            urlJsonReq,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d("MyApp", response.toString());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("MyApp", "Error: " + error.getMessage());
                    // hide the progress dialog
                }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("value1", "testValue1");
            params.put("value2", "testValue2");
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("X-Parse-REST-API-Key", "xxxxxxxxxxxx");
            headers.put("X-Parse-Application-Id", "xxxxxxxxxxx");
            return headers;
        }

        @Override
        public String getBodyContentType() {
            return "application/json";
        }
    };

ฉันยังคงได้รับข้อผิดพลาด ฉันอ่านเจอที่ไหนสักแห่ง แต่ไม่มีรายละเอียดใด ๆ ที่ วอลเลย์ ไม่สามารถส่ง JsonObjects ได้ แต่จะได้รับเท่านั้น หากคุณต้องการแก้ไขปัญหานั้นคุณควรใช้คลาสที่กำหนดเอง แต่ฉันไม่รู้จริงๆว่าฉันแค่ทำผิดพลาดโง่ ๆ ที่นี่หรือไม่ (เป็นไปได้)

พวกคุณรู้อะไรเกี่ยวกับเรื่องนั้นบ้างไหม?

ขอขอบคุณสำหรับเวลาของคุณ.


person Tin Megali    schedule 14.05.2015    source แหล่งที่มา


คำตอบ (1)


คุณสามารถส่ง JSONObject ได้โดยไม่ต้องแทนที่ getParams หรือ getBodyContentType บางอย่างเช่นนี้เช่น

JSONObject object = new JSONObject();
    JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, url, object, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

แน่นอนว่าคุณสามารถแทนที่ส่วนหัวได้หากต้องการ

person Andy Joyce    schedule 14.05.2015
comment
มหัศจรรย์! ดีใจที่สามารถช่วยได้ :) - person Andy Joyce; 15.05.2015