Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import retrofit2.Call;
- import retrofit2.http.Field;
- import retrofit2.http.FormUrlEncoded;
- import retrofit2.http.POST;
- public interface ServerApi {
- @FormUrlEncoded
- @POST("api/auth/login")
- Call<Client> autoriz(@Field("mail") String mail,
- @Field("password") String password);
- }
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- import retrofit2.Retrofit;
- import retrofit2.converter.gson.GsonConverterFactory;
- public class LoginActivity extends AppCompatActivity {
- Button btLogIn;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- btLogIn = findViewById(R.id.logIn);
- btLogIn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- auto();
- }
- });
- }
- private void auto(){
- final Retrofit retrofit = new Retrofit.Builder()
- .baseUrl("http://54.93.100.144:1337/")
- .addConverterFactory(GsonConverterFactory.create())
- .build();
- ServerApi serverApi = retrofit.create(ServerApi.class);
- Client client = new Client();
- client.mail = "ddima.ml@yandex.ru";
- client.password="qwerty";
- Call<Client> autorization = serverApi.autoriz(client.getMail(), client.getPassword());
- autorization.enqueue(new Callback<Client>() {
- @Override
- public void onResponse(Call<Client> call, Response<Client> response) {
- if(response.isSuccessful()){
- Intent intent = new Intent(LoginActivity.this, MainActivity.class);
- startActivity(intent);
- }
- else{ Log.i("Loog", "Response " +response.code());
- Log.i("Loog", "Response " +response.errorBody());}
- }
- @Override
- public void onFailure(Call<Client> call, Throwable t) {
- Toast.makeText(LoginActivity.this,"Ошибка " + t.getMessage(), Toast.LENGTH_LONG).show();
- return;
- }
- });
- }}
Add Comment
Please, Sign In to add comment