Advertisement
Guest User

Untitled

a guest
Dec 12th, 2016
113
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.parkeasy.DAO;
  2.  
  3. import com.google.gson.Gson;
  4. import com.henallux.parkeasy.Model.User;
  5.  
  6. import org.json.JSONObject;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.InputStreamReader;
  10. import java.io.OutputStream;
  11. import java.io.OutputStreamWriter;
  12. import java.net.HttpURLConnection;
  13. import java.net.URL;
  14.  
  15. public class UserDAO {
  16. public static String TOKEN = "";
  17. private int responseCode;
  18. private String stringJSON = "";
  19.  
  20. public int saveUser(User user) throws Exception{
  21. try{
  22. URL url = new URL("http://parkeasyapi.azurewebsites.net/api/Account/Register");
  23. HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  24. connection.setRequestMethod("POST");
  25. connection.setRequestProperty("Content-Type", "application/json");
  26. connection.setDoOutput(true);
  27. OutputStream outputStream = connection.getOutputStream();
  28. OutputStreamWriter writer = new OutputStreamWriter(outputStream);
  29. connection.connect();
  30. writer.write(userToJson(user));
  31. writer.flush();
  32. responseCode = connection.getResponseCode();
  33. writer.close();
  34. outputStream.close();
  35. connection.disconnect();
  36. } catch (Exception e){
  37. e.printStackTrace();
  38. }
  39. return responseCode;
  40. }
  41. public <T>String userToJson(T user){
  42. Gson gson = new Gson();
  43. String jsonString = gson.toJson(user);
  44. return jsonString;
  45. }
  46.  
  47. public int getToken(String email, String password) throws Exception{
  48. try{
  49. URL url = new URL("http://parkeasyapi.azurewebsites.net/token");
  50. HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  51. connection.setRequestMethod("POST");
  52. connection.setRequestProperty("Content-Type", "x-www-form-urlencoded");
  53. connection.setDoOutput(true);
  54. OutputStream outputStream = connection.getOutputStream();
  55. OutputStreamWriter writer = new OutputStreamWriter(outputStream);
  56. connection.connect();
  57. writer.write("Username="+email+"&Password="+password+"&grant_type=password");
  58. writer.flush();
  59. responseCode = connection.getResponseCode();
  60. writer.close();
  61. outputStream.close();
  62.  
  63. if(responseCode == 200) {
  64. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  65. StringBuilder stringBuilder = new StringBuilder();
  66. String line;
  67. while ((line = bufferedReader.readLine()) != null) {
  68. stringBuilder.append(line);
  69. }
  70. bufferedReader.close();
  71. stringJSON = stringBuilder.toString();
  72.  
  73. JSONObject jsonObject = new JSONObject(stringJSON);
  74. TOKEN = jsonObject.getString("access_token");
  75. }
  76. connection.disconnect();
  77. } catch(Exception e){
  78. e.printStackTrace();
  79. }
  80. return responseCode;
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement