I am obliged to put all the code given that I can not find a solution to this, and I have already seen several questions with the same subject, but not even that way. PUT and DELETE do not walk me.
I have these classes on Android
-RetrofiCliente.java
public class RetrofitCliente {
private static Retrofit retrofit = null;
public static Retrofit getClient(String url){
if(retrofit == null){
retrofit = new Retrofit.Builder().baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
-APIUtils.java
public class APIUtils {
private APIUtils(){
};
public static final String API_URL = "http://10.51.1.136:100/api/";
public static ClienteService getUserService(){
return RetrofitCliente.getClient(API_URL).create(ClienteService.class);
}
}
-ClientService.java
public interface ClienteService {
@GET("prueba/{id}/")
Call<List<Cliente>> listRepos(@Path("id") String user);
@GET("prueba")
Call<List<Cliente>> getCliente();
@POST("prueba/agregar")
Call<Cliente> insertCliente(@Body Cliente cliente);
@PUT("/prueba/actualizar/{id}/")
Call<Cliente> updateCliente(@Path("id") int id, @Body Cliente cliente);
@DELETE("/prueba/eliminar/{id}/")
Call<Cliente> deleteCliente(@Path("id") int id);
}
-ClientActivity
public class ClienteActivity extends AppCompatActivity {
ClienteService clienteService;
EditText edtUId;
EditText edtNombre;
EditText edtApellido;
Button btnSave;
Button btnDel;
TextView txtUId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cliente);
setTitle("Users");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
txtUId = (TextView) findViewById(R.id.txtUId);
edtUId = (EditText) findViewById(R.id.edtUId);
edtNombre = (EditText) findViewById(R.id.edtUsername);
edtApellido = (EditText) findViewById(R.id.edtApellido);
btnSave = (Button) findViewById(R.id.btnSave);
btnDel = (Button) findViewById(R.id.btnDel);
clienteService = APIUtils.getUserService();
Bundle extras = getIntent().getExtras();
final String userId = extras.getString("id");
String nombre = extras.getString("nombre");
String apellido = extras.getString("apellido");
edtUId.setText(userId);
edtNombre.setText(nombre);
edtApellido.setText(apellido);
if(userId != null && userId.trim().length() > 0 ){
edtUId.setFocusable(false);
} else {
txtUId.setVisibility(View.INVISIBLE);
edtUId.setVisibility(View.INVISIBLE);
btnDel.setVisibility(View.INVISIBLE);
}
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cliente u = new Cliente();
u.setNombre(edtNombre.getText().toString());
u.setApellido(edtApellido.getText().toString());
if(userId != null && userId.trim().length() > 0){
//update user
updateUser(Integer.parseInt(userId), u);
} else {
//add user
addUser(u);
}
}
});
btnDel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteUser(Integer.parseInt(userId));
Intent intent = new Intent(ClienteActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
public void addUser(Cliente u){
Call<Cliente> call = clienteService.insertCliente(u);
call.enqueue(new Callback<Cliente>() {
@Override
public void onResponse(Call<Cliente> call, Response<Cliente> response) {
if(response.isSuccessful()){
Toast.makeText(ClienteActivity.this, "Client created successfully!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Cliente> call, Throwable t) {
Log.e("ERROR: ", t.getMessage());
}
});
}
public void updateUser(int id, Cliente u){
Call<Cliente> call = clienteService.updateCliente(id, u);
call.enqueue(new Callback<Cliente>() {
@Override
public void onResponse(Call<Cliente> call, Response<Cliente> response) {
if(response.isSuccessful()){
Toast.makeText(ClienteActivity.this, "Client updated successfully!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Cliente> call, Throwable t) {
Log.e("ERROR: ", t.getMessage());
}
});
}
public void deleteUser(int id){
Call<Cliente> call = clienteService.deleteCliente(id);
call.enqueue(new Callback<Cliente>() {
@Override
public void onResponse(Call<Cliente> call, Response<Cliente> response) {
if(response.isSuccessful()){
Toast.makeText(ClienteActivity.this, "Client deleted successfully!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Cliente> call, Throwable t) {
Log.e("ERROR: ", t.getMessage());
}
});
}
}
I must indicate that I did this while following a video and adapted it to my needs, and I have already repeated it several times to try to find my error, only GET and POST work, but when I give UPDATE in the log it does not He shows me nothing but still does not update anything and when I give him DELETE he shows me this.
E / Editor: hideClipTrayIfNeeded () TextView is focused !! hideClipTray ()