c# – 反序列化从Android应用程序发送到WCF webservice的JSON对象

c# – 反序列化从Android应用程序发送到WCF webservice的JSON对象,第1张

概述我正在尝试将JSON对象发送到我的webservice方法,该方法定义如下:publicStringSendTransaction(stringtrans){varjson_serializer=newJavaScriptSerializer();TransactiontransObj=json_serializer.Deserialize<Transaction>(trans);

我正在尝试将JSON对象发送到我的webservice方法,该方法定义如下:

public String SendTransaction(string trans){            var Json_serializer = new JavaScriptSerializer();            Transaction transObj = Json_serializer.Deserialize<Transaction>(trans);            return transObj.filename;       }

我想在哪里返回我作为参数获得的这个JsON字符串的filename.

androID应用程序的代码:

httpPost request = new httpPost(                "http://10.118.18.88:8080/Service.svc/SendTransaction");        request.setheader("Accept", "application/Json");        request.setheader("Content-type", "application/Json");        // Build JsON string        JsONStringer JsonString;        JsonString = new JsONStringer()                .object().key("imei").value("2323232323").key("filename")                .value("Finger.NST").endobject();        Log.i("JsON STRING: ", JsonString.toString());        StringEntity entity;        entity = new StringEntity(JsonString.toString(), "UTF-8");        entity.setContentEnCoding(new Basicheader(http.CONTENT_TYPE,                "application/Json"));        entity.setContentType("application/Json");        request.setEntity(entity);        // Send request to WCF service        DefaulthttpClIEnt httpClIEnt = new DefaulthttpClIEnt();        httpResponse response = httpClIEnt.execute(request);        httpentity httpentity = response.getEntity();        String xml = EntityUtils.toString(httpentity);        Log.i("Response: ", xml);        Log.d("WebInvoke", "Status : " + response.getStatusline());

我只得到一个长的HTML文件,它告诉我服务器遇到处理请求的错误.状态代码是http / 1.1 400 Bad Request

我的Transaction类在C#中定义如下:

 [DataContract]public class Transaction{    [DataMember(name ="imei")]    public string Imei { get; set; }    [DataMember (name="filename")]    public string filename { get; set; }}

我怎样才能以正确的方式实现这一目标?

编辑,这是我的web.config

 <?xml version="1.0"?><configuration>  <appSettings>    <add key="aspnet:UseTaskFrIEndlySynchronizationContext" value="true" />  </appSettings>  <system.web>    <compilation deBUG="true" targetFramework="4.5" />    <httpRuntime targetFramework="4.5"/>  </system.web>  <system.serviceModel>    <behaviors>          <endpointBehaviors>            <behavior name="httpBehavior">                <webhttp />            </behavior >        </endpointBehaviors>      <serviceBehaviors>        <behavior name="">          <!-- To avoID disclosing Metadata information, set the values below to false before deployment -->          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>          <!-- To receive exception details in faults for deBUGging purposes, set the value below to true.  Set to false before deployment to avoID disclosing exception information -->          <serviceDeBUG includeExceptionDetailinFaults="false"/>        </behavior>      </serviceBehaviors>    </behaviors>    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>    <services>      <service name="Service.Service">        <endpoint address="" behaviorConfiguration="httpBehavior" binding="webhttpBinding" contract="Service.IService"/>      </service>    </services>    <protocolMapPing>        <add binding="webhttpBinding" scheme="http" />    </protocolMapPing>    <!--<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />-->  </system.serviceModel>  <system.webServer>  <!-- <modules runAllManagedModulesForAllRequests="true"/>-->    <!--        To browse web app root directory during deBUGging, set the value below to true.        Set to false before deployment to avoID disclosing web app folder information.      -->    <directorybrowse enabled="true"/>  </system.webServer></configuration>

解决方法:

@Tobias,这不是答案.但由于评论有点长,我在这里发布.也许它可以帮助诊断您的问题. [完整的工作代码].

public voID TestWCFService(){    //Start Server    Task.Factory.StartNew(        (_) =>{            Uri baseAddress = new Uri("http://localhost:8080/Test");            WebServiceHost host = new WebServiceHost(typeof(TestService), baseAddress);            host.open();        },null,TaskCreationoptions.LongRunning).Wait();    //ClIEnt    var JsonString = new JavaScriptSerializer().Serialize(new { xaction = new { Imei = "121212", filename = "Finger.NST" } });    WebClIEnt wc = new WebClIEnt();    wc.headers.Add("Content-Type", "application/Json");    var result = wc.UploadString("http://localhost:8080/Test/Hello", JsonString);}[ServiceContract]public class TestService{    [OperationContract]    [WebInvoke(RequestFormat = Webmessageformat.Json, ResponseFormat = Webmessageformat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]    public User Hello(Transaction xaction)    {        return new User() { ID = 1, name = "Joe", Xaction = xaction };    }    public class User    {        public int ID { get; set; }        public string name { get; set; }        public Transaction Xaction { get; set; }    }    public class Transaction    {        public string Imei { get; set; }        public string filename { get; set; }    }}
总结

以上是内存溢出为你收集整理的c# – 反序列化从Android应用程序发送到WCF webservice的JSON对象全部内容,希望文章能够帮你解决c# – 反序列化从Android应用程序发送到WCF webservice的JSON对象所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1102699.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存