我正在尝试从Android应用程序向我的WCF服务发布数据,该服务似乎不起作用,我认为调用WCF服务存在一些问题。有人可以帮助或修复该错误!还有一件事我还没有在IIS上发布这个服务。
以下是代码
Iservice.cs
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(
Method = "POST",
UriTemplate = "Cars",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
void UpdateMyCar(myCar myCar);
}
[DataContract]
public class myCar
{
[DataMember(Name="Name")]
public string Name
{
get;
set;
}
[DataMember(Name="Model")]
public string Model
{
get;
set;
}
[DataMember(Name="Make")]
public string Make
{
get;
set;
}
}
Service1.svc
public void UpdateMyCar(myCar myCar) {
string strConnectionString = ConfigurationManager.ConnectionStrings["Database1"].ConnectionString;
SqlConnection conn = new SqlConnection(strConnectionString);
conn.Open();
using (SqlCommand cmd = new SqlCommand("Insert into TestingTable (Name,Make,Model) Values (@Name,@Make,@Model)", conn)) {
cmd.Parameters.AddWithValue("@Name", myCar.Name);
cmd.Parameters.AddWithValue("@Make", myCar.Make);
cmd.Parameters.AddWithValue("@Model", myCar.Model);
int queryResult = cmd.ExecuteNonQuery();
} conn.Close();
}
Android代码
public final static String LOGIN_SERVICE_URL = "http://10.0.2.2:8259/Service1.svc";
public EditText edt_carName, edt_carMake , edt_carModel, edt_display;
public Button btn_send;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
xmlViews();
RegisterListenerOnViews();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId())
{
case R.id.button1:
new sendPostData().execute();
break;
default:
break;
}
}
private void xmlViews(){
edt_carName = (EditText) findViewById(R.id.editText_car_name);
edt_carMake = (EditText) findViewById(R.id.editText_car_make);
edt_carModel = (EditText) findViewById(R.id.editText_car_model);
// edt_display = (EditText) findViewById(R.id.editText1);
btn_send = (Button) findViewById(R.id.button1);
}
private void RegisterListenerOnViews(){
btn_send.setOnClickListener(this);
}
private class sendPostData extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
HttpPost request = new HttpPost( LOGIN_SERVICE_URL + "/Cars");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
JSONStringer getCarInfo;
try {
getCarInfo = new JSONStringer()
.object()
.key("myCar")
.object()
.key("Name").value(edt_carName.getText().toString())
.key("Make").value(edt_carMake.getText().toString())
.key("Model").value(edt_carModel.getText().toString())
.endObject()
.endObject();
StringEntity entity = new StringEntity(getCarInfo.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) {
edt_display.setText("Success");
}
}
}