Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.henallux.oml.DataAccess;
- import android.widget.Toast;
- import com.henallux.oml.Model.User;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.URL;
- /**
- * Created by HAUTIER_Clement on 12-12-16.
- */
- public class AccountDAO {
- public static String TOKEN = "";
- private int codeResult;
- public int inscription(User user) throws Exception
- {
- return 0;
- }
- public int connection(String userLogin, String userPassword) throws Exception {
- try {
- //URL url = new URL("http://onmangelocal-web.azurewebsites.net/token");
- URL url = new URL("http://localhost:65473/token");
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("POST");
- connection.setRequestProperty("Content-Type", "x-www-form-urlencoded");
- connection.setDoOutput(true); // Triggers POST.
- OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
- connection.connect();
- String data = "Username=" + userLogin + "&Password=" + userPassword + "&grant_type=password";
- writer.write(data);
- writer.flush();
- codeResult = connection.getResponseCode();
- if (codeResult == 200) {
- BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- StringBuilder stringBuilder = new StringBuilder();
- String stringJSON = "", line;
- while ((line = reader.readLine()) != null) {
- stringBuilder.append(line);
- }
- reader.close();
- getTOKEN(stringBuilder.toString()); // Send stringJSON to edit "TOKEN"
- }
- connection.disconnect();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return codeResult;
- }
- private void getTOKEN (String stringJSON) throws Exception
- {
- JSONObject jsonObject = new JSONObject(stringJSON);
- TOKEN = jsonObject.getString("access_token");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement