public class login extends AppCompatActivity {
EditText etEmail, etPassword;
TextView signin,q1,q2;
Button btlog;
String Email ;
String Password;
private Context ctx = this;
private String token = null;
private String z = "SecretToken";
private String x = "NotFound";
private String c = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
etEmail = (EditText) findViewById(R.id.uniqname);
etPassword = (EditText) findViewById(R.id.uniqpassword);
btlog = (Button) findViewById(R.id.btlog);
signin = (TextView) findViewById(R.id.signup);
signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(login.this,
regitractivity.class);
startActivity(i);
}
});
btlog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Email = etEmail.getText().toString();
Password = etPassword.getText().toString();
GetKeyTask getToken = new GetKeyTask();
getToken.execute();
}
});
}
private void signin() {
Toast.makeText(ctx, token, Toast.LENGTH_LONG).show();
if(token != z)
{
Intent intent = new Intent(login.this,Main.class);
startActivity(intent);
}
}
class GetKeyTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
//Toast.makeText(ctx, "Начинаем асинхрон", Toast.LENGTH_SHORT).show();
}
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("https://3sports.ru/signin/");
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
String urlParameters = "username="+Email+"&password="+Password;
connection.setRequestMethod("POST");
connection.setDoOutput(true);
DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());
dStream.writeBytes(urlParameters);
dStream.flush();
dStream.close();
int responseCode = connection.getResponseCode();
String output = "Request URL " + url;
output += System.getProperty("line.separator") + "Request Parameters " + urlParameters;
output += System.getProperty("line.separator") + "Response Code " + responseCode;
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = "";
StringBuilder responseOutput = new StringBuilder();
while ((line = br.readLine()) != null) {
responseOutput.append(line);
}
br.close();
output += System.getProperty("line.separator") + responseOutput.toString();
token = output;
JSONObject jsonObject = new JSONObject( responseOutput.toString() );
token = jsonObject.getString("token");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
// TextView outputView = (TextView) findViewById(R.id.postOutput);
//Toast.makeText(ctx, "Заканчиваем асинхрон", Toast.LENGTH_SHORT).show();
// outputView.setText( token );
signin();
}
}
}