无法从Android的POST数据到WCF [英] UNABLE to POST data to WCF from Android

查看:278
本文介绍了无法从Android的POST数据到WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数据从Android应用程序发送到WCF服务,但不知该服务似乎并不通过Android来调用。我收到了状态code 值= 500通过Adnroid内的 logcat的(这意味着内部服务器错误)我去通过源$ C ​​$ C 100次,但没'吨找出错误。几乎检查了所有有关我的问题的职位,但仍然没有得到任何解决方案。

I'm trying to send data to WCF service from an Android Application but somehow the service doesn't seems to be call through the android. I received a STATUSCODE value = 500 through adnroid in LOGCAT (which means the Internal Server Error) I go through the source code 100 times but didn't figure out the Bug. and almost checked all the posts related to my problem but still didn't get any solution.

这里是THS code

Android的code:

Android Code:

private class sendPostData extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params) {
    // TODO Auto-generated method stub

    HttpPost request = new HttpPost( LOGIN_SERVICE_URL + "/MyCar");
    request.setHeader("Accept", "application/json");            
    request.setHeader("Content-type", "application/json");
    JSONStringer getCarInfo;
    try {
        getCarInfo = new JSONStringer()
            .object()
                .key("myCar")
                    .object()
                        .key("Name").value(edt_carName.getText().toString())                                  
                        .key("Make").value(edt_carMake.getText().toString())
                        .key("Model").value(edt_carModel.getText().toString())
                    .endObject()
                .endObject();

    StringEntity entity = new StringEntity(getCarInfo.toString());

    request.setEntity(entity);

    // Send request to WCF service
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpResponse response = httpClient.execute(request);
    Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());
    }
    catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPostExecute(String result) {
    txt_verif.setText("Success");
}
}

一切工作在android系统code细除了调用WCF服务。 我调试的code几次,接收到的状态code = 500

everything is working fine in android code except calling the WCF service. I Debug the code several times and received statuscode = 500

这里是WCF服务

Service.cs

public class Service1 : IService1
{
    public void UpdateMyCar(myCar myCar) {

        string strConnectionString = ConfigurationManager.ConnectionStrings["Database1"].ConnectionString;
        SqlConnection conn = new SqlConnection(strConnectionString);
        conn.Open();
        using (SqlCommand cmd = new SqlCommand("Insert into TestingTable (Name,Make,Model) Values (@Name,@Make,@Model)", conn)) {

            cmd.Parameters.AddWithValue("@Name", myCar.Name);
            cmd.Parameters.AddWithValue("@Make", myCar.Make);
            cmd.Parameters.AddWithValue("@Model", myCar.Model);

            int queryResult = cmd.ExecuteNonQuery();
        } conn.Close();
    }

LogCat中

WebInvoke     Saving : 500

IService1.svc

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(
        Method = "POST",
        UriTemplate = "MyCar",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
    void UpdateMyCar(myCar myCar);
}

[DataContract]
public class myCar 
{

    [DataMember(Name="Name")]
    public string Name 
    { 
        get; 
       set; 
    }

    [DataMember(Name="Model")]
    public string Model 
    { 
        get; 
        set; 
    }

    [DataMember(Name="Make")]
    public string Make 
    { 
        get;
        set; 
    }

的Web.Config

<?xml version="1.0"?>

<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
    <compilation debug="true" targetFramework="4.0">
    </compilation>

    <authentication mode="Windows"/>
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>
<system.serviceModel>
    <services>
        <service name="CarSercive.Service1" behaviorConfiguration="CarSercive.Service1Behavior">
            <!-- Service Endpoints -->
            <endpoint address="" binding="webHttpBinding" contract="CarSercive.IService1">
                <identity>
                    <dns value="localhost"/>
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="CarSercive.Service1Behavior">
                <serviceMetadata httpGetEnabled="true"/>

                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

这服务也公布关于IIS。我还检查了与谷歌的Chrome扩展程序的简单的REST客户端该服务并收到内部服务器错误

this service is also publish on IIS. and I also checked that service with google chrome extension SIMPLE REST CLIENT and received an internal server error

推荐答案

没关系,我已经在的web.config 文件的一些变化,并得到了解决。 &LT; endpointBehaviors&GT; 标记缺少我的情况和其他一些事情。这里的的web.config 文件。

Never mind, I have made several changes in web.config file and got the solution. <endpointBehaviors> tag was missing in my case and several other things. here is the updated code of web.config file.

[已更新web.config文件]

<?xml version="1.0"?>
<configuration>
<appSettings/>
  <connectionStrings>
<add name="DB" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Munyb\Documents\Visual Studio 2010\Projects\CarSercive\CarSercive\App_Data\Database1.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
<system.web>
    <compilation debug="true" targetFramework="4.0">
    </compilation>
    <authentication mode="Windows"/>
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>
<system.serviceModel>
    <services>
        <service name="CarSercive.Service1" behaviorConfiguration="RESTfulServ">
            <!-- Service Endpoints -->
    <endpoint address="" binding="webHttpBinding" contract="CarSercive.IService1" behaviorConfiguration="web"></endpoint>
        </service>
    </services>
    <behaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
        <serviceBehaviors>
            <behavior name="RESTfulServ">
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        </serviceBehaviors>

    </behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true">
</serviceHostingEnvironment>
</system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"> 
    </modules>
  </system.webServer>
</configuration>

这篇关于无法从Android的POST数据到WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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