如何从android应用程序传递参数到wcf服务并与sql server数据库连接?
问题描述:
我的android端代码
My android side code
String from=et1.getText().toString();
String to=et2.getText().toString();
String id=et3.getText().toString();
String inputxele="ReportData FromDate='"+from+"' ToDate='"+to+"' UserId='"+id+"'";
String url="http://10.0.2.2/JsonWcfService/GetEmployees.svc/?";
String resp = null;
InputStream instream =null;
try
{
List<namevaluepair> postParams = new ArrayList<namevaluepair>();
postParams.add(new BasicNameValuePair("spName", "USP_ListingReport"));
postParams.add(new BasicNameValuePair("inputxele", inputxele));
postParams.add(new BasicNameValuePair("transType", "GetMobileData"));
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 20000);
HttpConnectionParams.setSoTimeout(httpParameters, 20000);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Content-Type", "application/json");
httppost.setEntity(new UrlEncodedFormEntity(postParams,HTTP.UTF_8));
HttpResponse response;
response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK)
{
HttpEntity entity = response.getEntity();
instream = entity.getContent();
resp = convertStreamToString(instream);
instream.close();
}
}
catch (IOException e)
{
e.printStackTrace();
resp = null;
}
return resp;
}
我的WCF服务代码
MY WCF SERVICE CODE
[OperationContract]
[WebInvoke(UriTemplate = "?spName={spName}&inputxele={inputxele}&transType={transType}",
BodyStyle = WebMessageBodyStyle.Wrapped,
Method = "POST",
RequestFormat = System.ServiceModel.Web.WebMessageFormat.Json)]
string ValidateCredentials(string spName, string inputxele,string transType);
使用此代码我收到错误methodnotallowed异常。
任何帮助将不胜感激。
提前致谢
Using this code i am getting an error "methodnotallowed" exception.
Any help will be appreciated.
Thanks in advance
答
如果我理解正确,那么删除?
来自U. riTemplate。这样做,
If I understood it correctly then remove that?
from the UriTemplate. Make it this way,
[OperationContract]
[WebInvoke(UriTemplate = "spName={spName}&inputxele={inputxele}&transType={transType}",
BodyStyle = WebMessageBodyStyle.Wrapped,
Method = "POST",
RequestFormat = System.ServiceModel.Web.WebMessageFormat.Json)]
string ValidateCredentials(string spName, string inputxele,string transType);
你不需要明确定义,请参阅此示例(POST方法):
You don't need to explicitly define that, see this example (POST method):
[OperationContract]
[WebInvoke(UriTemplate = "AddPost",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string AddPost(string title, string description, string startTime, string endTime, string noticeTypeId, string groupId, string userId, string approvalStatus);
b $ b
-KR
-KR