使用json对象向服务器发送数据时出错

时间:2015-09-15 13:25:12

标签: android json

我想以json格式向特定网址上的服务器发送一些数据,但是无法正常工作。我的json格式是

  JSON Object﹕ {"appt_email":"grddgf@ffgh.com","appt_service":"Test services 11","appt_contact":"8554688564","appt_time":"18.44","appt_name":"fddvnhff","appt_date":"18\/9\/2015"}

我正在使用Chrome Poster扩展程序进行测试。当我使用它时,数据被正确发送。当我从申请表发送时,它无法正常工作。

  HttpClient httpClient = new DefaultHttpClient();

            HttpPost httpRequest = new HttpPost(

                    "http://tipseducation.com/system/eadmin/insertschedule/");

            httpRequest.setHeader("Content-Type", "application/json");

            JSONObject json = new JSONObject();
            //StringEntity se = new StringEntity(json.toString()); httpRequest.setEntity(se); httpRequest.setHeader("Accept", "application/json");

            StringEntity se = new StringEntity(json.toString());

            se.setContentEncoding("UTF-8");
            se.setContentType("application/json");
            json.put("appt_name", ed_name);
            json.put("appt_email", ed_email);
            json.put("appt_contact", ed_contact);
            json.put("appt_date", ed_date);
            json.put("appt_time", ed_time);
            json.put("appt_service", ed_spinner);
}

1 个答案:

答案 0 :(得分:0)

我会使用字符串请求。

StringRequest sr = new StringRequest(type, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
          //Valid Response
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //error message
        }
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {

            Map<String, String> params = new HashMap<>();
             params.put("appt_name", ed_name);
             params.put("appt_email", ed_email);
             params.put("appt_contact", ed_contact);
             params.put("appt_date", ed_date);
             params.put("appt_time", ed_time);
             params.put("appt_service", ed_spinner);
          return params;
        }
    };