通过 WCF 服务发布数据时遇到问题。我已经托管了 wcf 服务并在我的 .net 网站上运行良好,但它不适用于我的 android 客户端,我无法理解发生了什么,谁能帮我解决这个问题?这是我的 android 和 wcf 代码:
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;
}
}
}
这是我的安卓代码:
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();
}
}
});
}
我的日志是:
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
谢谢