login.java
public class login extends AppCompatActivity {
TextView email, pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
email = (TextView) findViewById(R.id.log);
pass = (TextView) findViewById(R.id.pass);
}
public void reg1(View view) {
Intent intent = new Intent(login.this, register.class);
startActivity(intent);
}
public void vhodakk(View view) {
if (email.getText().toString().length() == 0) {
email.setError("Введите Email");
}
if (pass.getText().toString().length() == 0) {
pass.setError("Ведите пароль");
} else {
Gson gson = new GsonBuilder()
.setLenient()
.create();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder client = new OkHttpClient.Builder()
.addInterceptor(interceptor);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://mskko2021.mad.hakta.pro/api/")
.client(client.build())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
JSONPlaceHolderApi api = retrofit.create(JSONPlaceHolderApi.class);
final LoginRequest loginRequest = new LoginRequest();
loginRequest.email = email.getText().toString();
loginRequest.password = pass.getText().toString();
Call<LoginResponse> call = api.login(loginRequest);
call.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
LoginResponse loginResponse = response.body();
if (loginResponse != null) {
Log.d("authLog", loginResponse.data.token);
// Intent intent = new Intent(login.this, main.class);
//startActivity(intent);
}
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Log.d("authLog", "Error: " + t.getMessage());
}
});
}
}
}
JSON.java
public interface JSONPlaceHolderApi {
@POST("/user/login")
Call<LoginResponse> login(@Body LoginRequest loginRequest);
}
LiginResponse.java
public class LoginResponse {
@Nullable
@SerializedName("data")
public Data data;
public class Data {
@Nullable
@SerializedName("token")
public String token;
}
@Nullable
@SerializedName("error")
public String error;
@Nullable
@SerializedName("system")
public System system;
public class System {
@Nullable
@SerializedName("time")
public double time;
}
}
LoginRequest.java
public class LoginRequest {
public LoginRequest() {
this.email = email;
this.password = password;
}
@SerializedName("email")
String email;
@SerializedName("password")
String password;
Выдает
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
W/e.madmeditatio: Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (greylist,core-platform-api, reflection, allowed)
Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (greylist,core-platform-api, reflection, allowed)
Accessing hidden method Ldalvik/system/CloseGuard;->warnIfOpen()V (greylist,core-platform-api, reflection, allowed)
W/e.madmeditatio: Accessing hidden field Lsun/misc/Unsafe;->theUnsafe:Lsun/misc/Unsafe; (greylist, reflection, allowed)
Accessing hidden method Lsun/misc/Unsafe;->allocateInstance(Ljava/lang/Class;)Ljava/lang/Object; (greylist, reflection, allowed)
D/OkHttp: --> POST http://mskko2021.mad.hakta.pro/user/login http/1.1
Content-Type: application/json; charset=UTF-8
Content-Length: 32
{"email":"wsr","password":"wsr"}
D/OkHttp: --> END POST (32-byte body)
D/OkHttp: <-- 200 OK http://mskko2021.mad.hakta.pro/user/login (1208ms)
D/OkHttp: Date: Wed, 24 Mar 2021 08:53:21 GMT
Server: Apache/2.4.10 (Debian)
Refresh: 0; url=http://mskko2021.mad.hakta.pro/auth?url=http://mskko2021.mad.hakta.pro/user/login
Content-Length: 0
Content-Type: text/html; charset=UTF-8
X-Cache: MISS from ics.mf.local
X-Cache-Lookup: MISS from ics.mf.local:3128
Via: 1.1 ics.mf.local (squid)
Connection: keep-alive
<-- END HTTP (0-byte body)
D/authLog: Error: End of input at line 1 column 1 path $