Advertisement
Guest User

Untitled

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