0
Following the tutorial below,
http://www.vogella.com/tutorials/Retrofit/article.html
which deals with the use of retrofit library, for testing, I created:
One Activity who I called retrofit and a BUTTON in it
package carcleo.com.radiosingular;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import carcleo.com.radiosingular.classes.Clientes;
import carcleo.com.radiosingular.classes.ClientesI;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
public class retrofit extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.retrofit);
Button btnRf = (Button) findViewById(R.id.btnRf);
btnRf.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClientesI clientes = ClientesI.retrofit.create(ClientesI.class);
final Call<Clientes> call = clientes.getClientes();
call.enqueue(new Callback<Clientes>() {
@Override
public void onResponse(Call<Clientes> call, Response<Clientes> response) {
int code = response.code();
if (code == 200) {
Clientes cliente = response.body();
Toast.makeText(getBaseContext(), "Id do cliente: " + cliente.getIdClientesT(), Toast.LENGTH_LONG).show();
Toast.makeText(getBaseContext(), "Tipo do cliente: " + cliente.getTipo(), Toast.LENGTH_LONG).show();
Toast.makeText(getBaseContext(), "Nome do cliente: " + cliente.getNome(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(), "Falha: " + String.valueOf(code), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<Clientes> call, Throwable t) {
Toast.makeText(getBaseContext(), t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
);
}
}
One Interface Clientesi
package carcleo.com.radiosingular.classes;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;
public interface ClientesI {
@GET("/")
Call<Clientes> getClientes();
public static final Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://hotplateprensas.com.br/ws/clientest.php/")
.addConverterFactory(GsonConverterFactory.create())
.build();
}
To url of code above already delivers a string format JSON,that is just below
{"clientes":[{"idClientesT":"1","tipo":"s","nome":"Carlos"},{"idClientesT":"2","tipo":"s","nome":"Rogério"}]}
Turns out when you get on the line
final Call<Clientes> call = clientes.getIdClientesT("");
is giving error in console.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: carcleo.com.radiosingular, PID: 4015
java.lang.IllegalArgumentException: HTTP method annotation is required (e.g., @GET, @POST, etc.).
for method ClientesI.getClientes
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720)
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:711)
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:174)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
at java.lang.reflect.Proxy.invoke(Proxy.java:813)
at $Proxy0.getClientes(Unknown Source)
at carcleo.com.radiosingular.retrofit$1.onClick(retrofit.java:27)
at android.view.View.performClick(View.java:5640)
at android.view.View$PerformClick.run(View.java:22455)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
in the method OnResponse
, a reference is made Call<Clientes>
public void onResponse(Call<Clientes> call, Response<Clientes> response) {
And it seems that a search is made and nothing is returned. Because it searches for a parameter in the url
that doesn’t exist.
Where am I going wrong?
Please avoid long discussions in the comments; your talk was moved to the chat (and can proceed normally there, just click on the link)
– Bacco