2013-03-22 73 views
0

我在通過WCF服務發佈數據時遇到問題。我承載了wcf服務,並且與我的.net網站正常工作,但它不能與我的android客戶端一起工作,我無法理解發生了什麼,任何人都可以幫助我解決此問題嗎?這是我的Android和WCF代碼:從Android客戶端寫入WCF post json並將數據保存到sql server 2008

WCF服務:

[ServiceContract] 
    public interface IService1 
    { 
     [OperationContract] 
     string GetData(int value); 

     [OperationContract] 
     [WebInvoke(Method = "POST", 
     ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,UriTemplate = "insert")] 
     string InsertUserDetails(UserDetails userInfo); 
    }  

public class UserDetails 
    { 
     string username = string.Empty; 
     string fname = string.Empty; 
     string lname = string.Empty; 
     string loc = string.Empty; 

     [DataMember] 
     public string UserName 
     { 
      get { return username; } 
      set { username = value; } 
     } 
     [DataMember] 
     public string FirstName 
     { 
      get { return fname; } 
      set { fname = value; } 
     } 
     [DataMember] 
     public string LastName 
     { 
      get { return lname; } 
      set { lname = value; } 
     } 
     [DataMember] 
     public string Location 
     { 
      get { return loc; } 
      set { loc = value; } 
     } 

    } 

namespace CRUDWCFService 
{ 
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file. 
    public class Service1 : IService1 
    { 
     public string GetData(int value) 
     { 
      return string.Format("You entered: {0}", value); 
     } 

     public CompositeType GetDataUsingDataContract(CompositeType composite) 
     { 
      if (composite.BoolValue) 
      { 
       composite.StringValue += "Suffix"; 
      } 
      return composite; 
     } 


     public string InsertUserDetails(UserDetails userInfo) 
     { 
      string Message; 
      SqlConnection con = new SqlConnection("Data Source=192.168.1.99;Initial Catalog=database;User ID=sa;Password=zaeveypws21"); 
      con.Open(); 
      SqlCommand cmd = new SqlCommand("insert into UserInformation(UserName,FirstName,LastName,Location) values(@UName,@FName,@LName,@Location)", con); 
      cmd.Parameters.AddWithValue("@UName", userInfo.UserName); 
      cmd.Parameters.AddWithValue("@FName", userInfo.FirstName); 
      cmd.Parameters.AddWithValue("@LName", userInfo.LastName); 
      cmd.Parameters.AddWithValue("@Location", userInfo.Location); 

      int result = cmd.ExecuteNonQuery(); 
      if (result == 1) 
      { 
       Message = userInfo.UserName + " Details inserted successfully"; 
      } 
      else 
      { 
       Message = userInfo.UserName + " Details not inserted successfully"; 
      } 
      con.Close(); 
      return Message; 
     } 
    } 
    } 

這裏是我的Android代碼:

public void addListenerOnButton() { 

     Button btnCreateProduct = (Button) findViewById(R.id.btnInsert); 

     btnCreateProduct.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       try { 

        Editable UserName = inputUName.getText(); 
        Editable FirstName = inputFName.getText(); 
        Editable LastName = inputLName.getText(); 
        Editable Location = inputLoc.getText(); 

        boolean isValid = true; 



        if (isValid) { 

         // POST request to <service>/insert 
         HttpPost request = new HttpPost("http://192.168.1.99/wcfinsert/Service1.svc/insert"); 
         request.setHeader("Accept", "application/json"); 
         request.setHeader("Content-type", "application/json"); 

         // Build JSON string 
         JSONStringer userInfo = new JSONStringer().object() 
           .key("userInfo").object().key("UserName") 
           .value(UserName).key("FirstName") 
           .value(FirstName).key("LastName") 
           .value(LastName).key("Location") 
           .value(Location).endObject().endObject(); 
         StringEntity entity = new StringEntity(userInfo.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) { 
        e.printStackTrace(); 
       } 



      } 
     }); 

    } 

我logcat的是:

03-22 12:23:55.397: D/dalvikvm(115): GC_EXTERNAL_ALLOC freed 424 objects/21160 bytes in 233ms 
03-22 12:24:01.748: D/WebInvoke(305): Saving : 415 
03-22 12:26:43.081: D/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol 

感謝

回答

相關問題