将发布请求发送到RESTful WCF服务

时间:2014-04-08 19:36:31

标签: c# java android wcf android-async-http

我正和我的朋友一起研究我的android项目,但我们还是坚持了一下。我正在尝试向WCF休息Web服务发送异步帖子,而在服务端,我将数据插入到db中。我通过JSON对象发送数据。我打开一个对话框,当请求成功完成后,对话框消失。现在我的问题是,我无法发帖请求,实际上,我没有得到任何错误,但似乎有问题。获取请求没有问题。我疯了,所以我需要你的帮助。这是我的代码

谢谢

JAVA

        progressDialog = ProgressDialog.show(Activity3.this, "Please wait ...",  "Task in progress ...", true);
        progressDialog.setCancelable(true);
        jarray = new JSONArray();
        json2 = new JSONObject();

        AsyncHttpClient client = new AsyncHttpClient();
        try {

            json2.put("CreateDate", "30.03.2014 15:30:00");
            json2.put("EventCategory", "Yemek");
            json2.put("EventID", "6");
            json2.put("EventName", "Kanatçı Haydar");
            json2.put("EventStatus", "A");
            json2.put("FsqID", "561239");
            json2.put("IsPublic", "False");
            json2.put("LastUpdate", "01.01.0001 00:00:00");
            json2.put("Quota", "8");
            json2.put("UserID", "42");

            StringEntity entity = new StringEntity(json2.toString()); 

            client.post(arg0.getContext(), PURL, entity, "application/json",
                    new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(String response) {
                    progressDialog.dismiss();
                    Toast.makeText(Activity3.this,response, Toast.LENGTH_LONG).show();
                }

            });

        }

WCF服务

这就是我处理POST请求的方式

[OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "New")]
    bool SetAllEvents(Stream st);

此函数调用JSON解析器

public bool SetAllEvents(Stream s)
    {
        SetEvents se = new SetEvents();

        var data = se.SetNewEvent(s, connStr);
        return true;
    }

这就是我解析JSON的方法

StreamReader reader = new StreamReader(inputStream);
            string json = reader.ReadToEnd();

            var Jsonobject = JsonConvert.DeserializeObject<Events>(json);

            string eventName = Jsonobject.EventName;

1 个答案:

答案 0 :(得分:1)

客户端的httppost标头是什么样的?以下内容可以为您提供帮助:

httpost.setEntity(se);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");