从安卓发布一个JSONArray到WCF服务 [英] Posting a JSONArray to WCF Service from android

查看:137
本文介绍了从安卓发布一个JSONArray到WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦张贴值的JSONArray我的WCF服务。当我从后小提琴手或.Net测试客户端的数据,它工作正常。每次我尝试后从我的Andr​​oid应用程序,我得到请求错误

I am having trouble posting a JSONArray of values to my WCF Service. When I post the data from Fiddler or .Net Test Client it works fine. Every time I try to post from my android application I get Request Error

这是我发到我的WCF服务从Android应用程序的JSON数据。我试着从提琴手这个确切的数据和它的作品

This is the JSON data that I send to my WCF Service from the android application. I've tried this exact data from Fiddler and it works

[{"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"}]

我的Andr​​oid code

My android code

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服务

My WCF Service

[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服务。请请帮助提前me.Thanks

I have called multiple GET requests from android application to same WCF Service and it works great, but now I need to send an array of data to the wcf service. Please please help me.Thanks in advance

推荐答案

删除

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

这是乌拉圭回合的code

from ur code

这篇关于从安卓发布一个JSONArray到WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆