我正在研究Android应用程序,在调用我的" POST"时遇到问题从Android应用程序请求wcf RESTful服务。
这是我的wcf服务:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "finalProductList", RequestFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Bare)]
string finalProductList(Productdetails[] objprod);
public string finalProductList(Productdetails[] objprod)
{
try
{
BasicConfigurator.Configure();
log.Info("finalProductList method fired ");
StringBuilder sb = new StringBuilder();
sb.Append("\"jsonResult\":[");
int result = 0;
string json = objprod.ToString();
JObject rss = JObject.Parse(json);
JArray items = (JArray)rss["finalProductList"];
for (int i = 0; i < items.Count; i++)
{
string Merchantid = string.Empty;
string MerchantName = string.Empty;
alperm.Clear();
alperm.Add(new SqlParameter("@Merchantid", (string)rss["finalProductList"][i]["merchant_id"]));
alperm.Add(new SqlParameter("@MerchantName", (string)rss["finalProductList"][i]["merchant_name"]));
result = SqlHelper.ExecuteNonQuery(sqlConnection, CommandType.StoredProcedure, "Sp_InsProductDetails", (SqlParameter[])alperm.ToArray(typeof(SqlParameter)));
}
if (result == 1)
{
sb.Append("{\"success\": true" + ",\"message\":Data Inserted Successfully!}]");
return sb.ToString();
}
else
{
sb.Append("{\"success\": false" + ",\"message\":No input data retrived!}]");
return sb.ToString();
}
}
catch (Exception ex)
{
log.Error("finalProductList : Error while inserting producet details: " + ex);
throw;
}
}
Android代码:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("data", jsonStr));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response)
我在wcf方法中启用徽标。当我从Android应用程序调用wcf方法时,这个方法没有被解雇并在android中出错:
W/System.err(1603): org.apache.http.client.HttpResponseException: Bad Request