Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginActivity extends AppCompatActivity {
- private EditText login = null;
- private EditText password = null;
- private RadioButton radioButton = null;
- private Button button = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- login = (EditText)findViewById(R.id.loginEditText);
- password = (EditText)findViewById(R.id.passwordEditText);
- radioButton = (RadioButton)findViewById(R.id.saveRadioButton);
- button = (Button)findViewById(R.id.ConnectionButton);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (login.getText().toString().equals("")
- || password.getText().toString().equals("")) {
- alert(getString(R.string.alertDialoguErrorTitle), getString(R.string.UnfilledFieldLogin));
- } else {
- boolean haveToSave = radioButton.isChecked();
- User user = User.getUser(login.getText().toString(), password.getText().toString());
- try {
- Intranet.login.start();
- Intranet.login.join();
- Intranet.login.interrupt();
- Intranet.login.join();
- } catch (InterruptedException e) {
- alert(getString(R.string.alertDialoguErrorTitle), e.toString());
- login.setText("");
- password.setText("");
- } finally {
- if (!user._token.equals("")) {
- if (haveToSave) {
- // SAVE DATA
- }
- finish();
- } else {
- login.setText("");
- password.setText("");
- alert(getString(R.string.alertDialoguErrorTitle), getString(R.string.badLoginPassword));
- }
- }
- }
- }
- });
- }
- public void alert(String title, String message) {
- AlertDialog.Builder alertDialog = new AlertDialog.Builder(LoginActivity.this);
- alertDialog.setTitle(title);
- alertDialog.setMessage(message);
- alertDialog.setPositiveButton("Close",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- // nothing
- }
- });
- alertDialog.show();
- }
- }
- public class Intranet {
- public static int responseCode = 0;
- public static String responseString = "";
- public static Thread login = new Thread(new Runnable() {
- private OkHttpClient client = new OkHttpClient();
- private String url = "https://epitech-api.herokuapp.com/login";
- private User user = User.getUser();
- public void run() {
- try {
- // Build the request
- RequestBody formBody = new FormEncodingBuilder()
- .add("login", user._login)
- .add("password", user._password)
- .build();
- Request request = new Request.Builder()
- .url(url)
- .post(formBody)
- .build();
- Response responses = null;
- // Reset the response code
- responseCode = 0;
- // Make the request
- responses = client.newCall(request).execute();
- if ((responseCode = responses.code()) == 200) {
- // Get response
- String jsonData = responses.body().string();
- // Transform reponse to JSon Object
- JSONObject json = new JSONObject(jsonData);
- // Use the JSon Object
- user._token = json.getString("token");
- }
- } catch (IOException e) {
- responseString = e.toString();
- } catch (JSONException e) {
- responseString = e.toString();
- }
- ;
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement