【发布时间】:2011-12-06 00:46:55
【问题描述】:
我正在同时开发我的第一个 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;
}
我现在不知道出了什么问题。
【问题讨论】: