Android无法将数据发布到WCF服务

时间:2013-06-01 13:05:13

标签: c# java android wcf rest

我正在尝试使用WCF服务将数据发布到我的数据库,Web服务似乎不起作用,任何人都可以为我修复它吗?
这是我的代码:

WCF服务.cs:

[ServiceContract]
public interface IAllocationService
{
    [OperationContract]
    [WebInvoke(
        Method = "POST",
        UriTemplate = "StrongestWifi",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
    void UpdateStrongestWifi(myWifiClass myWifiClass);

}

[DataContract]
public class myWifiClass
{
    [DataMember(Name = "Time")]
    public string Time
    {
        get;
        set;
    }
    [DataMember(Name = "SignalStrength")]
    public string SignalStrength
    {
        get;
        set;
    }
    [DataMember(Name = "SSID")]
    public string SSID
    {
        get;
        set;
    }

** WCF服务.svc **

    public void UpdateStrongestWifi(myWifiClass myWifiClass)
    {
        string strConnectionString = ConfigurationManager.ConnectionStrings["PCSDB"].ConnectionString;
        SqlConnection conn = new SqlConnection(strConnectionString);
        conn.Open();
        using (SqlCommand cmd = new SqlCommand("Insert into TestingTable (Time,SignalStrength,SSID) Values (@Time,@SignalStrength,@SSID)", conn))
        {
            cmd.Parameters.AddWithValue("@Time", myWifiClass.Time);
            cmd.Parameters.AddWithValue("@SignalStrength", int.Parse(myWifiClass.SignalStrength));
            cmd.Parameters.AddWithValue("@SSID", myWifiClass.SSID);

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

Android代码:

private class sendPostData extends AsyncTask<String, Void, String>
{
    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        HttpPost request = new HttpPost(SERVICE_URI + "/StrongestWifi");
        request.setHeader("Accept", "application/json");            
        request.setHeader("Content-type", "application/json");
        JSONStringer getWifiInfo;
        try {
            getWifiInfo = new JSONStringer()
                .object()
                    .key("myWifiClass")
                        .object()
                            .key("Time").value("TIME")                                  
                            .key("SignalStrength").value("Strength")
                            .key("SSID").value("ID")
                        .endObject()
                    .endObject();

        StringEntity entity = new StringEntity(getWifiInfo.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) {
          textDisplay.setText("Success");
    }
}

Log Cat:

>06-01 21:02:20.150: D/WebInvoke(2832): Saving : 400

1 个答案:

答案 0 :(得分:1)

没关系,发现解决方案,是以String格式而不是int

发送SignalStrength