[英]Calling a web service from an Android client
我正在同時做我的第一個Android應用程序和第一個WCF服務。
我有以下服務合同:
[ServiceContract]
public interface ILoginService
{
[OperationContract]
string Login(string username, string password);
// TODO: Add your service operations here
}
我想從我的Android應用程序中調用它,並傳入用戶名和密碼參數。 1.該合同適合這份工作嗎? 2.有人可以指出我如何通過Android調用此WCF服務的教程/代碼示例嗎?
謝謝!
編輯:我可能會更近一些,但我仍然迷路。 到目前為止,這是我的合同:
[OperationContract]
[WebGet(ResponseFormat= WebMessageFormat.Json,
UriTemplate="/LoginService/?username={username}&password={password}")]
Response Login(string username, string password);
然后,我通過以下方式從Android調用該服務:
public LoginResponse RemoteLogin(String username, String password)
{
loginParameters = "/LoginService/username=" + username + "&password=" + password;
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(serviceAddress + loginParameters);
HttpResponse response;
LoginResponse loginResponse = null;
try
{
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if(entity != null)
{
InputStream instream = entity.getContent();
String result = convertStreamToString(instream);
JSONObject json = new JSONObject(result);
// Parsing
JSONArray nameArray = json.names();
JSONArray valArray = json.toJSONArray(nameArray);
loginResponse = new LoginResponse(valArray.getBoolean(1), valArray.getString(0), valArray.getString(2));
instream.close();
}
}
catch(Exception e)
{
loginResponse = new LoginResponse();
String sDummy = e.toString();
}
return loginResponse;
}
我不知道現在怎么了。
並沒有真正解決問題,只是回避了一下。 我從web.config中刪除了與ServiceModel有關的所有內容,並將ServiceHostFactory指令添加到了SVC文件中。 做到了。 現在一切正常。 一定是某種配置問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.