如何使用Volley将JSON对象发送到服务器

时间:2014-09-12 10:20:08

标签: android json send android-volley

我从Volley Library的服务器获取JSON数据,但现在,我需要向服务器发送JSON。但我不知道如何发回JSONArray或JSONObject。

我使用下一个示例检索数据。

JsonArrayRequest program_from_event = new JsonArrayRequest(URL, new Response.Listener<JSONArray>() {

    @Override
    public void onResponse(JSONArray response) {

        if(response.length() > 0){
            for(int i = 0; i < response.length(); i++){
                try {
                    JSONObject input = response.getJSONObject(i);
                    String name = input.getString("name");
                    String work_area = input.getString("work_area");
                    String email = input.getString("email");
                    String phone = input.getString("phone");
                    String avatar = input.getString("avatar");
                    String description = input.getString("description");
                    int id = input.getInt("id");

                    ...

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}, new Response.ErrorListener(){

    @Override
    public void onErrorResponse(VolleyError error) {
        NetworkResponse nr = error.networkResponse;
        if(nr != null && nr.statusCode == HttpStatus.SC_UNAUTHORIZED){
            Log.i("ERROR", "ERROR");
        }
    }
});

volley_queue.add(program_from_event);

}

我如何做send方法?谢谢。

1 个答案:

答案 0 :(得分:2)

protected void callWebService(final User user) {
        pd = ProgressDialog.show(activity, "Please Wait...", "Please Wait...");
        RequestQueue queue = Volley.newRequestQueue(activity);
        JsonObjectRequest jsObjRequest = new JsonObjectRequest(
                Request.Method.POST,url,
                createUserMapperObejct(user),
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        pd.dismiss();
                        Log.v("reponse", "" + response);


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                });
        queue.add(jsObjRequest);
    }

public static JSONObject createUserMapperObejct(User user)
    {
        JSONObject request=new JSONObject();
        try {
            request.put(StringConstant.USERNAME, user.getUserName());
            request.put(StringConstant.PASSWORD, user.getPassword());
            request.put(StringConstant.CONFIRMPASSWORD, user.getPassword());
            request.put(StringConstant.EMAILID, user.getEmailId());
            request.put(StringConstant.CONTACTNO, user.getMobileNo());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return request; 
    }