从Android客户端调用Web服务
问题描述:
我遇到了一些麻烦。我正在同时做我的第一个Android应用程序和第一个WCF服务。 请与我裸... 我有以下的服务合同:从Android客户端调用Web服务
[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有关的任何内容,并向SVC文件添加了ServiceHostFactory指令。那就是诀窍。现在一切正常。它一定是某种配置问题。
你能否提供一个示例代码? –
你能解释一下如何从android.please的wcf服务开始,给出一些启动步骤。我对此没有任何想法 –