0
Well, I’m asking for more help from friends so really, I’m not getting the dynamics.
Following tutorials and explanations here from the forum I am TRYING to use the Retrofit but it’s not working.
I got the following JSON
down below,
{
"clientes":
[
{"idClientesT":"1","tipo":"s","nome":"Carlos"},
{"idClientesT":"2","tipo":"s","nome":"Rogério"}
]
}
That I get when I access URL down below,
http://hotplateprensas.com.br/ws/clientest.php
I’m wearing 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 class of Customers:
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 walk the array JSON
obtained by access to webservice and return a list of objects of class Customers.
Error only occurs at runtime.
But for me the error is of encoding same. Of logic.
Note: I NAY can receive
code == 200
You’re walking right into this block:
@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 cannot manipulate the return (Sponse)
Console error 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 someone help me?
In this case, I understand that, in addition to the Clients class, I must also have Clienteresponse. That’s right?
– Carlos Rocha
@Carlosrocha is just that
– Leonardo Lima
Thanks, it worked out!
– Carlos Rocha