У меня есть API, Api = "https://retailapi.airtechsolutions.pk/api/login/[email protected]/admin/"
Я новичок в Retrofit2, если кто-нибудь может помочь мне с моим кодом
У меня есть ошибка при выполнении запроса GET и POST
При использовании Get Api: имя пользователя и пароль недействительны
При использовании Post Api: я получаю код ошибки 404
Активность входа:
private JsonPlaceHolderApi jsonPlaceHolderApi;
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://retailapi.airtechsolutions.pk/api/login/[email protected]/")
.addConverterFactory(GsonConverterFactory.create())
.build();
jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
getLogin();
createPost();
}
private void getLogin()
{
Call<Login> call = jsonPlaceHolderApi.getLogin();
call.enqueue(new Callback<Login>()
{
@Override
public void onResponse(Call<Login> call, Response<Login> response)
{
if (!response.isSuccessful())
{
Log.i("Code: " , String.valueOf(response.code()));
return;
}
Login logins = response.body();
String content = "";
content += "UserName: " + logins.getUsername() + "\n";
content += "Password: " + logins.getPassword() + "\n";
content += "Status: " + logins.getStatus() + "\n";
content += "Description: " + logins.getDescription() + "\n\n";
Log.i("Read me", content);
}
@Override
public void onFailure(Call<Login> call, Throwable t)
{
Log.i("Error", t.getMessage());
}
});
}
private void createPost() {
Login login = new Login("New Name", "New Password");
Call<Login> call = jsonPlaceHolderApi.createPost(login);
call.enqueue(new Callback<Login>() {
@Override
public void onResponse(Call<Login> call, Response<Login> response) {
if (!response.isSuccessful())
{
Log.i("Code: " , String.valueOf(response.code()));
return;
}
Login loginResponse = response.body();
String content = "";
content += "Code: " + response.code() + "\n";
content += "UserName: " + loginResponse.getUsername() + "\n";
content += "Password: " + loginResponse.getPassword() + "\n";
content += "Status: " + loginResponse.getStatus() + "\n";
content += "Description: " + loginResponse.getDescription() + "\n\n";
Log.i("Read me",content);
}
@Override
public void onFailure(Call<Login> call, Throwable t) {
Log.i("Failure", t.getMessage());
}
});
}
Класс входа:
String username;
String password;
int Status;
String Description;
public Login(String username, String password)
{
this.username = username;
this.password = password;
}
public String getUsername()
{
return username;
}
public String getPassword()
{
return password;
}
public int getStatus()
{
return Status;
}
public String getDescription()
{
return Description;
}
И класс интерфейса под названием JsonPlaceHolderApi
public interface JsonPlaceHolderApi
{
@GET("admin")
Call<Login> getLogin();
@POST("admin")
Call<Login> createPost(@Body Login login);
}