Android Volley JsonRequest

时间:2013-10-08 15:28:27

标签: android json http-post android-volley androidhttpclient

我希望POST JsonObject使用Volley从设备到服务器,但我找不到任何代码示例。如果你能提供一些参考或一些代码示例。

2 个答案:

答案 0 :(得分:4)

我是这样做的

public void doRequest(RequestQueue volleyRequestQueue,
        onResponse responseListener) {

    this._responseListener = responseListener;

    StringRequest stringRequest = new StringRequest(Method.POST,
            Settings.QUESTIONURL, this, this) {

        public String getBodyContentType() {
            return "application/json; charset=" + getParamsEncoding();
        }

        public byte[] getBody() throws AuthFailureError {
            try {
                return new GsonBuilder()
                        .excludeFieldsWithoutExposeAnnotation().create()
                        .toJson(YOUROBJECT).toString()
                        .getBytes(getParamsEncoding());
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

    };

    stringRequest.setRetryPolicy(new DefaultRetryPolicy(10000, MAXRETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

    volleyRequestQueue.add(stringRequest);
}

答案 1 :(得分:1)

我找到了一个很好的参考,其中有一些很好的例子:

Link