Guest User

Untitled

a guest
Aug 20th, 2018
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. Calling a web service from an Android client
  2. [ServiceContract]
  3. public interface ILoginService
  4. {
  5.  
  6. [OperationContract]
  7. string Login(string username, string password);
  8.  
  9. // TODO: Add your service operations here
  10. }
  11.  
  12. [OperationContract]
  13. [WebGet(ResponseFormat= WebMessageFormat.Json,
  14. UriTemplate="/LoginService/?username={username}&password={password}")]
  15. Response Login(string username, string password);
  16.  
  17. public LoginResponse RemoteLogin(String username, String password)
  18. {
  19. loginParameters = "/LoginService/username=" + username + "&password=" + password;
  20.  
  21.  
  22. HttpClient httpclient = new DefaultHttpClient();
  23. HttpGet httpget = new HttpGet(serviceAddress + loginParameters);
  24.  
  25. HttpResponse response;
  26.  
  27. LoginResponse loginResponse = null;
  28.  
  29. try
  30. {
  31. response = httpclient.execute(httpget);
  32.  
  33. HttpEntity entity = response.getEntity();
  34.  
  35. if(entity != null)
  36. {
  37. InputStream instream = entity.getContent();
  38. String result = convertStreamToString(instream);
  39. JSONObject json = new JSONObject(result);
  40.  
  41. // Parsing
  42. JSONArray nameArray = json.names();
  43. JSONArray valArray = json.toJSONArray(nameArray);
  44.  
  45. loginResponse = new LoginResponse(valArray.getBoolean(1), valArray.getString(0), valArray.getString(2));
  46.  
  47.  
  48. instream.close();
  49. }
  50.  
  51. }
  52. catch(Exception e)
  53. {
  54. loginResponse = new LoginResponse();
  55. String sDummy = e.toString();
  56. }
  57. return loginResponse;
  58. }
Add Comment
Please, Sign In to add comment