Advertisement
Guest User

Untitled

a guest
Dec 14th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. package com.henallux.oml.DataAccess;
  2.  
  3. import com.google.gson.Gson;
  4. import com.henallux.oml.Model.User;
  5.  
  6. import org.json.JSONObject;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.InputStreamReader;
  10. import java.io.OutputStreamWriter;
  11. import java.net.HttpURLConnection;
  12. import java.net.URL;
  13.  
  14. /**
  15. * Created by HAUTIER_Clement on 12-12-16.
  16. */
  17.  
  18. public class AccountDAO {
  19. public static String TOKEN = "";
  20. private int codeResult;
  21.  
  22. public int inscription(User user) throws Exception
  23. {
  24. try {
  25. URL url = new URL("http://onmangelocal-web.azurewebsites.net/api/Account/Register");
  26. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  27. connection.setRequestMethod("POST");
  28. connection.setRequestProperty("Content-Type", "application/json");
  29. connection.setDoOutput(true); // Triggers POST.
  30. OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
  31. connection.connect();
  32. String data = userToJSON(user);
  33. writer.write(data);
  34. writer.flush();
  35. codeResult = connection.getResponseCode();
  36. writer.close();
  37. connection.disconnect();
  38. }
  39. catch (Exception e)
  40. {
  41. e.printStackTrace();
  42. }
  43. return codeResult;
  44. }
  45. private String userToJSON(User user)
  46. {
  47. Gson gson = new Gson();
  48. return gson.toJson(gson);
  49. }
  50.  
  51. public int connection(String userLogin, String userPassword) throws Exception {
  52. try {
  53. URL url = new URL("http://onmangelocal-web.azurewebsites.net/token");
  54. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  55. connection.setRequestMethod("POST");
  56. connection.setRequestProperty("Content-Type", "x-www-form-urlencoded");
  57. connection.setDoOutput(true); // Triggers POST.
  58. OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
  59. connection.connect();
  60. String data = "Username=" + userLogin + "&Password=" + userPassword + "&grant_type=password";
  61. writer.write(data);
  62. writer.flush();
  63. codeResult = connection.getResponseCode();
  64. if (codeResult == 200) {
  65. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  66. StringBuilder stringBuilder = new StringBuilder();
  67. String line;
  68. while ((line = reader.readLine()) != null) {
  69. stringBuilder.append(line);
  70. }
  71. reader.close();
  72. jsonToToken(stringBuilder.toString()); // Send stringJSON to edit "TOKEN"
  73. }
  74. connection.disconnect();
  75. }
  76. catch (Exception e)
  77. {
  78. e.printStackTrace();
  79. }
  80. return codeResult;
  81. }
  82. private void jsonToToken(String stringJSON) throws Exception
  83. {
  84. JSONObject jsonObject = new JSONObject(stringJSON);
  85. TOKEN = jsonObject.getString("access_token");
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement