Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.india.gov.helperapp;
- import android.app.ProgressDialog;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.widget.Toast;
- import android.util.Log;
- import android.content.Intent;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import java.io.IOException;
- import butterknife.ButterKnife;
- import butterknife.Bind;
- import okhttp3.MediaType;
- import okhttp3.OkHttpClient;
- import okhttp3.Request;
- import okhttp3.RequestBody;
- import okhttp3.Response;
- /**
- * Created by vyas on 3/25/17.
- */
- public class LoginActivity extends AppCompatActivity {
- private static final String TAG = "LoginActivity";
- private static final int REQUEST_SIGNUP = 0;
- @Bind(R.id.input_username) EditText _usernameText;
- @Bind(R.id.input_password) EditText _passwordText;
- @Bind(R.id.btn_login) Button _loginButton;
- @Bind(R.id.link_signup) TextView _signupLink;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- ButterKnife.bind(this);
- _loginButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- login();
- }
- });
- _signupLink.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // Start the Signup activity
- Intent intent = new Intent(getApplicationContext(), SignupActivity.class);
- startActivityForResult(intent, REQUEST_SIGNUP);
- finish();
- }
- });
- }
- public void login() {
- Log.d(TAG, "Login");
- if (!validate()) {
- onLoginFailed();
- return;
- }
- _loginButton.setEnabled(false);
- final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
- progressDialog.setIndeterminate(true);
- progressDialog.setMessage("Authenticating...");
- progressDialog.show();
- // TODO: Implement your own authentication logic here.
- new android.os.Handler().postDelayed(
- new Runnable() {
- public void run() {
- String username = _usernameText.getText().toString();
- String password = _passwordText.getText().toString();
- String result;
- MediaType JSON
- = MediaType.parse("application/json; charset=utf-8");
- OkHttpClient client = new OkHttpClient();
- String json = "{username:\""+username+"\",password:\""+password+"\"}";
- String url = "192.168.43.143:8080";
- RequestBody body = RequestBody.create(JSON, json);
- Request request = new Request.Builder()
- .url(url)
- .post(body)
- .build();
- Response response = null;
- try {
- response = client.newCall(request).execute();
- result = response.body().string();
- } catch (IOException e) {
- e.printStackTrace();
- return;
- }
- onLoginSuccess();
- // onLoginFailed();
- progressDialog.dismiss();
- }
- }, 3000);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode == REQUEST_SIGNUP) {
- if (resultCode == RESULT_OK) {
- // TODO: Implement successful signup logic here
- // By default we just finish the Activity and log them in automatically
- this.finish();
- }
- }
- }
- @Override
- public void onBackPressed() {
- // Disable going back to the MainActivity
- moveTaskToBack(true);
- }
- public void onLoginSuccess() {
- _loginButton.setEnabled(true);
- finish();
- }
- public void onLoginFailed() {
- Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();
- _loginButton.setEnabled(true);
- }
- public boolean validate() {
- boolean valid = true;
- String password = _passwordText.getText().toString();
- if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
- _passwordText.setError("between 4 and 10 alphanumeric characters");
- valid = false;
- } else {
- _passwordText.setError(null);
- }
- return valid;
- }
- }
Add Comment
Please, Sign In to add comment