android - 从 Android 将 JSONArray 发布到 WCF 服务

标签 android json wcf post http-post

我无法将值的 JSONArray 发布到我的 WCF 服务。当我从 Fiddler 或 .Net Test Client 发布数据时,它工作正常。每次我尝试从我的 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 服务。请帮助我。提前致谢

最佳答案

删除

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

来自你的代码

关于android - 从 Android 将 JSONArray 发布到 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15088463/

相关文章:

Android 相机未保存在特定文件夹中 [MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA]

java - nextFocusForward 到禁用的 EditText 创建问题

android - 从 ADB/Shell 命令模拟强制停止

json - 将所有json对象存储到MongoDB中

json - FOR JSON 在 SQL Server Management Studio 中引发错误

wcf - 如何从WCF服务返回Json?

c# - 如何以 JSON 格式传递 TimeSpan 值?

java - 如何在Eclipse中读取调试日志

java - 如何使用 Jackson 反序列化动态 JSON 字段?

wcf - 从命令提示符调用休息服务