我的Android应用程序正在调用WCF Web服务但我无法发送获取请求:
这是我的WCF合约
[OperationContract]
[WebGet(UriTemplate = "ValidateLogin/{userId}/{password}",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
LoginValidation ValidateLogin(string userId, string password);
我在我的Android应用程序上调用它
public String ValidateLogin(ArrayList<NameValuePair> httgetArgs){
String result = "";
String url = "http://10.0.2.2/WSCom.svc/ValidateLogin";
InputStream is = null;
try {
HttpClient httpclient = new DefaultHttpClient();
setparameters(url , new UrlEncodedFormEntity(httgetArgs));
HttpGet httget = new HttpGet(url);
httget.setHeader("Accept", "application/json");
httget.setHeader("Content-Type", "application/json");
HttpResponse response = httpclient.execute(httget);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch (Exception e) {
result = e.toString();
}
try {
result = readStream(is);
is.close();
}
catch (Exception e) {
result = e.toString();
}
return (result);
}
但是我得到了错误
合同'IWSCom'中的'ValidateLogin'操作使用GET,但也有body参数'userId'。 GET操作不能有一个正文。将参数'userId'设为UriTemplate参数,或从WebGetAttribute切换到WebInvokeAttribute
答案 0 :(得分:1)
您需要具有以下网址
String url = "http://10.0.2.2/WSCom.svc/ValidateLogin/YourUserId/Password";
当你在url中传递数据时,你不需要使用
setparameters(url , new UrlEncodedFormEntity(httgetArgs));
答案 1 :(得分:1)
String user = "";
String pass = "";
String url = "http://10.0.2.2/WSCom.svc/ValidateLogin/" + user + "/" + pass;