我正在尝试使用 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();
安卓代码:
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");
}
}
原木猫:
>06-01 21:02:20.150: D/WebInvoke(2832): Saving : 400