使用Android Volley Library将JSON发布到WCF Restful Service

时间:2015-07-02 14:46:52

标签: c# android json wcf android-volley

如果同样的问题已经发布在某些地方,我表示道歉,但在搜索了将近5天之后,人们可以理解我是多么绝望,因为大多数答案似乎都没有给我一个解决方案。漫无边际。所以,我试图使用齐射将json发布到wcf restful web服务。以下是我的代码片段

// Android / Volley

public void setJsonDataToWebservices(String url){
        JSONObject params = new JSONObject();
        try {
            params.put("FullName","nathalue Gustava");
            params.put("Id","123");
            params.put("Password","123456789");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        // Create a future request object

        // Create a json request object
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, params,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                     Toast.makeText(getActivity(), "Success", Toast.LENGTH_SHORT).show();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getActivity(), "Failed", Toast.LENGTH_SHORT).show();
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Content-Type", "application/json; charset=utf-8");
                return headers;
            }
        };
        VolleySingleton.getIntance().addToRequestQueue(jsonObjectRequest);

    }

这是我的服务界面

[OperationContract]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "simplePostExample")]
    string simplePostExample(Stream jsonStream);

......这是一个反序列化流并将其返回一个字符串的方法

public string simplePostExample(Stream jsonStream)
        {
            StreamReader reader = null;
            string text = string.Empty;
            try
            {
                reader = new StreamReader(jsonStream);
                text = reader.ReadToEnd();
            }
            catch (Exception)
            {

                throw;
            }

            return text != null ? text : "String is empty";
        }

以下是错误日志 处理异常。异常详细信息:System.InvalidOperationException:操作的传入消息&#39; simplePostExample&#39; (合同&#39; IDigitizingDataRestfulWebService&#39; with namespace&#39; http://tempuri.org/&#39;)包含无法识别的http正文格式值&#39; Json&#39;。预期的正文格式值是&#39; Raw&#39;。这可能是因为尚未在绑定上配置WebContentTypeMapper。有关更多详细信息,请参阅WebContentTypeMapper的文档。    在System.ServiceModel.Dispatcher.HttpStreamFormatter.GetStreamFromMessage(消息消息,布尔isRequest)    at System.ServiceModel.Dispatcher.HttpStreamFormatter.DeserializeRequest(Message message,Object [] parameters)    在System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(消息消息,Object []参数)    在System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(消息消息,Object []参数)    在System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&amp; rpc)    在System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)    在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)    在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&amp; rpc)    在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)    在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)    在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp; rpc)    在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc)    在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&amp; rpc)    在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; rpc)

现在,亲爱的人们,请告诉我哪里出错了。

0 个答案:

没有答案