5

我无法将 JSONArray 值发布到我的 WCF 服务。当我从 Fiddler 或 .Net 测试客户端发布数据时,它工作正常。每次我尝试从我的 android 应用程序发帖时,我都会收到请求错误

这是我从 android 应用程序发送到我的 WCF 服务的 JSON 数据。我已经从 Fiddler 尝试过这个确切的数据并且它有效

[{"date":"2013-02-22 15:30:374:021","id":"1","description":"test","name":"test"},
"date":"2013-02-25 11:56:926:020","id":"2","description":"ghy","name":"fhh"},
"date":"2013-02-25 11:56:248:026","id":"3","description":"ghfm","name":"run"}]

我的安卓代码

public JSONObject makeHttpPost(String url, String method, JSONArray params) {
    try {

        if (method == "POST") {

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
             httpPost.setHeader("Accept", "application/json");
             httpPost.setHeader("Content-Type",
             "application/json; charset=utf-8");
             StringEntity se = new StringEntity(params.toString(),"UTF-8");

             se.setContentType("application/json;charset=UTF-8");
             httpPost.setEntity(se);

            Log.e("Gerhard", params.toString());
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();



        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

我的 WCF 服务

[OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "updateOrderAddress")]
    String UpdateOrderAddress(Stream JSONdataStream);

public String UpdateOrderAddress(Stream JSONdataStream)
    {
        try
        {
            // Read in our Stream into a string...
            StreamReader reader = new StreamReader(JSONdataStream);
            string JSONdata = reader.ReadToEnd();

            // ..then convert the string into a single "wsOrder" record.

            if (JSONdata == null)
            {
                // Error: Couldn't deserialize our JSON string into a "wsOrder" object.
                return "null";
            }



            return JSONdata;     // Success !
        }
        catch (Exception e)
        {
            return e.ToString();
        }
    }

我得到的错误

02-26 14:00:56.185: E/Gerhard(31064):       <p>The server encountered an error processing the request. The exception message is 'Incoming message for operation 'UpdateOrderAddress' (contract 'IService1' with namespace 'http://tempuri.org/') contains an unrecognized http body format value 'Json'. The expected body format value is 'Raw'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'. See server logs for more details. The exception stack trace is: </p>

我已经从 android 应用程序向同一个 WCF 服务调用了多个 GET 请求,并且效果很好,但是现在我需要将一组数据发送到 wcf 服务。请帮助我。在此先感谢

4

1 回答 1

6

消除

httpPost.setHeader("Accept", "application/json");                 
httpPost.setHeader("Content-Type", "application/json; charset=utf-8"); 

从你的代码

于 2013-03-07T12:34:00.817 回答