2013-06-01 107 views
-1

我想使用WCF服務將數據發佈到我的數據庫,Web服務似乎沒有工作,任何人都可以爲我解決它嗎?
這裏是我的代碼:Android無法將數據發佈到WCF服務

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"); 
    } 
} 

登錄貓:

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

你得到任何錯誤?你所顯示的是由你製作的日誌貓條目不是錯誤 – NetStarter

+1

我沒有得到任何錯誤,這是我點擊按鈕時出現的唯一的東西 –

+0

確定你發送的值是什麼,如果你沒有得到任何錯誤,這意味着你的代碼正在工作 – NetStarter

回答

1

沒關係,找到了解決辦法,以String格式將派遣SignalStrength代替INT