Well, I'm asking for more help from friends, but I really can not understand the dynamics.
Following tutorials and explanations here of the forum I am TRYING to use Retrofit but it is not working.
I have the following JSON
below,
{
"clientes":
[
{"idClientesT":"1","tipo":"s","nome":"Carlos"},
{"idClientesT":"2","tipo":"s","nome":"Rogério"}
]
}
What do I get when I access URL below,
I'm using it like this:
Interface:
package carcleo.com.radiosingular.classes;
import java.util.List;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;
public interface ClientesI {
@GET("clientest.php")
Call<List<Clientes>> listClientes();
}
Activity:
package carcleo.com.radiosingular;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.List;
import carcleo.com.radiosingular.classes.Clientes;
import carcleo.com.radiosingular.classes.ClientesI;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class retrofit extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.retrofit);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.hotplateprensas.com.br/ws/")
.addConverterFactory(GsonConverterFactory.create())
.build();
ClientesI service = retrofit.create(ClientesI.class);
service.listClientes().enqueue(new Callback<List<Clientes>>() {
@Override public void onResponse(Call<List<Clientes>> call, Response<List<Clientes>> response) {
int code = response.code();
if (code == 200) {
for (Clientes cli : response.body()) {
Toast.makeText(getBaseContext(), "Id do cliente: " + cli.getIdClientesT(), Toast.LENGTH_LONG).show();
Toast.makeText(getBaseContext(), "Tipo do cliente: " + cli.getTipo(), Toast.LENGTH_LONG).show();
Toast.makeText(getBaseContext(), "Nome do cliente: " + cli.getNome(), Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getBaseContext(), "Falha: " + String.valueOf(code), Toast.LENGTH_LONG).show();
}
}
@Override public void onFailure(Call<List<Clientes>> call, Throwable t) {
Log.v("Errado: ", t.toString());
Toast.makeText(getBaseContext(), t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
The Customer class:
package carcleo.com.radiosingular.classes;
public class Clientes {
private int idClientesT;
private String tipo;
private String nome;
public Clientes(int idClientesT, String tipo, String nome) {
this.idClientesT = idClientesT;
this.tipo = tipo;
this.nome = nome;
}
public int getIdClientesT() {
return idClientesT;
}
public String getTipo() {
return tipo;
}
public String getNome() {
return nome;
}
}
The goal is to go through the array JSON
obtained by webservice access and return a list of objects > from class Customers .
The error only occurs at runtime.
But for me the error is coding itself. Of logic.
Note: I NOT can receive
code == 200
You are entering this block directly:
@Override public void onFailure(Call<List<Clientes>> call, Throwable t) {
Log.v("Errado: ", t.toString());
Toast.makeText(getBaseContext(), t.getMessage(), Toast.LENGTH_LONG).show();
}
But I can not handle the response (response)
Error in console RUN :
W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
D/OpenGLRenderer: endAllActiveAnimators on 0x9a465680 (RippleDrawable) with handle 0x9a5fd370
V/Errado:: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
Can anyone help me?