Попробуйте этот код ..
Добавить приведенную ниже зависимость в файл gradle уровня приложения.
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
затем после того, как сделать ниже всех отдельных классов
Первый объект Retrofit создать класс, как показано ниже ..
public class ApiClient {
private final static String BASE_URL = "https://dog.ceo/api/breed/";
public static ApiClient apiClient;
private Retrofit retrofit = null;
private Retrofit retrofit2 = null;
public static ApiClient getInstance() {
if (apiClient == null) {
apiClient = new ApiClient();
}
return apiClient;
}
//private static Retrofit storeRetrofit = null;
public Retrofit getClient() {
return getClient(null);
}
private Retrofit getClient(final Context context) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.readTimeout(60, TimeUnit.SECONDS);
client.writeTimeout(60, TimeUnit.SECONDS);
client.connectTimeout(60, TimeUnit.SECONDS);
client.addInterceptor(interceptor);
client.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
return chain.proceed(request);
}
});
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
}
затем создайте интерфейс API, как показано ниже.
public interface ApiInterface {
@POST("login/")
Call<LoginResponseModel> loginCheck(@Body UserData data);
}
сделать вызов pojo для ответа сервера и ввода данных пользователем.
public class LoginResponseModel {
@SerializedName("message") // here define your json key
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
пользовательский класс ввода
public class UserData {
private String email,password;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
private void getLogin(){
ApiInterface apiInterface=ApiClient.getInstance().getClient().create(ApiInterface.class);
UserData data=new UserData();
data.setEmail("abc@gmail.com");
data.setPassword("123456");
Call<LoginResponseModel> loginResponseModelCall=apiInterface.loginCheck(data);
loginResponseModelCall.enqueue(new Callback<LoginResponseModel>() {
@Override
public void onResponse(Call<LoginResponseModel> call, retrofit2.Response<LoginResponseModel> response) {
if (response.isSuccessful() && response !=null && response.body() !=null){
LoginResponseModel loginResponseModel=response.body();
}
}
@Override
public void onFailure(Call<LoginResponseModel> call, Throwable t) {
}
});
}
Когда нет необходимости в пользовательском взаимодействии, в этот раз используется метод GET.
вы создаете класс pojo, а затем используете приведенную ниже ссылку, он генерирует класс pojo и вставляет ваши данные json в ..
http://www.jsonschema2pojo.org/