Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Calling a web service from an Android client
- [ServiceContract]
- public interface ILoginService
- {
- [OperationContract]
- string Login(string username, string password);
- // TODO: Add your service operations here
- }
- [OperationContract]
- [WebGet(ResponseFormat= WebMessageFormat.Json,
- UriTemplate="/LoginService/?username={username}&password={password}")]
- Response Login(string username, string password);
- 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;
- }
Add Comment
Please, Sign In to add comment