Guest User

Untitled

a guest
Sep 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. import retrofit2.Call;
  2. import retrofit2.http.Field;
  3. import retrofit2.http.FormUrlEncoded;
  4. import retrofit2.http.POST;
  5. public interface ServerApi {
  6.  
  7.  
  8. @FormUrlEncoded
  9. @POST("api/auth/login")
  10. Call<Client> autoriz(@Field("mail") String mail,
  11. @Field("password") String password);
  12. }
  13.  
  14. import retrofit2.Call;
  15. import retrofit2.Callback;
  16. import retrofit2.Response;
  17. import retrofit2.Retrofit;
  18. import retrofit2.converter.gson.GsonConverterFactory;
  19.  
  20. public class LoginActivity extends AppCompatActivity {
  21.  
  22.  
  23. Button btLogIn;
  24.  
  25.  
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.activity_login);
  30.  
  31. btLogIn = findViewById(R.id.logIn);
  32.  
  33. btLogIn.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View view) {
  36. auto();
  37. }
  38. });
  39. }
  40.  
  41. private void auto(){
  42. final Retrofit retrofit = new Retrofit.Builder()
  43. .baseUrl("http://54.93.100.144:1337/")
  44. .addConverterFactory(GsonConverterFactory.create())
  45. .build();
  46. ServerApi serverApi = retrofit.create(ServerApi.class);
  47.  
  48. Client client = new Client();
  49. client.mail = "ddima.ml@yandex.ru";
  50. client.password="qwerty";
  51.  
  52. Call<Client> autorization = serverApi.autoriz(client.getMail(), client.getPassword());
  53.  
  54. autorization.enqueue(new Callback<Client>() {
  55. @Override
  56. public void onResponse(Call<Client> call, Response<Client> response) {
  57. if(response.isSuccessful()){
  58. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  59. startActivity(intent);
  60. }
  61. else{ Log.i("Loog", "Response " +response.code());
  62. Log.i("Loog", "Response " +response.errorBody());}
  63. }
  64.  
  65. @Override
  66. public void onFailure(Call<Client> call, Throwable t) {
  67. Toast.makeText(LoginActivity.this,"Ошибка " + t.getMessage(), Toast.LENGTH_LONG).show();
  68. return;
  69. }
  70. });
  71. }}
Add Comment
Please, Sign In to add comment